Skip to content

Commit

Permalink
Fix flaky resource monitoring tests (#4782)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Taillefer <mataille@microsoft.com>
  • Loading branch information
geeknoid and Martin Taillefer authored Dec 3, 2023
1 parent 686e13c commit e743014
Showing 1 changed file with 32 additions and 10 deletions.
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
}
}
}

0 comments on commit e743014

Please sign in to comment.