public void AutoFlushTargetWrapperAsyncTest1()
        {
            var myTarget = new MyAsyncTarget();
            var wrapper = new AutoFlushTargetWrapper(myTarget);
            myTarget.Initialize(null);
            wrapper.Initialize(null);
            var logEvent = new LogEventInfo();
            Exception lastException = null;
            var continuationHit = new ManualResetEvent(false);
            AsyncContinuation continuation =
                ex =>
                {
                    lastException = ex;
                    continuationHit.Set();
                };

            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

            continuationHit.WaitOne();
            Assert.Null(lastException);
            Assert.Equal(1, myTarget.FlushCount);
            Assert.Equal(1, myTarget.WriteCount);

            continuationHit.Reset();
            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            continuationHit.WaitOne();
            Assert.Null(lastException);
            Assert.Equal(2, myTarget.WriteCount);
            Assert.Equal(2, myTarget.FlushCount);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        public void AutoFlushTargetWrapperAsyncTest1()
        {
            var myTarget = new MyAsyncTarget();
            var wrapper  = new AutoFlushTargetWrapper(myTarget);

            myTarget.Initialize(null);
            wrapper.Initialize(null);
            var               logEvent        = new LogEventInfo();
            Exception         lastException   = null;
            var               continuationHit = new ManualResetEvent(false);
            AsyncContinuation continuation    =
                ex =>
            {
                lastException = ex;
                continuationHit.Set();
            };

            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

            continuationHit.WaitOne();
            Assert.Null(lastException);
            Assert.Equal(1, myTarget.FlushCount);
            Assert.Equal(1, myTarget.WriteCount);

            continuationHit.Reset();
            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            continuationHit.WaitOne();
            Assert.Null(lastException);
            Assert.Equal(2, myTarget.WriteCount);
            Assert.Equal(2, myTarget.FlushCount);
        }
Beispiel #4
0
        public void AutoFlushTargetWrapperAsyncTest2()
        {
            var myTarget = new MyAsyncTarget();
            var wrapper  = new AutoFlushTargetWrapper(myTarget);

            myTarget.Initialize(null);
            wrapper.Initialize(null);
            var       logEvent      = new LogEventInfo();
            Exception lastException = null;

            for (int i = 0; i < 100; ++i)
            {
                wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(ex => lastException = ex));
            }

            var continuationHit            = new ManualResetEvent(false);
            AsyncContinuation continuation =
                ex =>
            {
                continuationHit.Set();
            };

            wrapper.Flush(ex => { });
            Assert.Null(lastException);
            wrapper.Flush(continuation);
            Assert.Null(lastException);
            continuationHit.WaitOne();
            Assert.Null(lastException);
            wrapper.Flush(ex => { });   // Executed right away
            Assert.Null(lastException);
            Assert.Equal(100, myTarget.WriteCount);
            Assert.Equal(103, myTarget.FlushCount);
        }
        public void FilteringTargetWrapperAsyncTest2()
        {
            var myMockCondition = new MyMockCondition(false);
            var myTarget = new MyAsyncTarget();
            var wrapper = new FilteringTargetWrapper(myTarget, myMockCondition);
            wrapper.Initialize(CommonCfg);
            var logEvent = new LogEventInfo();
            Exception lastException = null;
            var continuationHit = new ManualResetEvent(false);
            Action<Exception> continuation =
                ex =>
                {
                    lastException = ex;
                    continuationHit.Set();
                };

            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

            continuationHit.WaitOne();
            Assert.IsNull(lastException);
            Assert.AreEqual(0, myTarget.WriteCount);
            Assert.AreEqual(1, myMockCondition.CallCount);

            continuationHit.Reset();
            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            continuationHit.WaitOne();
            Assert.IsNull(lastException);
            Assert.AreEqual(0, myTarget.WriteCount);
            Assert.AreEqual(2, myMockCondition.CallCount);
        }
