### Question:
SentinelCircuitBreaker implements CircuitBreaker { @Override public <T> T run(Supplier<T> toRun, Function<Throwable, T> fallback) { Entry entry = null; try { entry = SphU.entry(resourceName, entryType); return toRun.get(); } catch (BlockException ex) { return fallback.apply(ex); } catch (Exception ex) { Tracer.trace(ex); return fallback.apply(ex); } finally { if (entry != null) { entry.exit(); } } } SentinelCircuitBreaker(String resourceName, EntryType entryType,
			List<DegradeRule> rules);  SentinelCircuitBreaker(String resourceName, List<DegradeRule> rules);  SentinelCircuitBreaker(String resourceName); @Override T run(Supplier<T> toRun, Function<Throwable, T> fallback);  }### Answer:
@Test public void testCreateDirectlyThenRun() { CircuitBreaker cb = new SentinelCircuitBreaker( "testSentinelCreateDirectlyThenRunA"); assertThat(cb.run(() -> "Sentinel")).isEqualTo("Sentinel"); assertThat(DegradeRuleManager.hasConfig("testSentinelCreateDirectlyThenRunA")) .isFalse(); CircuitBreaker cb2 = new SentinelCircuitBreaker( "testSentinelCreateDirectlyThenRunB", Collections.singletonList( new DegradeRule("testSentinelCreateDirectlyThenRunB") .setCount(100).setTimeWindow(10))); assertThat(cb2.run(() -> "Sentinel")).isEqualTo("Sentinel"); assertThat(DegradeRuleManager.hasConfig("testSentinelCreateDirectlyThenRunB")) .isTrue(); }
@Test public void testCreateWithNullRule() { String id = "testCreateCbWithNullRule"; CircuitBreaker cb = new SentinelCircuitBreaker(id, Collections.singletonList(null)); assertThat(cb.run(() -> "Sentinel")).isEqualTo("Sentinel"); assertThat(DegradeRuleManager.hasConfig(id)).isFalse(); }
@Test public void testCreateFromFactoryThenRun() { CircuitBreaker cb = new SentinelCircuitBreakerFactory().create("testSentinelRun"); assertThat(cb.run(() -> "foobar")).isEqualTo("foobar"); }
@Test public void testRunWithFallback() { CircuitBreaker cb = new SentinelCircuitBreakerFactory() .create("testSentinelRunWithFallback"); assertThat(cb.<String>run(() -> { throw new RuntimeException("boom"); }, t -> "fallback")).isEqualTo("fallback"); }