diff --git a/src/libraries/Common/tests/System/Net/ActivityRecorder.cs b/src/libraries/Common/tests/System/Net/ActivityRecorder.cs index 6647076d4bac5..ebeef8b4bc4e3 100644 --- a/src/libraries/Common/tests/System/Net/ActivityRecorder.cs +++ b/src/libraries/Common/tests/System/Net/ActivityRecorder.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.Linq; using System.Text; +using System.Threading; using Xunit; namespace System.Net.Test.Common @@ -17,12 +18,16 @@ internal class ActivityRecorder : IDisposable private readonly ActivityListener _listener; private List _finishedActivities = new(); + private int _started; + private int _stopped; + + public int Started => _started; + public int Stopped => _stopped; + public Predicate Filter { get; set; } = _ => true; public bool VerifyParent { get; set; } = true; public Activity ExpectedParent { get; set; } - public int Started { get; private set; } - public int Stopped { get; private set; } public Activity LastStartedActivity { get; private set; } public Activity LastFinishedActivity { get; private set; } public IEnumerable FinishedActivities => _finishedActivities; @@ -35,7 +40,8 @@ public ActivityRecorder(string activitySourceName, string activityName) { ShouldListenTo = (activitySource) => activitySource.Name == _activitySourceName, Sample = (ref ActivityCreationOptions options) => ActivitySamplingResult.AllData, - ActivityStarted = (activity) => { + ActivityStarted = (activity) => + { if (activity.OperationName == _activityName && Filter(activity)) { if (VerifyParent) @@ -43,11 +49,13 @@ public ActivityRecorder(string activitySourceName, string activityName) Assert.Same(ExpectedParent, activity.Parent); } - Started++; + Interlocked.Increment(ref _started); + LastStartedActivity = activity; } }, - ActivityStopped = (activity) => { + ActivityStopped = (activity) => + { if (activity.OperationName == _activityName && Filter(activity)) { if (VerifyParent) @@ -55,14 +63,18 @@ public ActivityRecorder(string activitySourceName, string activityName) Assert.Same(ExpectedParent, activity.Parent); } - Stopped++; - LastFinishedActivity = activity; - _finishedActivities.Add(activity); + Interlocked.Increment(ref _stopped); + + lock (_finishedActivities) + { + LastFinishedActivity = activity; + _finishedActivities.Add(activity); + } } } }; - ActivitySource.AddActivityListener(_listener); + ActivitySource.AddActivityListener(_listener); } public void Dispose() => _listener.Dispose();