Ejemplo n.º 1
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(3, AsyncTargetWrapperOverflowAction.Grow);
            Assert.AreEqual(3, queue.RequestLimit);
            Assert.AreEqual(AsyncTargetWrapperOverflowAction.Grow, queue.OnOverflow);
            Assert.AreEqual(0, queue.RequestCount);
            queue.Enqueue(ev1);
            Assert.AreEqual(1, queue.RequestCount);
            queue.Enqueue(ev2);
            Assert.AreEqual(2, queue.RequestCount);
            queue.Enqueue(ev3);
            Assert.AreEqual(3, queue.RequestCount);
            queue.Enqueue(ev4);
            Assert.AreEqual(4, queue.RequestCount);
            queue.Clear();
            Assert.AreEqual(0, queue.RequestCount);

            AsyncLogEventInfo[] logEventInfos;

            logEventInfos = queue.DequeueBatch(10);
            int result = logEventInfos.Length;
            Assert.AreEqual(0, result);
            Assert.AreEqual(0, queue.RequestCount);
        }
Ejemplo n.º 2
0
        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.AreEqual(3, queue.RequestLimit);
            Assert.AreEqual(AsyncTargetWrapperOverflowAction.Discard, queue.OnOverflow);
            Assert.AreEqual(0, queue.RequestCount);
            queue.Enqueue(ev1);
            Assert.AreEqual(1, queue.RequestCount);
            queue.Enqueue(ev2);
            Assert.AreEqual(2, queue.RequestCount);
            queue.Enqueue(ev3);
            Assert.AreEqual(3, queue.RequestCount);
            queue.Enqueue(ev4);
            Assert.AreEqual(3, queue.RequestCount);

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

            // ev1 is lost
            Assert.AreSame(logEventInfos[0].LogEvent, ev2.LogEvent);
            Assert.AreSame(logEventInfos[1].LogEvent, ev3.LogEvent);
            Assert.AreSame(logEventInfos[2].LogEvent, ev4.LogEvent);
            Assert.AreSame(logEventInfos[0].Continuation, ev2.Continuation);
            Assert.AreSame(logEventInfos[1].Continuation, ev3.Continuation);
            Assert.AreSame(logEventInfos[2].Continuation, ev4.Continuation);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes the target.
 /// </summary>
 protected override void InitializeTarget()
 {
     base.InitializeTarget();
     _buffer = new AsyncRequestQueue(BufferSize, AsyncTargetWrapperOverflowAction.Discard);
     InternalLogger.Trace("{0}: Create Timer", this);
     _flushTimer = new Timer(FlushCallback, null, Timeout.Infinite, Timeout.Infinite);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AsyncTargetWrapper" /> class.
 /// </summary>
 /// <param name="wrappedTarget">The wrapped target.</param>
 /// <param name="queueLimit">Maximum number of requests in the queue.</param>
 /// <param name="overflowAction">The action to be taken when the queue overflows.</param>
 public AsyncTargetWrapper(Target wrappedTarget, int queueLimit, AsyncTargetWrapperOverflowAction overflowAction)
 {
     RequestQueue = new AsyncRequestQueue(10000, AsyncTargetWrapperOverflowAction.Discard);
     TimeToSleepBetweenBatches = 50;
     BatchSize = 200;
     FullBatchSizeWriteLimit = 5;
     WrappedTarget           = wrappedTarget;
     QueueLimit     = queueLimit;
     OverflowAction = overflowAction;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncTargetWrapper" /> class.
        /// </summary>
        /// <param name="wrappedTarget">The wrapped target.</param>
        /// <param name="queueLimit">Maximum number of requests in the queue.</param>
        /// <param name="overflowAction">The action to be taken when the queue overflows.</param>
        public AsyncTargetWrapper(Target wrappedTarget, int queueLimit, AsyncTargetWrapperOverflowAction overflowAction)
        {
#if NETSTANDARD2_0
            // NetStandard20 includes many optimizations for ConcurrentQueue:
            //  - See: https://blogs.msdn.microsoft.com/dotnet/2017/06/07/performance-improvements-in-net-core/
            // Net40 ConcurrencyQueue can seem to leak, because it doesn't clear properly on dequeue
            //  - See: https://blogs.msdn.microsoft.com/pfxteam/2012/05/08/concurrentqueuet-holding-on-to-a-few-dequeued-elements/
            _requestQueue = new ConcurrentRequestQueue(10000, AsyncTargetWrapperOverflowAction.Discard);
#else
            _requestQueue = new AsyncRequestQueue(10000, AsyncTargetWrapperOverflowAction.Discard);
#endif
            TimeToSleepBetweenBatches = 1;
            BatchSize = 200;
            FullBatchSizeWriteLimit = 5;
            WrappedTarget           = wrappedTarget;
            QueueLimit     = queueLimit;
            OverflowAction = overflowAction;
        }
Ejemplo n.º 6
0
        public void AsyncRequestQueueClearTest()
        {
            AsyncContinuation cont1 = ex => { };
            AsyncContinuation cont2 = ex => { };
            AsyncContinuation cont3 = ex => { };
            AsyncContinuation cont4 = ex => { };
            AsyncContinuation cont5 = ex => { };

            var ev1 = LogEventInfo.CreateNullEvent();
            var ev2 = LogEventInfo.CreateNullEvent();
            var ev3 = LogEventInfo.CreateNullEvent();
            var ev4 = LogEventInfo.CreateNullEvent();
            var ev5 = LogEventInfo.CreateNullEvent();

            var queue = new AsyncRequestQueue(3, AsyncTargetWrapperOverflowAction.Grow);
            Assert.AreEqual(3, queue.RequestLimit);
            Assert.AreEqual(AsyncTargetWrapperOverflowAction.Grow, queue.OnOverflow);
            Assert.AreEqual(0, queue.RequestCount);
            queue.Enqueue(ev1, cont1);
            Assert.AreEqual(1, queue.RequestCount);
            queue.Enqueue(ev2, cont2);
            Assert.AreEqual(2, queue.RequestCount);
            queue.Enqueue(ev3, cont3);
            Assert.AreEqual(3, queue.RequestCount);
            queue.Enqueue(ev4, cont4);
            Assert.AreEqual(4, queue.RequestCount);
            queue.Clear();
            Assert.AreEqual(0, queue.RequestCount);

            LogEventInfo[] logEventInfos;
            AsyncContinuation[] asyncContinuations;

            int result = queue.DequeueBatch(10, out logEventInfos, out asyncContinuations);
            Assert.AreEqual(result, logEventInfos.Length);
            Assert.AreEqual(result, asyncContinuations.Length);

            Assert.AreEqual(0, result);
            Assert.AreEqual(0, queue.RequestCount);
        }
Ejemplo n.º 7
0
        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.IsTrue(got <= queue.RequestLimit);
                total += got;
            }

            Thread.Sleep(500);

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

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

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

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

            // producer should now finish
            producerFinished.WaitOne();
        }
