public void AsyncTargetWrapperAsyncTest1() { var myTarget = new MyAsyncTarget(); var targetWrapper = new AsyncTargetWrapper(myTarget); ((ISupportsInitialize)targetWrapper).Initialize(); ((ISupportsInitialize)myTarget).Initialize(); var logEvent = new LogEventInfo(); Exception lastException = null; var continuationHit = new ManualResetEvent(false); AsyncContinuation continuation = ex => { lastException = ex; continuationHit.Set(); }; targetWrapper.WriteLogEvent(logEvent, continuation); continuationHit.WaitOne(); Assert.IsNull(lastException); Assert.AreEqual(1, myTarget.WriteCount); continuationHit.Reset(); targetWrapper.WriteLogEvent(logEvent, continuation); continuationHit.WaitOne(); Assert.IsNull(lastException); Assert.AreEqual(2, myTarget.WriteCount); }
public void AsyncTargetWrapperAsyncWithExceptionTest1() { var myTarget = new MyAsyncTarget { ThrowExceptions = true, }; var targetWrapper = new AsyncTargetWrapper(myTarget); ((ISupportsInitialize)targetWrapper).Initialize(); ((ISupportsInitialize)myTarget).Initialize(); var logEvent = new LogEventInfo(); Exception lastException = null; var continuationHit = new ManualResetEvent(false); AsyncContinuation continuation = ex => { lastException = ex; continuationHit.Set(); }; targetWrapper.WriteLogEvent(logEvent, continuation); continuationHit.WaitOne(); Assert.IsNotNull(lastException); Assert.IsInstanceOfType(lastException, typeof(InvalidOperationException)); // no flush on exception Assert.AreEqual(0, myTarget.FlushCount); Assert.AreEqual(1, myTarget.WriteCount); continuationHit.Reset(); lastException = null; targetWrapper.WriteLogEvent(logEvent, continuation); continuationHit.WaitOne(); Assert.IsNotNull(lastException); Assert.IsInstanceOfType(lastException, typeof(InvalidOperationException)); Assert.AreEqual(0, myTarget.FlushCount); Assert.AreEqual(2, myTarget.WriteCount); }
public void AsyncTargetWrapperSyncTest1() { var myTarget = new MyTarget(); var targetWrapper = new AsyncTargetWrapper { WrappedTarget = myTarget, }; ((ISupportsInitialize)targetWrapper).Initialize(); ((ISupportsInitialize)myTarget).Initialize(); var logEvent = new LogEventInfo(); Exception lastException = null; ManualResetEvent continuationHit = new ManualResetEvent(false); Thread continuationThread = null; AsyncContinuation continuation = ex => { lastException = ex; continuationThread = Thread.CurrentThread; continuationHit.Set(); }; targetWrapper.WriteLogEvent(logEvent, continuation); // continuation was not hit continuationHit.WaitOne(); Assert.AreNotSame(continuationThread, Thread.CurrentThread); Assert.IsNull(lastException); Assert.AreEqual(1, myTarget.WriteCount); continuationHit.Reset(); targetWrapper.WriteLogEvent(logEvent, continuation); continuationHit.WaitOne(); Assert.AreNotSame(continuationThread, Thread.CurrentThread); Assert.IsNull(lastException); Assert.AreEqual(2, myTarget.WriteCount); }
public void AsyncTargetWrapperFlushTest() { var myTarget = new MyAsyncTarget { ThrowExceptions = true, }; var targetWrapper = new AsyncTargetWrapper(myTarget) { OverflowAction = AsyncTargetWrapperOverflowAction.Grow, }; ((ISupportsInitialize)targetWrapper).Initialize(); ((ISupportsInitialize)myTarget).Initialize(); List<Exception> exceptions = new List<Exception>(); int eventCount = 5000; for (int i = 0; i < eventCount; ++i) { targetWrapper.WriteLogEvent(LogEventInfo.CreateNullEvent(), ex => { lock (exceptions) { exceptions.Add(ex); } }); } Exception lastException = null; ManualResetEvent mre = new ManualResetEvent(false); string internalLog = RunAndCaptureInternalLog( () => { targetWrapper.Flush( cont => { try { // by this time all continuations should be completed Assert.AreEqual(eventCount, exceptions.Count); // with just 1 flush of the target Assert.AreEqual(1, myTarget.FlushCount); // and all writes should be accounted for Assert.AreEqual(eventCount, myTarget.WriteCount); } catch (Exception ex) { lastException = ex; } finally { mre.Set(); } }); mre.WaitOne(); }, LogLevel.Trace); if (lastException != null) { Assert.Fail(lastException.ToString() + "\r\n" + internalLog); } }
public void AsyncTargetWrapperExceptionTest() { var targetWrapper = new AsyncTargetWrapper { OverflowAction = AsyncTargetWrapperOverflowAction.Grow, TimeToSleepBetweenBatches = 500, }; ((ISupportsInitialize)targetWrapper).Initialize(); // don't set wrapped taret - will cause exception on the timer thread string internalLog = RunAndCaptureInternalLog( () => { targetWrapper.WriteLogEvent(LogEventInfo.CreateNullEvent(), ex => { }); Thread.Sleep(1000); }, LogLevel.Trace); ((ISupportsInitialize)targetWrapper).Close(); Assert.IsTrue(internalLog.StartsWith("Error Error in lazy writer timer procedure: System.NullReferenceException", StringComparison.Ordinal), internalLog); }
public void AsyncTargetWrapperCloseTest() { var myTarget = new MyAsyncTarget { ThrowExceptions = true, }; var targetWrapper = new AsyncTargetWrapper(myTarget) { OverflowAction = AsyncTargetWrapperOverflowAction.Grow, TimeToSleepBetweenBatches = 1000, }; ((ISupportsInitialize)targetWrapper).Initialize(); ((ISupportsInitialize)myTarget).Initialize(); bool continuationHit = false; targetWrapper.WriteLogEvent(LogEventInfo.CreateNullEvent(), ex => { continuationHit = true; }); // quickly close the target before the timer elapses ((ISupportsInitialize)targetWrapper).Close(); // continuation will not be hit because the thread is down. Thread.Sleep(1000); Assert.IsFalse(continuationHit); }