示例#1
0
        public void UseFifo()
        {
            // Arrange
            var queue = new LogEventQueue(null);

            queue.Enqueue("1");
            queue.Enqueue("2");
            queue.Enqueue("3");

            // Act
            var dequeueResult1 = queue.TryDequeue(long.MaxValue, out var got1);
            var dequeueResult2 = queue.TryDequeue(long.MaxValue, out var got2);
            var dequeueResult3 = queue.TryDequeue(long.MaxValue, out var got3);
            var dequeueResult4 = queue.TryDequeue(long.MaxValue, out var got4);

            // Assert
            dequeueResult1.ShouldBe(LogEventQueue.DequeueResult.Ok);
            dequeueResult2.ShouldBe(LogEventQueue.DequeueResult.Ok);
            dequeueResult3.ShouldBe(LogEventQueue.DequeueResult.Ok);
            dequeueResult4.ShouldBe(LogEventQueue.DequeueResult.QueueEmpty);

            got1.ShouldBe("1");
            got2.ShouldBe("2");
            got3.ShouldBe("3");
            got4.ShouldBe("");
        }
        public static Batch Read(LogEventQueue queue, int batchPostingLimit, long batchSizeLimitBytes)
        {
            var batch = new Batch();
            var remainingBatchSizeBytes = batchSizeLimitBytes;

            while (true)
            {
                var result = queue.TryDequeue(remainingBatchSizeBytes, out var logEvent);
                if (result == LogEventQueue.DequeueResult.Ok)
                {
                    batch.LogEvents.Add(logEvent);
                    remainingBatchSizeBytes -= ByteSize.From(logEvent);

                    // Respect batch posting limit
                    if (batch.LogEvents.Count == batchPostingLimit)
                    {
                        batch.HasReachedLimit = true;
                        break;
                    }
                }
                else if (result == LogEventQueue.DequeueResult.MaxSizeViolation)
                {
                    if (batch.LogEvents.Count == 0)
                    {
                        // This single log event exceeds the batch size limit, let's drop it
                        queue.TryDequeue(long.MaxValue, out var logEventToDrop);

                        SelfLog.WriteLine(
                            "Event exceeds the batch size limit of {0} bytes set for this sink and will be dropped; data: {1}",
                            batchSizeLimitBytes,
                            logEventToDrop);
                    }
                    else
                    {
                        batch.HasReachedLimit = true;
                        break;
                    }
                }
                else if (result == LogEventQueue.DequeueResult.QueueEmpty)
                {
                    break;
                }
            }

            return(batch);
        }
示例#3
0
        public void NotDequeueGivenEmptyQueue()
        {
            // Arrange
            var queue = new LogEventQueue(null);

            // Act
            var result = queue.TryDequeue(long.MaxValue, out var got);

            // Assert
            result.ShouldBe(LogEventQueue.DequeueResult.QueueEmpty);
            got.ShouldBe("");
        }
示例#4
0
        public void DequeueGivenMaxSize(string logEvent, int maxSize, LogEventQueue.DequeueResult want)
        {
            // Arrange
            var queue = new LogEventQueue(null);

            queue.Enqueue(logEvent);

            // Act
            var got = queue.TryDequeue(maxSize, out var dequeuedLogEvent);

            // Assert
            got.ShouldBe(want);
            dequeuedLogEvent.ShouldBe(want == LogEventQueue.DequeueResult.Ok ? logEvent : "");
        }
示例#5
0
        public void Dequeue()
        {
            // Arrange
            var queue = new LogEventQueue(null);

            queue.Enqueue("1");

            // Act
            var result = queue.TryDequeue(long.MaxValue, out var got);

            // Assert
            result.ShouldBe(LogEventQueue.DequeueResult.Ok);
            got.ShouldBe("1");
        }