@Test public void testCancelFutureWithoutInterruption() throws InterruptedException, ExecutionException, TimeoutException { // given InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), true, true, 1000); // when Future<Boolean> f = cmd.queue(); Thread.sleep(500); f.cancel(false); Thread.sleep(500); // then try { f.get(); fail("Should have thrown a CancellationException"); } catch (CancellationException e) { assertFalse(cmd.hasBeenInterrupted()); } }
@Test public void testInterruptObserveOnTimeout() throws InterruptedException { // given InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), true); // when cmd.observe().subscribe(); // then Thread.sleep(500); assertTrue(cmd.hasBeenInterrupted()); }
@Test public void testDoNotInterruptToObservableOnTimeoutIfPropertySaysNotTo() throws InterruptedException { // given InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), false); // when cmd.toObservable().subscribe(); // then Thread.sleep(500); assertFalse(cmd.hasBeenInterrupted()); }
@Test public void testCancelFutureWithInterruption() throws InterruptedException, ExecutionException { // given InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), true, true, 1000); // when Future<Boolean> f = cmd.queue(); Thread.sleep(500); f.cancel(true); Thread.sleep(500); // then try { f.get(); fail("Should have thrown a CancellationException"); } catch (CancellationException e) { assertTrue(cmd.hasBeenInterrupted()); } }
@Test public void testSemaphoreExecutionWithTimeout() { TestHystrixCommand<Boolean> cmd = new InterruptibleCommand(new TestCircuitBreaker(), false); System.out.println("Starting command"); long timeMillis = System.currentTimeMillis(); try { cmd.execute(); fail("Should throw"); } catch (Throwable t) { assertNotNull(cmd.getExecutionException()); System.out.println("Unsuccessful Execution took : " + (System.currentTimeMillis() - timeMillis)); assertCommandExecutionEvents(cmd, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING); assertEquals(0, cmd.metrics.getCurrentConcurrentExecutionCount()); assertSaneHystrixRequestLog(1); } }
@Test public void testCancelFutureWithInterruptionWhenPropertySaysNotTo() throws InterruptedException, ExecutionException { // given InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), true, false, 1000); // when Future<Boolean> f = cmd.queue(); Thread.sleep(500); f.cancel(true); Thread.sleep(500); // then try { f.get(); fail("Should have thrown a CancellationException"); } catch (CancellationException e) { assertFalse(cmd.hasBeenInterrupted()); } }
public InterruptibleCommand(TestCircuitBreaker circuitBreaker, boolean shouldInterrupt, boolean shouldInterruptOnCancel, int timeoutInMillis) { super(testPropsBuilder() .setCircuitBreaker(circuitBreaker).setMetrics(circuitBreaker.metrics) .setCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter() .withExecutionIsolationThreadInterruptOnFutureCancel(shouldInterruptOnCancel) .withExecutionIsolationThreadInterruptOnTimeout(shouldInterrupt) .withExecutionTimeoutInMilliseconds(timeoutInMillis))); }
@Test public void testDoNotInterruptFutureOnTimeoutIfPropertySaysNotTo() throws InterruptedException, ExecutionException { // given InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), false); // when Future<Boolean> f = cmd.queue(); // then Thread.sleep(500); assertFalse(cmd.hasBeenInterrupted()); }
@Test public void testInterruptToObservableOnTimeout() throws InterruptedException { // given InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), true); // when cmd.toObservable().subscribe(); // then Thread.sleep(500); assertTrue(cmd.hasBeenInterrupted()); }
@Test public void testInterruptFutureOnTimeout() throws InterruptedException, ExecutionException { // given InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), true); // when Future<Boolean> f = cmd.queue(); // then Thread.sleep(500); assertTrue(cmd.hasBeenInterrupted()); }
@Test public void testDoNotInterruptObserveOnTimeoutIfPropertySaysNotTo() throws InterruptedException { // given InterruptibleCommand cmd = new InterruptibleCommand(new TestCircuitBreaker(), false); // when cmd.observe().subscribe(); // then Thread.sleep(500); assertFalse(cmd.hasBeenInterrupted()); }