From ec88ccebfb7beb8e337bd883f54819c818b83b5d Mon Sep 17 00:00:00 2001 From: FantasticFiasco Date: Sun, 10 Mar 2024 22:59:04 +0100 Subject: [PATCH] wip --- .../Durable/FileSizeRolledDurableHttpSink.cs | 1 - .../TimeRolledDurableHttpSinkShould.cs | 87 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/src/Serilog.Sinks.Http/Sinks/Http/Private/Durable/FileSizeRolledDurableHttpSink.cs b/src/Serilog.Sinks.Http/Sinks/Http/Private/Durable/FileSizeRolledDurableHttpSink.cs index adb8b3f3..a072afd0 100644 --- a/src/Serilog.Sinks.Http/Sinks/Http/Private/Durable/FileSizeRolledDurableHttpSink.cs +++ b/src/Serilog.Sinks.Http/Sinks/Http/Private/Durable/FileSizeRolledDurableHttpSink.cs @@ -14,7 +14,6 @@ using System; using Serilog.Core; -using Serilog.Debugging; using Serilog.Events; using Serilog.Formatting; using Serilog.Sinks.Http.Private.IO; diff --git a/test/Serilog.Sinks.HttpTests/Sinks/Http/Private/Durable/TimeRolledDurableHttpSinkShould.cs b/test/Serilog.Sinks.HttpTests/Sinks/Http/Private/Durable/TimeRolledDurableHttpSinkShould.cs index ae1da877..323219da 100644 --- a/test/Serilog.Sinks.HttpTests/Sinks/Http/Private/Durable/TimeRolledDurableHttpSinkShould.cs +++ b/test/Serilog.Sinks.HttpTests/Sinks/Http/Private/Durable/TimeRolledDurableHttpSinkShould.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using System.Threading.Tasks; using Serilog.Sinks.Http.BatchFormatters; using Serilog.Sinks.Http.HttpClients; @@ -146,4 +147,90 @@ public async Task RespectLogEventLimitBytes() webServerFixture.GetAllBatches(testId).ShouldBeEmpty(); webServerFixture.GetAllEvents(testId).ShouldBeEmpty(); } + + [Fact] + public void SendStoredLogEventsGivenFlushOnClose() + { + // Arrange + var testId = $"SendStoredLogEventsGivenFlushOnClose_{Guid.NewGuid()}"; + + // Create 10 log events + var logEvents = Enumerable + .Range(1, 10) + .Select(number => Some.LogEvent("Event {number}", number)) + .ToArray(); + + var period = TimeSpan.FromSeconds(5); + var flushOnClose = true; + + var sink = new TimeRolledDurableHttpSink( + requestUri: webServerFixture.RequestUri(testId), + bufferBaseFileName: Path.Combine("logs", testId), + bufferRollingInterval: BufferRollingInterval.Day, + bufferFileSizeLimitBytes: null, + bufferFileShared: false, + retainedBufferFileCountLimit: 31, + logEventLimitBytes: null, + logEventsInBatchLimit: 1000, + batchSizeLimitBytes: null, + period: period, + flushOnClose: flushOnClose, + textFormatter: new NormalTextFormatter(), + batchFormatter: new ArrayBatchFormatter(), + httpClient: new JsonHttpClient(webServerFixture.CreateClient())); + + // Act + foreach (var logEvent in logEvents) + { + sink.Emit(logEvent); + } + + sink.Dispose(); + + // Assert + webServerFixture.GetAllEvents(testId).Length.ShouldBe(logEvents.Length); + } + + [Fact] + public void IgnoreSendingStoredLogEventsGivenNoFlushOnClose() + { + // Arrange + var testId = $"IgnoreSendingStoredLogEventsGivenNoFlushOnClose_{Guid.NewGuid()}"; + + // Create 10 log events + var logEvents = Enumerable + .Range(1, 10) + .Select(number => Some.LogEvent("Event {number}", number)) + .ToArray(); + + var period = TimeSpan.FromSeconds(5); + var flushOnClose = false; + + var sink = new TimeRolledDurableHttpSink( + requestUri: webServerFixture.RequestUri(testId), + bufferBaseFileName: Path.Combine("logs", testId), + bufferRollingInterval: BufferRollingInterval.Day, + bufferFileSizeLimitBytes: null, + bufferFileShared: false, + retainedBufferFileCountLimit: 31, + logEventLimitBytes: null, + logEventsInBatchLimit: 1000, + batchSizeLimitBytes: null, + period: period, + flushOnClose: flushOnClose, + textFormatter: new NormalTextFormatter(), + batchFormatter: new ArrayBatchFormatter(), + httpClient: new JsonHttpClient(webServerFixture.CreateClient())); + + // Act + foreach (var logEvent in logEvents) + { + sink.Emit(logEvent); + } + + sink.Dispose(); + + // Assert + webServerFixture.GetAllEvents(testId).Length.ShouldBe(0); + } }