protected override async Task OnProcessAsync(CancellationToken cancellationToken = default) { Console.WriteLine("Retry 1"); Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); var retry = new LinearRetryPolicy(3, TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(100)); await retry.ProcessAsync(() => { Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); throw new ApplicationException(); }, typeof(ApplicationException)); Console.WriteLine("Retry 2"); Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); try { await retry.ProcessAsync(() => { Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); throw new InvalidOperationException(); }, typeof(ApplicationException)); } catch (InvalidOperationException) { } var h = new ExceptionHandler(); h.Add <ApplicationException>(ex => { return(null); }); h.Add <ArgumentException>(ex => { if (ex.Message == "name") { return(null); } return(new ApplicationException()); }); Console.WriteLine("Retry 3"); Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); await retry.ProcessAsync(() => { Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); throw new ApplicationException(); }, h.GetException); Console.WriteLine("Retry 4"); Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); await retry.ProcessAsync(() => { Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); throw new ArgumentException("name"); }, h.GetException); Console.WriteLine("Retry 5"); Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); try { await retry.ProcessAsync(() => { Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); throw new ArgumentException("other"); }, h.GetException); } catch (ApplicationException) { } }
public async Task TestLinearAsync() { var count = 0; var now = DateTime.Now; var retry = new LinearRetryPolicy(3, TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(10)); await retry.ProcessAsync(() => { count++; throw new ApplicationException(); }, typeof(ApplicationException)); Assert.AreEqual(4, count); Assert.IsTrue(DateTime.Now - now >= TimeSpan.FromMilliseconds(90)); var isSucc = false; try { await retry.ProcessAsync(() => { count++; throw new InvalidOperationException(); }, typeof(ApplicationException)); } catch (InvalidOperationException) { isSucc = true; } Assert.IsTrue(isSucc); Assert.AreEqual(5, count); var h = new ExceptionHandler(); h.Add <ApplicationException>(ex => { return(null); }); h.Add <ArgumentException>(ex => { if (ex.Message == "name") { return(null); } return(new ApplicationException()); }); await retry.ProcessAsync(() => { count++; throw new ApplicationException(); }, h.GetException); await retry.ProcessAsync(() => { count++; throw new ArgumentException("name"); }, h.GetException); Assert.AreEqual(13, count); isSucc = false; try { await retry.ProcessAsync(() => { count++; throw new ArgumentException("other"); }, h.GetException); } catch (ApplicationException) { isSucc = true; } Assert.AreEqual(14, count); Assert.IsTrue(isSucc); }