public void testRequestCache3UsingThreadIsolation() { TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); SuccessfulCacheableCommand<String> command1 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "A"); SuccessfulCacheableCommand<String> command2 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "B"); SuccessfulCacheableCommand<String> command3 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "A"); assertTrue(command1.isCommandRunningInThread()); Future<String> f1 = command1.observe().toBlocking().toFuture(); Future<String> f2 = command2.observe().toBlocking().toFuture(); Future<String> f3 = command3.observe().toBlocking().toFuture(); assertCommandExecutionEvents(command2, HystrixEventType.EMIT, HystrixEventType.SUCCESS); assertCommandExecutionEvents(command3, HystrixEventType.EMIT, HystrixEventType.SUCCESS, HystrixEventType.RESPONSE_FROM_CACHE); assertTrue(command3.getExecutionTimeInMilliseconds() == -1); assertTrue(command3.isResponseFromCache());
/** * Test that if we try and execute a command with a cacheKey without initializing RequestVariable that it gives an error. */ @Test public void testCacheKeyExecutionRequiresRequestVariable() { try { /* force the RequestVariable to not be initialized */ HystrixRequestContext.setContextOnCurrentThread(null); TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); SuccessfulCacheableCommand<String> command = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "one"); assertEquals("one", command.observe().toBlocking().single()); SuccessfulCacheableCommand<String> command2 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "two"); assertEquals("two", command2.observe().toBlocking().toFuture().get()); fail("We expect an exception because cacheKey requires RequestVariable."); // semaphore isolated assertFalse(command.isExecutedInThread()); assertNull(command.getExecutionException()); } catch (Exception e) { e.printStackTrace(); } }
public void testRequestCache1UsingThreadIsolation() { TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); SuccessfulCacheableCommand<String> command1 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "A"); SuccessfulCacheableCommand<String> command2 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "A"); assertTrue(command1.isCommandRunningInThread()); Future<String> f1 = command1.observe().toBlocking().toFuture(); Future<String> f2 = command2.observe().toBlocking().toFuture(); assertTrue(command1.getExecutionTimeInMilliseconds() > -1); assertFalse(command1.isResponseFromCache()); assertNull(command1.getExecutionException()); assertTrue(command2.getExecutionTimeInMilliseconds() == -1); assertTrue(command2.isResponseFromCache()); assertNull(command2.getExecutionException());
public void testNoRequestCache3UsingThreadIsolation() { TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); SuccessfulCacheableCommand<String> command1 = new SuccessfulCacheableCommand<String>(circuitBreaker, false, "A"); SuccessfulCacheableCommand<String> command2 = new SuccessfulCacheableCommand<String>(circuitBreaker, false, "B"); SuccessfulCacheableCommand<String> command3 = new SuccessfulCacheableCommand<String>(circuitBreaker, false, "A"); assertTrue(command1.isCommandRunningInThread()); Future<String> f1 = command1.observe().toBlocking().toFuture(); Future<String> f2 = command2.observe().toBlocking().toFuture(); Future<String> f3 = command3.observe().toBlocking().toFuture(); assertTrue(command1.isExecutedInThread()); assertTrue(command2.isExecutedInThread()); assertTrue(command3.isExecutedInThread());
/** * Test Request scoped caching doesn't prevent different ones from executing */ @Test public void testRequestCache2UsingThreadIsolation() { TestCircuitBreaker circuitBreaker = new TestCircuitBreaker(); SuccessfulCacheableCommand<String> command1 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "A"); SuccessfulCacheableCommand<String> command2 = new SuccessfulCacheableCommand<String>(circuitBreaker, true, "B"); assertTrue(command1.isCommandRunningInThread()); Future<String> f1 = command1.observe().toBlocking().toFuture(); Future<String> f2 = command2.observe().toBlocking().toFuture(); try { assertEquals("A", f1.get()); assertEquals("B", f2.get()); } catch (Exception e) { throw new RuntimeException(e); } assertTrue(command1.executed); // both should execute as they are different assertTrue(command2.executed); assertCommandExecutionEvents(command1, HystrixEventType.EMIT, HystrixEventType.SUCCESS); assertCommandExecutionEvents(command2, HystrixEventType.EMIT, HystrixEventType.SUCCESS); assertTrue(command2.getExecutionTimeInMilliseconds() > -1); assertFalse(command2.isResponseFromCache()); assertEquals(0, circuitBreaker.metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(2); }
public SuccessfulCacheableCommand(TestCircuitBreaker circuitBreaker, boolean cacheEnabled, T value) { super(testPropsBuilder().setCircuitBreaker(circuitBreaker).setMetrics(circuitBreaker.metrics) .setCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter().withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD))); this.value = value; this.cacheEnabled = cacheEnabled; }