public void CircuitBreakTest() { var policy = PollyService.CircuitBreak <AngleExceptions>(_retryCount, TimeSpan.FromSeconds(10)); try { while (true) { try { policy.Execute(() => { _retryCount--; Console.WriteLine("Job Start"); throw new AngleExceptions("Special error occured"); }); } catch (AngleExceptions ex) { Console.WriteLine("There's one unhandled exception : " + ex.Message); } System.Threading.Thread.Sleep(500); } } catch { Assert.True(_retryCount == 0); } }
public async Task BreakCircuitAndThrow() { var mockLogger = new Mock <ILogger>(); mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("Breaking circuit")))).Verifiable(); var pollyService = new PollyService(mockLogger.Object, 10); for (int i = 0; i < 10; i++) { try { var posts = await pollyService.GetWithPolicy <string>( PolicyTypes.CircuitBreaker, async() => { await Task.Delay(40); throw new Exception("BOEM"); }, null).ConfigureAwait(false); } catch (Exception ex) { // expected } } mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("Breaking circuit"))), Times.AtLeast(1)); }
public void PollyRetryTset() { var policy = PollyService.Retry <MicroAngels.Core.AngleExceptions>(_retryCount, (e, n) => { Console.WriteLine(n); Console.WriteLine(e.ToString()); _retryCount--; }); try { policy.Execute(() => { Console.WriteLine("Job Start"); if (DateTime.Now.Second % 10 != 0) { throw new AngleExceptions("Special error occured"); } Console.WriteLine("Job End"); }); } catch { Assert.True(_retryCount == 0); } }
public async Task BreakCircuitRetryAndFinallyReturnFallbackData() { var mockLogger = new Mock <ILogger>(); mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("FallbackPolicy invoked")))).Verifiable(); mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("Breaking circuit")))).Verifiable(); mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("RetryPolicy invoked")))).Verifiable(); var pollyService = new PollyService(mockLogger.Object); for (int i = 0; i < 10; i++) { try { var data = await pollyService.GetWithPolicy <string>( PolicyTypes.CircuitBreakerWithRetryAndFallBack, () => throw new Exception("BOEM"), () => Task.FromResult("test")).ConfigureAwait(false); data.Should().Be("test"); mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("FallbackPolicy invoked"))), Times.AtLeastOnce()); mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("RetryPolicy invoked"))), Times.Exactly(3)); } catch (Exception ex) { // expected } } mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("Breaking circuit"))), Times.AtLeastOnce()); }
public async Task ReturnFallbackData() { var mockLogger = new Mock <ILogger>(); mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("FallbackPolicy invoked")))).Verifiable(); var pollyService = new PollyService(mockLogger.Object); var result = await pollyService.GetWithPolicy <string>(PolicyTypes.Fallback, () => throw new Exception("BOEM"), () => Task.FromResult("test") ); result.Should().Be("test"); mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("FallbackPolicy invoked"))), Times.Exactly(1)); }
public void RetryThreeTimesBeforeThrowing() { var mockLogger = new Mock <ILogger>(); mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("RetryPolicy invoked")))).Verifiable(); var pollyService = new PollyService(mockLogger.Object); var action = new Func <Task>(async() => await pollyService.GetWithPolicy <string>(PolicyTypes.Retry, () => throw new Exception("BOEM"), null )); action.Should().Throw <Exception>().WithMessage("BOEM"); mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("RetryPolicy invoked"))), Times.Exactly(3)); }
public async Task RetryThreeTimesAndReturnFallbackData() { var mockLogger = new Mock <ILogger>(); mockLogger.Setup(x => x.Write(It.Is <string>(y => y.Equals("RetryPolicy invoked")))).Verifiable(); var pollyService = new PollyService(mockLogger.Object); var data = await pollyService.GetWithPolicy <string>( PolicyTypes.RetryWithFallBack, () => throw new Exception("BOEM"), () => Task.FromResult("test")).ConfigureAwait(false); data.Should().Be("test"); mockLogger.Verify(x => x.Write(It.Is <string>(y => y.Equals("RetryPolicy invoked"))), Times.Exactly(3)); }
public void TimeoutTest() { var policy = PollyService.Timeout(TimeSpan.FromSeconds(5), () => { System.Console.WriteLine("time out"); }); try { policy.Execute(() => { Console.WriteLine("Job Start"); System.Threading.Thread.Sleep(10000); Console.WriteLine("Job Stop"); }); } catch { System.Console.WriteLine("gone"); } }
public WithPolly(IRemoteApiService remoteApiService) { _logger = new ConsoleLogger(); _remoteApiService = remoteApiService; _pollyService = new PollyService(new ConsoleLogger()); }
public PollyController(PollyService service) { _service = service; }
public PollyServiceShould(ITestOutputHelper output) { _logger = new TestLogger(output); _pollyService = new PollyService(_logger); }