Beispiel #6
0
        public void AsyncTargetWrapperCloseTest()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,
            };

            var targetWrapper = new AsyncTargetWrapper(myTarget)
            {
                OverflowAction            = AsyncTargetWrapperOverflowAction.Grow,
                TimeToSleepBetweenBatches = 1000,
            };

            targetWrapper.Initialize(null);
            myTarget.Initialize(null);

            bool continuationHit = false;

            targetWrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(ex => { continuationHit = true; }));

            // quickly close the target before the timer elapses
            targetWrapper.Close();

            // continuation will not be hit because the thread is down.
            Thread.Sleep(1000);
            Assert.IsFalse(continuationHit);
        }
Beispiel #7
0
        public void FilteringTargetWrapperAsyncTest1()
        {
            var myMockCondition = new MyMockCondition(true);
            var myTarget        = new MyAsyncTarget();
            var wrapper         = new FilteringTargetWrapper(myTarget, myMockCondition);

            myTarget.Initialize(null);
            wrapper.Initialize(null);
            var               logEvent        = new LogEventInfo();
            Exception         lastException   = null;
            var               continuationHit = new ManualResetEvent(false);
            AsyncContinuation continuation    =
                ex =>
            {
                lastException = ex;
                continuationHit.Set();
            };

            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

            continuationHit.WaitOne();
            Assert.IsNull(lastException);
            Assert.AreEqual(1, myTarget.WriteCount);
            Assert.AreEqual(1, myMockCondition.CallCount);

            continuationHit.Reset();
            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            continuationHit.WaitOne();
            Assert.IsNull(lastException);
            Assert.AreEqual(2, myTarget.WriteCount);
            Assert.AreEqual(2, myMockCondition.CallCount);
        }
        public void AsyncTargetWrapperAsyncWithExceptionTest1()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,
            };

            var targetWrapper = new AsyncTargetWrapper(myTarget)
            {
                Name = "AsyncTargetWrapperAsyncWithExceptionTest1_Wrapper"
            };

            targetWrapper.Initialize(null);
            myTarget.Initialize(null);
            try
            {
                var               logEvent        = new LogEventInfo();
                Exception         lastException   = null;
                var               continuationHit = new ManualResetEvent(false);
                AsyncContinuation continuation    =
                    ex =>
                {
                    lastException = ex;
                    continuationHit.Set();
                };

                targetWrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

                Assert.True(continuationHit.WaitOne(5000));
                Assert.NotNull(lastException);
                Assert.IsType <InvalidOperationException>(lastException);

                // no flush on exception
                Assert.Equal(0, myTarget.FlushCount);
                Assert.Equal(1, myTarget.WriteCount);

                continuationHit.Reset();
                lastException = null;
                targetWrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
                Assert.True(continuationHit.WaitOne(5000));
                Assert.NotNull(lastException);
                Assert.IsType <InvalidOperationException>(lastException);
                Assert.Equal(0, myTarget.FlushCount);
                Assert.Equal(2, myTarget.WriteCount);
            }
            finally
            {
                myTarget.Close();
                targetWrapper.Close();
            }
        }
        public void AsyncTargetWrapperCloseTest()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,
            };

            var targetWrapper = new AsyncTargetWrapper(myTarget)
            {
                OverflowAction            = AsyncTargetWrapperOverflowAction.Grow,
                TimeToSleepBetweenBatches = 1000,
            };

            targetWrapper.Initialize(null);
            myTarget.Initialize(null);

            targetWrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(ex => { }));

            // quickly close the target before the timer elapses
            targetWrapper.Close();
        }
        public void AutoFlushTargetWrapperAsyncWithExceptionTest1()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,
            };

            var wrapper = new AutoFlushTargetWrapper(myTarget);

            myTarget.Initialize(null);
            wrapper.Initialize(null);
            var               logEvent        = new LogEventInfo();
            Exception         lastException   = null;
            var               continuationHit = new ManualResetEvent(false);
            AsyncContinuation continuation    =
                ex =>
            {
                lastException = ex;
                continuationHit.Set();
            };

            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

            continuationHit.WaitOne();
            Assert.IsNotNull(lastException);
            Assert.IsInstanceOfType(typeof(InvalidOperationException), lastException);

            // no flush on exception
            Assert.AreEqual(0, myTarget.FlushCount);
            Assert.AreEqual(1, myTarget.WriteCount);

            continuationHit.Reset();
            lastException = null;
            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            continuationHit.WaitOne();
            Assert.IsNotNull(lastException);
            Assert.IsInstanceOfType(typeof(InvalidOperationException), lastException);
            Assert.AreEqual(0, myTarget.FlushCount);
            Assert.AreEqual(2, myTarget.WriteCount);
        }
