Skip to content

Commit

Permalink
Update dependencies, fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
nblumhardt committed Jun 6, 2024
1 parent ef00e39 commit b32bb6a
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sealed class BackgroundWorkerSink : ILogEventSink, IAsyncLogEventSinkInspector,

long _droppedMessages;

public BackgroundWorkerSink(ILogEventSink wrappedSink, int bufferCapacity, bool blockWhenFull, IAsyncLogEventSinkMonitor? monitor = null)
public BackgroundWorkerSink(ILogEventSink wrappedSink, int bufferCapacity, bool blockWhenFull, IAsyncLogEventSinkMonitor? monitor)
{
if (bufferCapacity <= 0) throw new ArgumentOutOfRangeException(nameof(bufferCapacity));
_wrappedSink = wrappedSink ?? throw new ArgumentNullException(nameof(wrappedSink));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
<PackageReference Include="Serilog.Sinks.File" Version="3.1.0" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>

</Project>
149 changes: 75 additions & 74 deletions test/Serilog.Sinks.Async.Tests/BackgroundWorkerSinkIntegrationSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,157 +10,158 @@

namespace Serilog.Sinks.Async.Tests;

public class BackgroundWorkerSinkIntegrationSpec
public static class BackgroundWorkerSinkIntegrationSpec
{
/// <summary>
/// If <see cref="withDelay" />, then adds a 1sec delay before every fifth element created
/// </summary>
static void CreateAudits(ILogger logger, int count, bool withDelay)
{
var delay = TimeSpan.FromMilliseconds(1000);
var sw = new Stopwatch();
sw.Start();
Debug.WriteLine("{0:h:mm:ss tt} Start: Writing {1} audits", DateTime.Now, count);
try
var delay = TimeSpan.FromMilliseconds(1000);
var sw = new Stopwatch();
sw.Start();
Debug.WriteLine("{0:h:mm:ss tt} Start: Writing {1} audits", DateTime.Now, count);
try
{
var delayCount = 0;
Loop.For(counter =>
{
var delayCount = 0;
Loop.For(counter =>
if (withDelay
&& counter > 0
&& counter % 5 == 0)
{
if (withDelay
&& counter > 0
&& counter%5 == 0)
{
delayCount++;
Debug.WriteLine("{0:h:mm:ss tt} Delay ({1}) after {2}th write, for {3:0.###}secs", DateTime.Now,
delayCount, counter,
delay.TotalSeconds);
Thread.Sleep(delay);
}
logger.Information("{$Counter}", counter);
}, count);
}
finally
{
sw.Stop();
Debug.WriteLine("{0:h:mm:ss tt} End: Writing {1} audits, taking {2:0.###}", DateTime.Now, count,
sw.Elapsed.TotalSeconds);
}
delayCount++;
Debug.WriteLine("{0:h:mm:ss tt} Delay ({1}) after {2}th write, for {3:0.###}secs", DateTime.Now,
delayCount, counter,
delay.TotalSeconds);
Thread.Sleep(delay);
}
logger.Information("{$Counter}", counter);
}, count);
}
finally
{
sw.Stop();
Debug.WriteLine("{0:h:mm:ss tt} End: Writing {1} audits, taking {2:0.###}", DateTime.Now, count,
sw.Elapsed.TotalSeconds);
}
}

static List<LogEvent> RetrieveEvents(MemorySink sink, int count)
{
Debug.WriteLine("{0:h:mm:ss tt} Retrieving {1} events", DateTime.Now, count);
Debug.WriteLine("{0:h:mm:ss tt} Retrieving {1} events", DateTime.Now, count);

Loop.Retry(() => sink.Events, events => events != null && events.Count >= count, TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(30));
Loop.Retry(() => sink.Events, events => events != null && events.Count >= count, TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(30));

return sink.Events.ToList();
}
return sink.Events.ToList();
}

public class GivenNoBufferQueueAndNoDelays : SinkSpecBase
{
public GivenNoBufferQueueAndNoDelays()
: base(false, false)
{
}
}
}

public class GivenBufferQueueAndNoDelays : SinkSpecBase
{
public GivenBufferQueueAndNoDelays()
: base(true, false)
{
}
}
}

public class GivenNoBufferQueueAndDelays : SinkSpecBase
{
public GivenNoBufferQueueAndDelays()
: base(false, true)
{
}
}
}

public class GivenBufferQueueAndDelays : SinkSpecBase
{
public GivenBufferQueueAndDelays()
: base(true, true)
{
}
}
}

public abstract class SinkSpecBase : IDisposable
{
bool _delayCreation;
Logger _logger;
MemorySink _memorySink;
readonly bool _delayCreation;
readonly Logger _logger;
readonly MemorySink _memorySink;

protected SinkSpecBase(bool useBufferedQueue, bool delayCreation)
{
_delayCreation = delayCreation;
_delayCreation = delayCreation;

_memorySink = new MemorySink();
_memorySink = new MemorySink();

if (useBufferedQueue)
{
_logger = new LoggerConfiguration()
.WriteTo.Async(a => a.Sink(_memorySink))
.CreateLogger();
}
else
{
_logger = new LoggerConfiguration()
.WriteTo.Sink(_memorySink)
.CreateLogger();
}

Debug.WriteLine("{0:h:mm:ss tt} Started test", DateTime.Now);
if (useBufferedQueue)
{
_logger = new LoggerConfiguration()
.WriteTo.Async(a => a.Sink(_memorySink))
.CreateLogger();
}
else
{
_logger = new LoggerConfiguration()
.WriteTo.Sink(_memorySink)
.CreateLogger();
}

Debug.WriteLine("{0:h:mm:ss tt} Started test", DateTime.Now);
}

public void Dispose()
{
_logger.Dispose();
Debug.WriteLine("{0:h:mm:ss tt} Ended test", DateTime.Now);
}
_logger.Dispose();
Debug.WriteLine("{0:h:mm:ss tt} Ended test", DateTime.Now);
}

[Fact]
public void WhenAuditSingle_ThenQueued()
{
CreateAudits(_logger, 1, _delayCreation);
CreateAudits(_logger, 1, _delayCreation);

var result = RetrieveEvents(_memorySink, 1);
var result = RetrieveEvents(_memorySink, 1);

Assert.Single(result);
}
Assert.Single(result);
}

[Fact]
public void WhenAuditTen_ThenQueued()
{
CreateAudits(_logger, 10, _delayCreation);
CreateAudits(_logger, 10, _delayCreation);

var result = RetrieveEvents(_memorySink, 10);
var result = RetrieveEvents(_memorySink, 10);

Assert.Equal(10, result.Count);
}
Assert.Equal(10, result.Count);
}

[Fact]
public void WhenAuditHundred_ThenQueued()
{
CreateAudits(_logger, 100, _delayCreation);
CreateAudits(_logger, 100, _delayCreation);

var result = RetrieveEvents(_memorySink, 100);
var result = RetrieveEvents(_memorySink, 100);

Assert.Equal(100, result.Count);
}
Assert.Equal(100, result.Count);
}

[Fact]
public void WhenAuditFiveHundred_ThenQueued()
{
CreateAudits(_logger, 500, _delayCreation);
CreateAudits(_logger, 500, _delayCreation);

var result = RetrieveEvents(_memorySink, 500);
var result = RetrieveEvents(_memorySink, 500);

Assert.Equal(500, result.Count);
}
Assert.Equal(500, result.Count);
}
}
}
Loading

0 comments on commit b32bb6a

Please sign in to comment.