/** * 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(); } }
/** * 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); }