-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from nblumhardt/reinstate-inheritance-api
Reinstate the legacy inheritance-based API - again
- Loading branch information
Showing
5 changed files
with
269 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
test/Serilog.Sinks.PeriodicBatching.Tests/BackwardsCompatibilityTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#if FEATURE_ASYNCDISPOSABLE | ||
|
||
using Serilog.Sinks.PeriodicBatching.Tests.Support; | ||
using Serilog.Tests.Support; | ||
using Xunit; | ||
|
||
namespace Serilog.Sinks.PeriodicBatching.Tests; | ||
|
||
public class BackwardsCompatibilityTests | ||
{ | ||
static readonly TimeSpan TinyWait = TimeSpan.FromMilliseconds(200); | ||
|
||
[Fact] | ||
public void LegacyWhenAnEventIsEnqueuedItIsWrittenToABatchOnDispose() | ||
{ | ||
var bs = new LegacyDisposeTrackingSink(); | ||
var evt = Some.InformationEvent(); | ||
bs.Emit(evt); | ||
bs.Dispose(); | ||
var batch = Assert.Single(bs.Batches); | ||
var batched = Assert.Single(batch); | ||
Assert.Same(evt, batched); | ||
Assert.True(bs.IsDisposed); | ||
Assert.False(bs.WasCalledAfterDisposal); | ||
} | ||
|
||
[Fact] | ||
public void LegacyWhenAnEventIsEnqueuedItIsWrittenToABatchOnTimer() | ||
{ | ||
var bs = new LegacyDisposeTrackingSink(); | ||
var evt = Some.InformationEvent(); | ||
bs.Emit(evt); | ||
Thread.Sleep(TinyWait + TinyWait); | ||
bs.Stop(); | ||
bs.Dispose(); | ||
Assert.Single(bs.Batches); | ||
Assert.True(bs.IsDisposed); | ||
Assert.False(bs.WasCalledAfterDisposal); | ||
} | ||
|
||
[Fact] | ||
public void LegacySinksAreDisposedWhenLoggerIsDisposed() | ||
{ | ||
var sink = new LegacyDisposeTrackingSink(); | ||
var logger = new LoggerConfiguration().WriteTo.Sink(sink).CreateLogger(); | ||
logger.Dispose(); | ||
Assert.True(sink.IsDisposed); | ||
} | ||
|
||
[Fact] | ||
public async Task LegacySinksAreDisposedWhenLoggerIsDisposedAsync() | ||
{ | ||
var sink = new LegacyDisposeTrackingSink(); | ||
var logger = new LoggerConfiguration().WriteTo.Sink(sink).CreateLogger(); | ||
await logger.DisposeAsync(); | ||
Assert.True(sink.IsDisposed); | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
test/Serilog.Sinks.PeriodicBatching.Tests/Support/LegacyDisposeTrackingSink.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Serilog.Events; | ||
|
||
#pragma warning disable CS0618 | ||
|
||
namespace Serilog.Sinks.PeriodicBatching.Tests.Support; | ||
|
||
public class LegacyDisposeTrackingSink() | ||
: PeriodicBatchingSink(10, TimeSpan.FromMinutes(1)) | ||
{ | ||
readonly object _stateLock = new(); | ||
bool _stopped; | ||
|
||
// Postmortem only | ||
public bool WasCalledAfterDisposal { get; private set; } | ||
public bool IsDisposed { get; private set; } | ||
public IList<IList<LogEvent>> Batches { get; } = new List<IList<LogEvent>>(); | ||
|
||
public void Stop() | ||
{ | ||
lock (_stateLock) | ||
{ | ||
_stopped = true; | ||
} | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
base.Dispose(disposing); | ||
|
||
lock (_stateLock) | ||
{ | ||
IsDisposed = true; | ||
} | ||
} | ||
|
||
protected override void EmitBatch(IEnumerable<LogEvent> events) | ||
{ | ||
lock (_stateLock) | ||
{ | ||
if (_stopped) | ||
return; | ||
|
||
if (IsDisposed) | ||
WasCalledAfterDisposal = true; | ||
|
||
Batches.Add(events.ToList()); | ||
} | ||
} | ||
} |