Beispiel #11
0
        public void AsyncTargetWrapperAsyncTest1()
        {
            var myTarget      = new MyAsyncTarget();
            var targetWrapper = new AsyncTargetWrapper(myTarget)
            {
                Name = "AsyncTargetWrapperAsyncTest1_Wrapper"
            };

            targetWrapper.Initialize(null);
            myTarget.Initialize(null);
            try
            {
                var               logEvent        = new LogEventInfo();
                Exception         lastException   = null;
                var               continuationHit = new ManualResetEvent(false);
                AsyncContinuation continuation    =
                    ex =>
                {
                    lastException = ex;
                    continuationHit.Set();
                };

                targetWrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

                Assert.True(continuationHit.WaitOne(5000));
                Assert.Null(lastException);
                Assert.Equal(1, myTarget.WriteCount);

                continuationHit.Reset();
                targetWrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
                Assert.True(continuationHit.WaitOne(5000));
                Assert.Null(lastException);
                Assert.Equal(2, myTarget.WriteCount);
            }
            finally
            {
                myTarget.Close();
                targetWrapper.Close();
            }
        }
Beispiel #12
0
        public void AsyncTargetWrapperAsyncWithExceptionTest1()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,

            };

            var targetWrapper = new AsyncTargetWrapper(myTarget) {Name = "AsyncTargetWrapperAsyncWithExceptionTest1_Wrapper"};
            targetWrapper.Initialize(null);
            myTarget.Initialize(null);
            var logEvent = new LogEventInfo();
            Exception lastException = null;
            var continuationHit = new ManualResetEvent(false);
            AsyncContinuation continuation =
                ex =>
                {
                    lastException = ex;
                    continuationHit.Set();
                };

            targetWrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

            Assert.True(continuationHit.WaitOne());
            Assert.NotNull(lastException);
            Assert.IsType(typeof(InvalidOperationException), lastException);

            // no flush on exception
            Assert.Equal(0, myTarget.FlushCount);
            Assert.Equal(1, myTarget.WriteCount);

            continuationHit.Reset();
            lastException = null;
            targetWrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            continuationHit.WaitOne();
            Assert.NotNull(lastException);
            Assert.IsType(typeof(InvalidOperationException), lastException);
            Assert.Equal(0, myTarget.FlushCount);
            Assert.Equal(2, myTarget.WriteCount);
        }