Ejemplo n.º 8
0
        public void AsyncRequestQueueWithDiscardBehaviorTest()
        {
            AsyncContinuation cont1 = ex => { };
            AsyncContinuation cont2 = ex => { };
            AsyncContinuation cont3 = ex => { };
            AsyncContinuation cont4 = ex => { };
            AsyncContinuation cont5 = ex => { };

            var ev1 = LogEventInfo.CreateNullEvent();
            var ev2 = LogEventInfo.CreateNullEvent();
            var ev3 = LogEventInfo.CreateNullEvent();
            var ev4 = LogEventInfo.CreateNullEvent();
            var ev5 = LogEventInfo.CreateNullEvent();

            var queue = new AsyncRequestQueue(3, AsyncTargetWrapperOverflowAction.Discard);
            Assert.AreEqual(3, queue.RequestLimit);
            Assert.AreEqual(AsyncTargetWrapperOverflowAction.Discard, queue.OnOverflow);
            Assert.AreEqual(0, queue.RequestCount);
            queue.Enqueue(ev1, cont1);
            Assert.AreEqual(1, queue.RequestCount);
            queue.Enqueue(ev2, cont2);
            Assert.AreEqual(2, queue.RequestCount);
            queue.Enqueue(ev3, cont3);
            Assert.AreEqual(3, queue.RequestCount);
            queue.Enqueue(ev4, cont4);
            Assert.AreEqual(3, queue.RequestCount);

            LogEventInfo[] logEventInfos;
            AsyncContinuation[] asyncContinuations;

            int result = queue.DequeueBatch(10, out logEventInfos, out asyncContinuations);
            Assert.AreEqual(result, logEventInfos.Length);
            Assert.AreEqual(result, asyncContinuations.Length);

            Assert.AreEqual(3, result);
            Assert.AreEqual(0, queue.RequestCount);

            // ev1 is lost
            Assert.AreSame(logEventInfos[0], ev2);
            Assert.AreSame(logEventInfos[1], ev3);
            Assert.AreSame(logEventInfos[2], ev4);

            // cont1 is lost
            Assert.AreSame(asyncContinuations[0], cont2);
            Assert.AreSame(asyncContinuations[1], cont3);
            Assert.AreSame(asyncContinuations[2], cont4);
        }