public void RetryingTargetWrapperTest1() { var target = new MyTarget(); var wrapper = new RetryingTargetWrapper() { WrappedTarget = target, RetryCount = 10, RetryDelayMilliseconds = 1, }; ((ISupportsInitialize)wrapper).Initialize(); ((ISupportsInitialize)target).Initialize(); var events = new LogEventInfo[] { new LogEventInfo(LogLevel.Debug, "Logger1", "Hello"), new LogEventInfo(LogLevel.Info, "Logger1", "Hello"), new LogEventInfo(LogLevel.Info, "Logger2", "Hello"), }; var exceptions = new List<Exception>(); var continuations = new AsyncContinuation[events.Length]; for (int i = 0; i < continuations.Length; ++i) { continuations[i] = exceptions.Add; } wrapper.WriteLogEvents(events, continuations); // make sure all events went through Assert.AreEqual(3, target.Events.Count); Assert.AreSame(events[0], target.Events[0]); Assert.AreSame(events[1], target.Events[1]); Assert.AreSame(events[2], target.Events[2]); Assert.AreEqual(continuations.Length, exceptions.Count, "Some continuations were not invoked."); // make sure there were no exception foreach (var ex in exceptions) { Assert.IsNull(ex); } }
public void RetryingTargetWrapperTest2() { var target = new MyTarget() { ThrowExceptions = 6, }; var wrapper = new RetryingTargetWrapper() { WrappedTarget = target, RetryCount = 4, RetryDelayMilliseconds = 1, }; ((ISupportsInitialize)wrapper).Initialize(); ((ISupportsInitialize)target).Initialize(); var events = new LogEventInfo[] { new LogEventInfo(LogLevel.Debug, "Logger1", "Hello"), new LogEventInfo(LogLevel.Info, "Logger1", "Hello"), new LogEventInfo(LogLevel.Info, "Logger2", "Hello"), }; var exceptions = new List<Exception>(); var continuations = new AsyncContinuation[events.Length]; for (int i = 0; i < continuations.Length; ++i) { continuations[i] = exceptions.Add; } var internalLogOutput = RunAndCaptureInternalLog(() => wrapper.WriteLogEvents(events, continuations), LogLevel.Trace); string expectedLogOutput = @"Warn Error while writing to 'MyTarget': System.InvalidOperationException: Some exception has ocurred.. Try 1/4 Warn Error while writing to 'MyTarget': System.InvalidOperationException: Some exception has ocurred.. Try 2/4 Warn Error while writing to 'MyTarget': System.InvalidOperationException: Some exception has ocurred.. Try 3/4 Warn Error while writing to 'MyTarget': System.InvalidOperationException: Some exception has ocurred.. Try 4/4 Warn Too many retries. Aborting. Warn Error while writing to 'MyTarget': System.InvalidOperationException: Some exception has ocurred.. Try 1/4 Warn Error while writing to 'MyTarget': System.InvalidOperationException: Some exception has ocurred.. Try 2/4 "; Assert.AreEqual(expectedLogOutput, internalLogOutput); // first event does not get to wrapped target because of too many attempts. // second event gets there in 3rd retry // and third event gets there immediately Assert.AreEqual(2, target.Events.Count); Assert.AreSame(events[1], target.Events[0]); Assert.AreSame(events[2], target.Events[1]); Assert.AreEqual(continuations.Length, exceptions.Count, "Some continuations were not invoked."); Assert.IsNotNull(exceptions[0]); Assert.AreEqual("Some exception has ocurred.", exceptions[0].Message); Assert.IsNull(exceptions[1]); Assert.IsNull(exceptions[2]); }