Beispiel #13
0
        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 BufferingTargetWrapperAsyncTest1()
        {
            RetryingIntegrationTest(3, () =>
            {
                var myTarget = new MyAsyncTarget();
                var targetWrapper = new BufferingTargetWrapper
                {
                    WrappedTarget = myTarget,
                    BufferSize = 10,
                };

                InitializeTargets(myTarget, targetWrapper);

                const int totalEvents = 100;

                var continuationHit = new bool[totalEvents];
                var lastException = new Exception[totalEvents];
                var continuationThread = new Thread[totalEvents];
                var hitCount = 0;

                CreateContinuationFunc createAsyncContinuation =
                    eventNumber =>
                        ex =>
                        {
                            lastException[eventNumber] = ex;
                            continuationThread[eventNumber] = Thread.CurrentThread;
                            continuationHit[eventNumber] = true;
                            Interlocked.Increment(ref hitCount);
                        };

                // write 9 events - they will all be buffered and no final continuation will be reached
                var eventCounter = 0;
                for (var i = 0; i < 9; ++i)
                {
                    targetWrapper.WriteAsyncLogEvent(
                        new LogEventInfo().WithContinuation(createAsyncContinuation(eventCounter++)));
                }

                Assert.Equal(0, hitCount);

                // write one more event - everything will be flushed
                targetWrapper.WriteAsyncLogEvent(
                    new LogEventInfo().WithContinuation(createAsyncContinuation(eventCounter++)));

                while (hitCount < 10)
                {
                    Thread.Sleep(10);
                }

                Assert.Equal(10, hitCount);
                Assert.Equal(1, myTarget.BufferedWriteCount);
                Assert.Equal(10, myTarget.BufferedTotalEvents);
                for (var i = 0; i < hitCount; ++i)
                {
                    Assert.NotSame(Thread.CurrentThread, continuationThread[i]);
                    Assert.Null(lastException[i]);
                }

                // write 9 more events - they will all be buffered and no final continuation will be reached
                for (var i = 0; i < 9; ++i)
                {
                    targetWrapper.WriteAsyncLogEvent(
                        new LogEventInfo().WithContinuation(createAsyncContinuation(eventCounter++)));
                }

                // no change
                Assert.Equal(10, hitCount);
                Assert.Equal(1, myTarget.BufferedWriteCount);
                Assert.Equal(10, myTarget.BufferedTotalEvents);

                Exception flushException = null;
                var flushHit = new ManualResetEvent(false);

                targetWrapper.Flush(
                    ex =>
                    {
                        flushException = ex;
                        flushHit.Set();
                    });

                flushHit.WaitOne();
                Assert.Null(flushException);

                // make sure remaining events were written
                Assert.Equal(19, hitCount);
                Assert.Equal(2, myTarget.BufferedWriteCount);
                Assert.Equal(19, myTarget.BufferedTotalEvents);

                // flushes happen on another thread
                for (var i = 10; i < hitCount; ++i)
                {
                    Assert.NotNull(continuationThread[i]);
                    Assert.NotSame(Thread.CurrentThread, continuationThread[i]);
                    Assert.Null(lastException[i]);
                }

                // flush again - should not do anything
                flushHit.Reset();
                targetWrapper.Flush(
                    ex =>
                    {
                        flushException = ex;
                        flushHit.Set();
                    });

                flushHit.WaitOne();
                Assert.Equal(19, hitCount);
                Assert.Equal(2, myTarget.BufferedWriteCount);
                Assert.Equal(19, myTarget.BufferedTotalEvents);

                targetWrapper.Close();
                myTarget.Close();

            });
        }
