public void AsyncRequestQueueWithDiscardBehaviorTest()
        {
            var ev1 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev2 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev3 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev4 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });

            var queue = new AsyncRequestQueue(3, AsyncTargetWrapperOverflowAction.Discard);

            Assert.Equal(3, queue.RequestLimit);
            Assert.Equal(AsyncTargetWrapperOverflowAction.Discard, queue.OnOverflow);
            Assert.Equal(0, queue.RequestCount);
            queue.Enqueue(ev1);
            Assert.Equal(1, queue.RequestCount);
            queue.Enqueue(ev2);
            Assert.Equal(2, queue.RequestCount);
            queue.Enqueue(ev3);
            Assert.Equal(3, queue.RequestCount);
            queue.Enqueue(ev4);
            Assert.Equal(3, queue.RequestCount);

            AsyncLogEventInfo[] logEventInfos = queue.DequeueBatch(10);
            Assert.Equal(0, queue.RequestCount);

            // ev1 is lost
            Assert.Same(logEventInfos[0].LogEvent, ev2.LogEvent);
            Assert.Same(logEventInfos[1].LogEvent, ev3.LogEvent);
            Assert.Same(logEventInfos[2].LogEvent, ev4.LogEvent);
            Assert.Same(logEventInfos[0].Continuation, ev2.Continuation);
            Assert.Same(logEventInfos[1].Continuation, ev3.Continuation);
            Assert.Same(logEventInfos[2].Continuation, ev4.Continuation);
        }
        public void AsyncRequestQueueClearTest()
        {
            var ev1 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev2 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev3 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev4 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });

            var queue = new AsyncRequestQueue(3, AsyncTargetWrapperOverflowAction.Grow);

            Assert.Equal(3, queue.RequestLimit);
            Assert.Equal(AsyncTargetWrapperOverflowAction.Grow, queue.OnOverflow);
            Assert.Equal(0, queue.RequestCount);
            queue.Enqueue(ev1);
            Assert.Equal(1, queue.RequestCount);
            queue.Enqueue(ev2);
            Assert.Equal(2, queue.RequestCount);
            queue.Enqueue(ev3);
            Assert.Equal(3, queue.RequestCount);
            queue.Enqueue(ev4);
            Assert.Equal(4, queue.RequestCount);
            queue.Clear();
            Assert.Equal(0, queue.RequestCount);

            AsyncLogEventInfo[] logEventInfos;

            logEventInfos = queue.DequeueBatch(10);
            int result = logEventInfos.Length;

            Assert.Equal(0, result);
            Assert.Equal(0, queue.RequestCount);
        }
Ejemplo n.º 3
0
        public void AsyncRequestQueueClearTest()
        {
            var ev1 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev2 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev3 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev4 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });

            var queue = new AsyncRequestQueue(new NLog.Config.LoggingConfiguration(), 3, AsyncTargetWrapperOverflowAction.Grow);

            Assert.Equal(3, queue.RequestLimit);
            Assert.Equal(AsyncTargetWrapperOverflowAction.Grow, queue.OnOverflow);
            Assert.Equal(0, queue.RequestCount);
            queue.Enqueue(ev1);
            Assert.Equal(1, queue.RequestCount);
            queue.Enqueue(ev2);
            Assert.Equal(2, queue.RequestCount);
            queue.Enqueue(ev3);
            Assert.Equal(3, queue.RequestCount);
            queue.Enqueue(ev4);
            Assert.Equal(4, queue.RequestCount);
            queue.Clear();
            Assert.Equal(0, queue.RequestCount);

            AsyncLogEventInfo[] logEventInfos;
            int actualCount;

            logEventInfos = queue.DequeueBatch(10, out actualCount);


            Assert.Equal(0, actualCount);
            Assert.Equal(0, queue.RequestCount);
        }
Ejemplo n.º 4
0
        public void AsyncRequestQueueWithGrowBehaviorTest()
        {
            var ev1 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev2 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev3 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev4 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });

            var queue = new AsyncRequestQueue(new NLog.Config.LoggingConfiguration(), 3, AsyncTargetWrapperOverflowAction.Grow);

            Assert.Equal(3, queue.RequestLimit);
            Assert.Equal(AsyncTargetWrapperOverflowAction.Grow, queue.OnOverflow);
            Assert.Equal(0, queue.RequestCount);
            queue.Enqueue(ev1);
            Assert.Equal(1, queue.RequestCount);
            queue.Enqueue(ev2);
            Assert.Equal(2, queue.RequestCount);
            queue.Enqueue(ev3);
            Assert.Equal(3, queue.RequestCount);
            queue.Enqueue(ev4);
            Assert.Equal(4, queue.RequestCount);
            int actualCount;

            AsyncLogEventInfo[] logEventInfos = queue.DequeueBatch(10, out actualCount);


            Assert.Equal(4, actualCount);
            Assert.Equal(0, queue.RequestCount);

            // ev1 is lost
            Assert.Same(logEventInfos[0].LogEvent, ev1.LogEvent);
            Assert.Same(logEventInfos[1].LogEvent, ev2.LogEvent);
            Assert.Same(logEventInfos[2].LogEvent, ev3.LogEvent);
            Assert.Same(logEventInfos[3].LogEvent, ev4.LogEvent);
            Assert.Same(logEventInfos[0].Continuation, ev1.Continuation);
            Assert.Same(logEventInfos[1].Continuation, ev2.Continuation);
            Assert.Same(logEventInfos[2].Continuation, ev3.Continuation);
            Assert.Same(logEventInfos[3].Continuation, ev4.Continuation);
        }
