Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race conditions in tests #1358

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}