Skip to content

Commit

Permalink
Fix race conditions in tests (#1358)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Jun 27, 2023
1 parent faaf2ee commit 67d72b0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class HedgingResilienceStrategyTests : IDisposable
private static readonly TimeSpan AssertTimeout = TimeSpan.FromSeconds(15);

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

public HedgingResilienceStrategyTests(ITestOutputHelper testOutput)
{
_telemetry = TestUtilities.CreateResilienceTelemetry(_events.Add);
_telemetry = TestUtilities.CreateResilienceTelemetry(_events.Enqueue);
_timeProvider = new HedgingTimeProvider { AutoAdvance = _options.HedgingDelay };
_actions = new HedgingActions(_timeProvider);
_primaryTasks = new PrimaryStringTasks(_timeProvider);
Expand Down
4 changes: 2 additions & 2 deletions test/Polly.Core.Tests/Hedging/HedgingTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public void Advance(TimeSpan diff)

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

public List<TimerEntry> TimerEntries { get; } = new List<TimerEntry>();
public ConcurrentQueue<TimerEntry> TimerEntries { get; } = new();

public override DateTimeOffset GetUtcNow() => _utcNow;

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

Advance(AutoAdvance);

Expand Down
7 changes: 6 additions & 1 deletion test/Polly.TestUtils/TestUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public static ResilienceContext WithResultType<T>(this ResilienceContext context
private sealed class CallbackDiagnosticSource : DiagnosticSource
{
private readonly Action<TelemetryEventArguments> _callback;
private readonly object _syncRoot = new();

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

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

// copy the args because these are pooled and in tests we want to preserve them
args = TelemetryEventArguments.Get(args.Source, args.EventName, args.Context, args.Outcome, arguments);
_callback(args);

lock (_syncRoot)
{
_callback(args);
}
}
}
}

0 comments on commit 67d72b0

Please sign in to comment.