示例#1
0
        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();
            }
        }
示例#2
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();
            }
        }
        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();

            });
        }
示例#4
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();
            }
        }
示例#5
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());
                    },
                    LogLevel.Trace);

                if (lastException != null)
                {
                    Assert.True(false, lastException.ToString() + "\r\n" + internalLog);
                }
            }
            finally
            {
                myTarget.Close();
                targetWrapper.Close();
            }
        }
示例#6
0
        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());
                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);
            }
            finally
            {
                myTarget.Close();
                targetWrapper.Close();
            }
        }
示例#7
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());
                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();
            }
        }
        public void BufferingTargetWrapperAsyncTest1()
        {
            var myTarget = new MyAsyncTarget();
            var targetWrapper = new BufferingTargetWrapper
            {
                WrappedTarget = myTarget,
                BufferSize = 10,
            };

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

            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.WriteAsyncLogEvent(new LogEventInfo().WithContinuation(createAsyncContinuation(eventCounter++)));
            }

            Assert.AreEqual(0, hitCount);

            // write one more event - everything will be flushed
            targetWrapper.WriteAsyncLogEvent(new LogEventInfo().WithContinuation(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.WriteAsyncLogEvent(new LogEventInfo().WithContinuation(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);

            targetWrapper.Close();
            myTarget.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();
            }
        }