public void Call_IgnoreUnauthorisedAccess_ClosesOnOtherException() { // Arrange var cb = new SimpleCircuitBreaker<TestClass>(new TestClass(), new SimpleCircuitBreakerConfig {RetryThreshold = 0}, state: CircuitBreakerState.Closed); // Act cb.Call(tc => tc.DoNothing()); // Assert cb.State.ShouldBe(CircuitBreakerState.Open); }
public void Call_DoNothing_DoesNothing() { // Arrange SimpleCircuitBreaker<TestClass> cb = new SimpleCircuitBreaker<TestClass>(new TestClass(), new SimpleCircuitBreakerConfig {Threshold = 0}); // Act cb.Call(tc => tc.DoNothing()); // Assert cb.State.ShouldBe(CircuitBreakerState.Open); cb.LastException.ShouldBe(null); }
public void CircuitBreaker_ReturnsOnOpenCircuit() { var circuitBreaker = new SimpleCircuitBreaker(); var cache = new CircuitBreakingFluentDictionaryCache(circuitBreaker) .WithSource(new CacheMe()); CacheStrategy <double> cacheStrategy = cache.Method(r => r.DoWork()); Assert.IsFalse(circuitBreaker.IsBroken, "circuit should be open"); CachedValue <double> result1 = cacheStrategy.Get(); Assert.IsNotNull(result1, "because the circuit is open, we should have gotten a result"); CachedValue <double> result2 = cacheStrategy.Get(); Assert.IsNotNull(result2, "because the circuit is open, we should have gotten a result"); Assert.AreEqual(result1.Version, result2.Version, "the first and 2nd results should be the same"); }
public void Call_WithExceptionFilter_IgnoresException() { // Arrange SimpleCircuitBreaker<TestClass> cb = new SimpleCircuitBreaker<TestClass>(new TestClass(), new SimpleCircuitBreakerConfig {Threshold = 0}, exception => false); // Act try { cb.Call(tc => tc.ThrowException()); } catch (Exception) { } // Assert cb.State.ShouldBe(CircuitBreakerState.Open); cb.LastException.ShouldBe(null); }
public void CircuitBreaker_NullOnBrokenCircuit() { var circuitBreaker = new SimpleCircuitBreaker(); var cache = new CircuitBreakingFluentDictionaryCache(circuitBreaker) .WithSource(new CacheMe()); CacheStrategy <double> cacheStrategy = cache.Method(r => r.DoWork()); Assert.IsFalse(circuitBreaker.IsBroken, "circuit should be open"); CachedValue <double> result1 = cacheStrategy.Get(); Assert.IsNotNull(result1, "because the circuit is open, we should have gotten a result"); circuitBreaker.TryBreak(new InvalidOperationException()); Assert.IsTrue(circuitBreaker.IsBroken, "circuit should be closed"); CachedValue <double> result2 = cacheStrategy.Get(); Assert.IsNull(result2, "because the circuit is closed, we should have gotten no result"); }
public void Call_ThrowException_ClosesCircuit() { // Arrange SimpleCircuitBreaker<TestClass> cb = new SimpleCircuitBreaker<TestClass>(new TestClass(), new SimpleCircuitBreakerConfig {Threshold = 0}); Exception exception = null; // Act try { cb.Call(tc => { tc.ThrowException(); }); } catch (Exception e) { exception = e; } // Assert cb.State.ShouldBe(CircuitBreakerState.Closed); exception.ShouldNotBe(null); exception.Message.ShouldBe("fake exception"); }
public void CircuitBreaker_NullOnBrokenCircuitDueToCachingFailure() { var circuitBreaker = new SimpleCircuitBreaker(); var rawCache = new CircuitBreakingFluentDictionaryCache(circuitBreaker); var cache = rawCache.WithSource(new CacheMe()); CacheStrategy <double> cacheStrategy = cache.Method(r => r.DoWork()); Assert.IsFalse(circuitBreaker.IsBroken, "circuit should be open"); CachedValue <double> result1 = cacheStrategy.Get(); Assert.IsNotNull(result1, "because the circuit is open, we should have gotten a result"); //Simulate failure rawCache.ShouldThrowException = true; CachedValue <double> result2 = cacheStrategy.Get(); Assert.IsTrue(circuitBreaker.IsBroken, "circuit should now be broken"); Assert.IsNull(result2, "because the circuit is closed, we should have gotten no result"); }
public void Ctor_Nothing_IsOpenByDefault() { // Arrange SimpleCircuitBreaker<TestClass> cb = new SimpleCircuitBreaker<TestClass>(new TestClass(), new SimpleCircuitBreakerConfig()); // Assert cb.State.ShouldBe(CircuitBreakerState.Open); }
public void Ctor_NullTarget_ThrowsArgumentNull() { // Arrange SimpleCircuitBreaker<TestClass> cb = new SimpleCircuitBreaker<TestClass>(null, new SimpleCircuitBreakerConfig()); }
public void Ctor_NullConfig_ThrowsArgumentNull() { // Arrange SimpleCircuitBreaker<TestClass> cb = new SimpleCircuitBreaker<TestClass>(new TestClass(), null); }