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 flaky resource monitoring tests #4782

Merged
merged 1 commit into from
Dec 3, 2023
Merged
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 @@ -245,7 +245,7 @@ public async Task RunTrackerAsync_IfProviderThrows_LogsError()
}),
new List<IResourceUtilizationPublisher>
{
new GenericPublisher((_) => e.Set())
new GenericPublisher((_) => ResilientSetEvent(e))
},
clock);

Expand Down Expand Up @@ -322,7 +322,7 @@ public async Task ResourceUtilizationTracker_InitializedProperly_InvokesPublishe
new GenericPublisher(_ =>
{
publisherCalled = true;
autoResetEvent.Set();
ResilientSetEvent(autoResetEvent);
})
},
clock);
Expand Down Expand Up @@ -415,10 +415,7 @@ public async Task ResourceUtilizationTracker_WhenInitializedWithZeroSnapshots_Re
Create(options),
new List<IResourceUtilizationPublisher>
{
new GenericPublisher(_ =>
{
autoResetEvent.Set();
}),
new GenericPublisher(_ => ResilientSetEvent(autoResetEvent))
},
clock);

Expand Down Expand Up @@ -527,10 +524,7 @@ public async Task ResourceUtilizationTracker_WhenInitializedWithProperSnapshots_
Create(options),
new List<IResourceUtilizationPublisher>
{
new GenericPublisher(_ =>
{
autoResetEvent.Set();
}),
new GenericPublisher(_ => ResilientSetEvent(autoResetEvent))
},
clock);

Expand Down Expand Up @@ -711,4 +705,32 @@ public async Task Disposing_Service_Twice_Does_Not_Throw()
var type = typ.GetField(name, BindingFlags.NonPublic | BindingFlags.Instance);
return (T?)type?.GetValue(tracker);
}

private static void ResilientSetEvent(AutoResetEvent e)
{
try
{
e.Set();
}
#pragma warning disable CA1031
catch
#pragma warning restore CA1031
{
// can happen since test termination is racy and the event might have already been disposed
}
}

private static void ResilientSetEvent(ManualResetEventSlim e)
{
try
{
e.Set();
}
#pragma warning disable CA1031
catch
#pragma warning restore CA1031
{
// can happen since test termination is racy and the event might have already been disposed
}
}
}