Skip to content

Commit 393e4ad

Browse files
committed
Defensive check to make sure OperationCanceledException is not thrown; try to make test more robust on slow machines
1 parent 31f08ba commit 393e4ad

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/Serilog.Sinks.PeriodicBatching/Sinks/PeriodicBatching/PeriodicBatchingSink.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ async Task<bool> TryWaitToReadAsync(ChannelReader<LogEvent> reader, Task timeout
228228
return false;
229229
}
230230

231+
if (waitToRead.Status is not TaskStatus.RanToCompletion)
232+
return false;
233+
231234
return await waitToRead;
232235
}
233236

test/Serilog.Sinks.PeriodicBatching.Tests/PeriodicBatchingSinkTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,16 @@ public void ExecutionContextDoesNotFlowToBatchedSink()
115115
[InlineData(false)]
116116
public async Task EagerlyEmitFirstEventCausesQuickInitialBatch(bool eagerlyEmit)
117117
{
118-
long batchesEmitted = 0;
118+
var x = new ManualResetEvent(false);
119119
var bs = new CallbackBatchedSink(_ =>
120120
{
121-
// ReSharper disable once AccessToModifiedClosure
122-
Interlocked.Increment(ref batchesEmitted);
121+
x.Set();
123122
return Task.CompletedTask;
124123
});
125124

126125
var options = new PeriodicBatchingSinkOptions
127126
{
128-
Period = TimeSpan.FromSeconds(2),
127+
Period = TimeSpan.FromSeconds(3),
129128
EagerlyEmitFirstEvent = eagerlyEmit,
130129
BatchSizeLimit = 10,
131130
QueueLimit = 1000
@@ -137,14 +136,14 @@ public async Task EagerlyEmitFirstEventCausesQuickInitialBatch(bool eagerlyEmit)
137136
pbs.Emit(evt);
138137

139138
await Task.Delay(1900);
140-
Assert.Equal(eagerlyEmit ? 1L : 0, Interlocked.Read(ref batchesEmitted));
139+
Assert.Equal(eagerlyEmit, x.WaitOne(0));
141140

142141
#if FEATURE_ASYNCDISPOSABLE
143142
await pbs.DisposeAsync();
144143
#else
145144
pbs.Dispose();
146145
#endif
147146

148-
Assert.Equal(1L, Interlocked.Read(ref batchesEmitted));
147+
Assert.True(x.WaitOne(0));
149148
}
150149
}

0 commit comments

Comments
 (0)