Ejemplo n.º 5
0
        public void AsyncRequestQueueEventDiscardedEventTest()
        {
            var ev1 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev2 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev3 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
            var ev4 = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });

            var queue  = new AsyncRequestQueue(3, AsyncTargetWrapperOverflowAction.Discard);
            var raised = false;
            var eh     = new BufferOverflowEventHandler(
                (a, b, c, d) =>
            {
                Assert.Same(a, AsyncTargetWrapperOverflowAction.Discard);
                Assert.Same(ev1, d);
                raised = true;
            });

            queue.Enqueue(ev1);
            queue.Enqueue(ev2);
            queue.Enqueue(ev3);
            Assert.False(raised);
            queue.Enqueue(ev4);
            Assert.True(raised);
        }
        public void RaiseEventLogEventDropped_OnLogItems()
        {
            const int RequestsLimit       = 2;
            const int EventsCount         = 5;
            int       discardedItemsCount = 0;

            int ExpectedDiscardedItemsCount = EventsCount - RequestsLimit;
            AsyncRequestQueue requestQueue  = new AsyncRequestQueue(RequestsLimit, AsyncTargetWrapperOverflowAction.Discard);

            requestQueue.LogEventDropped += (o, e) => { discardedItemsCount++; };

            for (int i = 0; i < EventsCount; i++)
            {
                requestQueue.Enqueue(new AsyncLogEventInfo());
            }

            Assert.Equal(ExpectedDiscardedItemsCount, discardedItemsCount);
        }
        public void RaiseEventLogEventQueueGrow_OnLogItems()
        {
            const int RequestsLimit = 2;
            const int EventsCount   = 5;
            const int ExpectedCountOfGrovingTimes = 2;
            const int ExpectedFinalSize           = 8;
            int       grovingItemsCount           = 0;

            AsyncRequestQueue requestQueue = new AsyncRequestQueue(RequestsLimit, AsyncTargetWrapperOverflowAction.Grow);

            requestQueue.LogEventQueueGrow += (o, e) => { grovingItemsCount++; };

            for (int i = 0; i < EventsCount; i++)
            {
                requestQueue.Enqueue(new AsyncLogEventInfo());
            }

            Assert.Equal(ExpectedCountOfGrovingTimes, grovingItemsCount);
            Assert.Equal(ExpectedFinalSize, requestQueue.RequestLimit);
        }
        public void AsyncRequestQueueWithBlockBehavior()
        {
            var queue = new AsyncRequestQueue(10, AsyncTargetWrapperOverflowAction.Block);

            ManualResetEvent producerFinished = new ManualResetEvent(false);

            int pushingEvent = 0;

            ThreadPool.QueueUserWorkItem(
                s =>
            {
                // producer thread
                for (int i = 0; i < 1000; ++i)
                {
                    AsyncLogEventInfo logEvent = LogEventInfo.CreateNullEvent().WithContinuation(ex => { });
                    logEvent.LogEvent.Message  = "msg" + i;

                    // Console.WriteLine("Pushing event {0}", i);
                    pushingEvent = i;
                    queue.Enqueue(logEvent);
                }

                producerFinished.Set();
            });

            // consumer thread
            AsyncLogEventInfo[] logEventInfos;
            int total = 0;

            while (total < 500)
            {
                int left = 500 - total;

                logEventInfos = queue.DequeueBatch(left);
                int got = logEventInfos.Length;
                Assert.True(got <= queue.RequestLimit);
                total += got;
            }

            Thread.Sleep(500);

            // producer is blocked on trying to push event #510
            Assert.Equal(510, pushingEvent);
            queue.DequeueBatch(1);
            total++;
            Thread.Sleep(500);

            // producer is now blocked on trying to push event #511

            Assert.Equal(511, pushingEvent);
            while (total < 1000)
            {
                int left = 1000 - total;

                logEventInfos = queue.DequeueBatch(left);
                int got = logEventInfos.Length;
                Assert.True(got <= queue.RequestLimit);
                total += got;
            }

            // producer should now finish
            producerFinished.WaitOne();
        }