Beispiel #15
0
        public void AsyncTargetWrapperFlushTest()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true
            };

            var targetWrapper = new AsyncTargetWrapper(myTarget)
            {
                Name           = "AsyncTargetWrapperFlushTest_Wrapper",
                OverflowAction = AsyncTargetWrapperOverflowAction.Grow
            };

            targetWrapper.Initialize(null);
            myTarget.Initialize(null);

            try
            {
                List <Exception> exceptions = new List <Exception>();

                int eventCount = 5000;

                for (int i = 0; i < eventCount; ++i)
                {
                    targetWrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(
                                                         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.Equal(eventCount, exceptions.Count);

                            // with just 1 flush of the target
                            Assert.Equal(1, myTarget.FlushCount);

                            // and all writes should be accounted for
                            Assert.Equal(eventCount, myTarget.WriteCount);
                        }
                        catch (Exception ex)
                        {
                            lastException = ex;
                        }
                        finally
                        {
                            mre.Set();
                        }
                    });
                    Assert.True(mre.WaitOne(5000), InternalLogger.LogWriter?.ToString() ?? string.Empty);
                },
                    LogLevel.Trace);

                if (lastException != null)
                {
                    Assert.True(false, lastException.ToString() + "\r\n" + internalLog);
                }
            }
            finally
            {
                myTarget.Close();
                targetWrapper.Close();
            }
        }
        public void AsyncTargetWrapperFlushTest()
        {
            InternalLogger.LogToConsole     = true;
            InternalLogger.IncludeTimestamp = true;
            InternalLogger.LogLevel         = LogLevel.Trace;
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true
            };


            var targetWrapper = new AsyncTargetWrapper(myTarget)
            {
                Name                      = "AsyncTargetWrapperFlushTest_Wrapper",
                OverflowAction            = AsyncTargetWrapperOverflowAction.Grow,
                TimeToSleepBetweenBatches = 3
            };

            targetWrapper.Initialize(null);
            myTarget.Initialize(null);

            try
            {
                List <Exception> exceptions = new List <Exception>();
#if !SILVERLIGHT
                int  eventCount    = Environment.Is64BitProcess ? 5000 : 500;
                long missingEvents = Environment.Is64BitProcess ? 5000 : 500;
#else
                int  eventCount    = 500;
                long missingEvents = 500;
#endif
                for (int i = 0; i < eventCount; ++i)
                {
                    targetWrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(
                                                         ex =>
                    {
                        try
                        {
                            lock (exceptions)
                            {
                                exceptions.Add(ex);
                            }
                            Interlocked.Decrement(ref missingEvents);
                        }
                        catch (Exception e)
                        {
                            InternalLogger.Trace("Error in callback", e);
                        }
                    }));
                }

                Exception        lastException = null;
                ManualResetEvent mre           = new ManualResetEvent(false);

                string internalLog = RunAndCaptureInternalLog(
                    () =>
                {
                    targetWrapper.Flush(
                        cont =>
                    {
                        try
                        {
                            DateTime start = DateTime.Now;

                            // We have to spin until all events are done being written by the above code, otherwise on
                            // slow computers the flush will be called before all events has been pushed to the event queue.
                            // causing the below assertions to fail.
                            if (missingEvents > 0)
                            {
                                InternalLogger.Trace("Still missing {0} events, exceptions captured:{1}", missingEvents, exceptions.Count);
                            }
                            while (missingEvents > 0)
                            {
                                InternalLogger.Trace("Still missing {0} events, exceptions captured:{1}", missingEvents, exceptions.Count);
                                Thread.Sleep(50);
                                if (DateTime.Now - start > TimeSpan.FromSeconds(20))
                                {
                                    Assert.False(true, string.Format("threads did not manage to enqueue their messages within time limit, still missing:{0}events, exceptions captured:{1}", missingEvents, exceptions.Count));
                                }
                            }

                            // by this time all continuations should be completed
                            Assert.Equal(eventCount, exceptions.Count);

                            // We have to use interlocked, otherwise there are no guarantee that we get the correct value
                            // with  just 1 flush of the target
                            int flushCount = Interlocked.CompareExchange(ref myTarget.FlushCount, 0, 1);

                            Assert.Equal(1, flushCount);

                            int writeCount = Interlocked.CompareExchange(ref myTarget.WriteCount, 0, eventCount);
                            // and all writes should be accounted for
                            Assert.Equal(eventCount, writeCount);
                        }
                        catch (Exception ex)
                        {
                            lastException = ex;
                        }
                        finally
                        {
                            mre.Set();
                        }
                    });
                    Assert.True(mre.WaitOne());
                },
                    LogLevel.Trace);

                if (lastException != null)
                {
                    Assert.True(false, lastException.ToString() + "\r\n" + internalLog);
                }
            }
            finally
            {
                myTarget.Close();
                targetWrapper.Close();
            }
        }
        public void AsyncTargetWrapperCloseTest()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,

            };

            var targetWrapper = new AsyncTargetWrapper(myTarget)
            {
                OverflowAction = AsyncTargetWrapperOverflowAction.Grow,
                TimeToSleepBetweenBatches = 1000,
            };

            targetWrapper.Initialize(null);
            myTarget.Initialize(null);

            targetWrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(ex => { }));

            // quickly close the target before the timer elapses
            targetWrapper.Close();
        }
        public void AsyncTargetWrapperFlushTest()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,
               
            };

            var targetWrapper = new AsyncTargetWrapper(myTarget)
            {
                OverflowAction = AsyncTargetWrapperOverflowAction.Grow,
            };

            targetWrapper.Initialize(null);
            myTarget.Initialize(null);

            List<Exception> exceptions = new List<Exception>();

            int eventCount = 5000;

            for (int i = 0; i < eventCount; ++i)
            {
                targetWrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(
                    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.Equal(eventCount, exceptions.Count);

                                // with just 1 flush of the target
                                Assert.Equal(1, myTarget.FlushCount);

                                // and all writes should be accounted for
                                Assert.Equal(eventCount, myTarget.WriteCount);
                            }
                            catch (Exception ex)
                            {
                                lastException = ex;
                            }
                            finally
                            {
                                mre.Set();
                            }
                        });
                    Assert.True(mre.WaitOne());
                },
                LogLevel.Trace);

            if (lastException != null)
            {
                Assert.True(false, lastException.ToString() + "\r\n" + internalLog);
            }
        }
        public void FilteringTargetWrapperAsyncTest2()
        {
            var myMockCondition = new MyMockCondition(false);
            var myTarget = new MyAsyncTarget();
            var wrapper = new FilteringTargetWrapper(myTarget, myMockCondition);
            ((ISupportsInitialize)myTarget).Initialize();
            ((ISupportsInitialize)wrapper).Initialize();
            var logEvent = new LogEventInfo();
            Exception lastException = null;
            var continuationHit = new ManualResetEvent(false);
            AsyncContinuation continuation =
                ex =>
                {
                    lastException = ex;
                    continuationHit.Set();
                };

            wrapper.WriteLogEvent(logEvent, continuation);

            continuationHit.WaitOne();
            Assert.IsNull(lastException);
            Assert.AreEqual(0, myTarget.WriteCount);
            Assert.AreEqual(1, myMockCondition.CallCount);

            continuationHit.Reset();
            wrapper.WriteLogEvent(logEvent, continuation);
            continuationHit.WaitOne();
            Assert.IsNull(lastException);
            Assert.AreEqual(0, myTarget.WriteCount);
            Assert.AreEqual(2, myMockCondition.CallCount);
        }
        public void FilteringTargetWrapperAsyncWithExceptionTest1()
        {
            var myMockCondition = new MyMockCondition(true);
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,
            };

            var wrapper = new FilteringTargetWrapper(myTarget, myMockCondition);
            myTarget.Initialize(null);
            wrapper.Initialize(null);
            var logEvent = new LogEventInfo();
            Exception lastException = null;
            var continuationHit = new ManualResetEvent(false);
            AsyncContinuation continuation =
                ex =>
                {
                    lastException = ex;
                    continuationHit.Set();
                };

            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

            continuationHit.WaitOne();
            Assert.IsNotNull(lastException);
            Assert.IsInstanceOfType(typeof(InvalidOperationException), lastException);

            Assert.AreEqual(1, myTarget.WriteCount);
            Assert.AreEqual(1, myMockCondition.CallCount);

            continuationHit.Reset();
            lastException = null;
            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            continuationHit.WaitOne();
            Assert.IsNotNull(lastException);
            Assert.IsInstanceOfType(typeof(InvalidOperationException), lastException);
            Assert.AreEqual(2, myTarget.WriteCount);
            Assert.AreEqual(2, myMockCondition.CallCount);
        }
        public void AsyncTargetWrapperAsyncTest1()
        {
            var myTarget = new MyAsyncTarget();
            var targetWrapper = new AsyncTargetWrapper(myTarget) { Name = "AsyncTargetWrapperAsyncTest1_Wrapper" };
            targetWrapper.Initialize(null);
            myTarget.Initialize(null);
            try
            {
                var logEvent = new LogEventInfo();
                Exception lastException = null;
                var continuationHit = new ManualResetEvent(false);
                AsyncContinuation continuation =
                    ex =>
                    {
                        lastException = ex;
                        continuationHit.Set();
                    };

                targetWrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

                Assert.True(continuationHit.WaitOne());
                Assert.Null(lastException);
                Assert.Equal(1, myTarget.WriteCount);

                continuationHit.Reset();
                targetWrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
                continuationHit.WaitOne();
                Assert.Null(lastException);
                Assert.Equal(2, myTarget.WriteCount);
            }
            finally
            {
                myTarget.Close();
                targetWrapper.Close();
            }
        }
