Skip to content

Commit 67d72b0

Browse files
authored
Fix race conditions in tests (#1358)
1 parent faaf2ee commit 67d72b0

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

test/Polly.Core.Tests/Hedging/HedgingResilienceStrategyTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class HedgingResilienceStrategyTests : IDisposable
1515
private static readonly TimeSpan AssertTimeout = TimeSpan.FromSeconds(15);
1616

1717
private readonly HedgingStrategyOptions<string> _options = new();
18-
private readonly List<TelemetryEventArguments> _events = new();
18+
private readonly ConcurrentQueue<TelemetryEventArguments> _events = new();
1919
private readonly ResilienceStrategyTelemetry _telemetry;
2020
private readonly HedgingTimeProvider _timeProvider;
2121
private readonly HedgingActions _actions;
@@ -27,7 +27,7 @@ public class HedgingResilienceStrategyTests : IDisposable
2727

2828
public HedgingResilienceStrategyTests(ITestOutputHelper testOutput)
2929
{
30-
_telemetry = TestUtilities.CreateResilienceTelemetry(_events.Add);
30+
_telemetry = TestUtilities.CreateResilienceTelemetry(_events.Enqueue);
3131
_timeProvider = new HedgingTimeProvider { AutoAdvance = _options.HedgingDelay };
3232
_actions = new HedgingActions(_timeProvider);
3333
_primaryTasks = new PrimaryStringTasks(_timeProvider);

test/Polly.Core.Tests/Hedging/HedgingTimeProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public void Advance(TimeSpan diff)
2020

2121
public Func<int> TimeStampProvider { get; set; } = () => 0;
2222

23-
public List<TimerEntry> TimerEntries { get; } = new List<TimerEntry>();
23+
public ConcurrentQueue<TimerEntry> TimerEntries { get; } = new();
2424

2525
public override DateTimeOffset GetUtcNow() => _utcNow;
2626

2727
public override ITimer CreateTimer(TimerCallback callback, object? state, TimeSpan dueTime, TimeSpan period)
2828
{
2929
var entry = new TimerEntry(dueTime, new TaskCompletionSource<bool>(), _utcNow.Add(dueTime), () => callback(state));
30-
TimerEntries.Add(entry);
30+
TimerEntries.Enqueue(entry);
3131

3232
Advance(AutoAdvance);
3333

test/Polly.TestUtils/TestUtilities.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public static ResilienceContext WithResultType<T>(this ResilienceContext context
105105
private sealed class CallbackDiagnosticSource : DiagnosticSource
106106
{
107107
private readonly Action<TelemetryEventArguments> _callback;
108+
private readonly object _syncRoot = new();
108109

109110
public CallbackDiagnosticSource(Action<TelemetryEventArguments> callback) => _callback = callback;
110111

@@ -122,7 +123,11 @@ public override void Write(string name, object? value)
122123

123124
// copy the args because these are pooled and in tests we want to preserve them
124125
args = TelemetryEventArguments.Get(args.Source, args.EventName, args.Context, args.Outcome, arguments);
125-
_callback(args);
126+
127+
lock (_syncRoot)
128+
{
129+
_callback(args);
130+
}
126131
}
127132
}
128133
}

0 commit comments

Comments
 (0)