CircuitBreaker implements MonitoredService, ServiceWrapper { public <V> V invoke(Callable<V> c) throws Exception { if (!byPass) { if (!allowRequest()) { throw mapException(new CircuitBreakerException()); } try { isAttemptLive = true; V result = c.call(); close(); return result; } catch (Throwable cause) { handleFailure(cause); } throw new IllegalStateException("not possible"); } else { return c.call(); } } CircuitBreaker();  CircuitBreaker(String name);  CircuitBreaker(FailureInterpreter fi);  CircuitBreaker(String name, FailureInterpreter fi);  CircuitBreaker(String name, CircuitBreakerExceptionMapper<? extends Exception> mapper);  CircuitBreaker(String name,
                          FailureInterpreter fi,
                          CircuitBreakerExceptionMapper<? extends Exception> mapper); Throwable getTripException(); String getTripExceptionAsString(); void setClock(Clock clock); Clock getClock(); V invoke(Callable<V> c); void invoke(Runnable r); V invoke(Runnable r, V result); void setByPassState(boolean b); boolean getByPassState(); void trip(); void tripHard(); long getLastTripTime(); long getTripCount(); void reset(); Status getStatus(); ServiceStatus getServiceStatus(); long getResetMillis(); void setResetMillis(long l); String getHealthCheck(); void setLimit(int limit); void setIgnore(Collection<Class<? extends Throwable>> ignore); void setWindowMillis(long windowMillis); void setFailureInterpreter(FailureInterpreter failureInterpreter); FailureInterpreter getFailureInterpreter(); void setExceptionMapper(CircuitBreakerExceptionMapper<? extends Exception> mapper); void addListener(CircuitBreakerNotificationCallback listener); void setListeners(ArrayList<CircuitBreakerNotificationCallback> listeners); CircuitBreakerExceptionMapper<? extends Exception> getExceptionMapper();  }
@Test public void testInvokeWithRunnableResultAndResultReturnsResult() throws Exception { final Object result = new Object(); mockRunnable.run(); replay(mockRunnable); Object theReturned = impl.invoke(mockRunnable, result); verify(mockRunnable); assertSame(result, theReturned); }
@Test public void testInvokeWithRunnableDoesNotError() throws Exception { mockRunnable.run(); replay(mockRunnable); impl.invoke(mockRunnable); verify(mockRunnable); }
@Test public void testStaysClosedOnSuccess() throws Exception { impl.state = CircuitBreaker.BreakerState.CLOSED; final Object obj = new Object(); expect(mockCallable.call()).andReturn(obj); replay(mockCallable); Object result = impl.invoke(mockCallable); verify(mockCallable); assertSame(obj, result); assertEquals(CircuitBreaker.BreakerState.CLOSED, impl.state); }
@Test public void testOpensOnFailure() throws Exception { long start = System.currentTimeMillis(); impl.state = CircuitBreaker.BreakerState.OPEN; expect(mockCallable.call()).andThrow(new RuntimeException()); replay(mockCallable); try { impl.invoke(mockCallable); fail("should have thrown an exception"); } catch (RuntimeException expected) { } long end = System.currentTimeMillis(); verify(mockCallable); assertEquals(CircuitBreaker.BreakerState.OPEN, impl.state); assertTrue(impl.lastFailure.get() >= start); assertTrue(impl.lastFailure.get() <= end); }
@Test public void testOpenDuringCooldownThrowsCBException() throws Exception { impl.state = CircuitBreaker.BreakerState.OPEN; impl.lastFailure.set(System.currentTimeMillis()); replay(mockCallable); try { impl.invoke(mockCallable); fail("should have thrown an exception"); } catch (CircuitBreakerException expected) { } verify(mockCallable); assertEquals(CircuitBreaker.BreakerState.OPEN, impl.state); }
@Test public void testHalfClosedFailureOpensAgain() throws Exception { impl.state = CircuitBreaker.BreakerState.HALF_CLOSED; impl.resetMillis.set(1000); impl.lastFailure.set(System.currentTimeMillis() - 2000); long start = System.currentTimeMillis(); expect(mockCallable.call()).andThrow(new RuntimeException()); replay(mockCallable); try { impl.invoke(mockCallable); fail("should have thrown exception"); } catch (RuntimeException expected) { } long end = System.currentTimeMillis(); verify(mockCallable); assertEquals(CircuitBreaker.BreakerState.OPEN, impl.state); assertTrue(impl.lastFailure.get() >= start); assertTrue(impl.lastFailure.get() <= end); }