Beispiel #22
0
        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);
        }
        public void BufferingTargetWrapperAsyncTest1()
        {
            var myTarget = new MyAsyncTarget();
            var targetWrapper = new BufferingTargetWrapper
            {
                WrappedTarget = myTarget,
                BufferSize = 10,
            };

            ((ISupportsInitialize)myTarget).Initialize();
            ((ISupportsInitialize)targetWrapper).Initialize();

            int totalEvents = 100;

            var continuationHit = new bool[totalEvents];
            var lastException = new Exception[totalEvents];
            var continuationThread = new Thread[totalEvents];
            int hitCount = 0;

            CreateContinuationFunc createAsyncContinuation =
                eventNumber =>
                    ex =>
                    {
                        lastException[eventNumber] = ex;
                        continuationThread[eventNumber] = Thread.CurrentThread;
                        continuationHit[eventNumber] = true;
                        Interlocked.Increment(ref hitCount);
                    };

            // write 9 events - they will all be buffered and no final continuation will be reached
            int eventCounter = 0;
            for (int i = 0; i < 9; ++i)
            {
                targetWrapper.WriteLogEvent(new LogEventInfo(), createAsyncContinuation(eventCounter++));
            }

            Assert.AreEqual(0, hitCount);

            // write one more event - everything will be flushed
            targetWrapper.WriteLogEvent(new LogEventInfo(), createAsyncContinuation(eventCounter++));

            while (hitCount < 10)
            {
                Thread.Sleep(10);
            }

            Assert.AreEqual(10, hitCount);
            Assert.AreEqual(1, myTarget.BufferedWriteCount);
            Assert.AreEqual(10, myTarget.BufferedTotalEvents);
            for (int i = 0; i < hitCount; ++i)
            {
                Assert.AreNotSame(Thread.CurrentThread, continuationThread[i]);
                Assert.IsNull(lastException[i]);
            }

            // write 9 more events - they will all be buffered and no final continuation will be reached
            for (int i = 0; i < 9; ++i)
            {
                targetWrapper.WriteLogEvent(new LogEventInfo(), createAsyncContinuation(eventCounter++));
            }

            // no change
            Assert.AreEqual(10, hitCount);
            Assert.AreEqual(1, myTarget.BufferedWriteCount);
            Assert.AreEqual(10, myTarget.BufferedTotalEvents);

            Exception flushException = null;
            var flushHit = new ManualResetEvent(false);

            targetWrapper.Flush(
                ex =>
                {
                    flushException = ex;
                    flushHit.Set();
                });

            Thread.Sleep(1000);

            flushHit.WaitOne();
            Assert.IsNull(flushException);

            // make sure remaining events were written
            Assert.AreEqual(19, hitCount);
            Assert.AreEqual(2, myTarget.BufferedWriteCount);
            Assert.AreEqual(19, myTarget.BufferedTotalEvents);

            // flushes happen on another thread
            for (int i = 10; i < hitCount; ++i)
            {
                Assert.IsNotNull(continuationThread[i]);
                Assert.AreNotSame(Thread.CurrentThread, continuationThread[i], "Invalid thread #" + i);
                Assert.IsNull(lastException[i]);
            }

            // flush again - should not do anything
            flushHit.Reset();
            targetWrapper.Flush(
                ex =>
                {
                    flushException = ex;
                    flushHit.Set();
                });

            flushHit.WaitOne();
            Assert.AreEqual(19, hitCount);
            Assert.AreEqual(2, myTarget.BufferedWriteCount);
            Assert.AreEqual(19, myTarget.BufferedTotalEvents);

            ((ISupportsInitialize)targetWrapper).Close();
            ((ISupportsInitialize)myTarget).Close();
        }