From f6d22201603db303d8e7feb4c8c9f27b0dcade6a Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Sun, 13 Aug 2023 09:41:52 -0700 Subject: [PATCH] Fix TimeProvider Test (#90448) --- .../Common/tests/System/TimeProviderTests.cs | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/libraries/Common/tests/System/TimeProviderTests.cs b/src/libraries/Common/tests/System/TimeProviderTests.cs index a4209e83f52..ecc7e934391 100644 --- a/src/libraries/Common/tests/System/TimeProviderTests.cs +++ b/src/libraries/Common/tests/System/TimeProviderTests.cs @@ -118,24 +118,27 @@ namespace Tests.System stat => { TimerState s = (TimerState)stat; - s.Counter++; + lock (s) + { + s.Counter++; - s.TotalTicks += DateTimeOffset.UtcNow.Ticks - s.UtcNow.Ticks; + s.TotalTicks += DateTimeOffset.UtcNow.Ticks - s.UtcNow.Ticks; - switch (s.Counter) - { - case 2: - s.Period = 400; - s.Timer.Change(TimeSpan.FromMilliseconds(s.Period), TimeSpan.FromMilliseconds(s.Period)); - break; - - case 4: - s.TokenSource.Cancel(); - s.Timer.Dispose(); - break; - } + switch (s.Counter) + { + case 2: + s.Period = 400; + s.Timer.Change(TimeSpan.FromMilliseconds(s.Period), TimeSpan.FromMilliseconds(s.Period)); + break; - s.UtcNow = DateTimeOffset.UtcNow; + case 4: + s.TokenSource.Cancel(); + s.Timer.Dispose(); + break; + } + + s.UtcNow = DateTimeOffset.UtcNow; + } }, state, TimeSpan.FromMilliseconds(state.Period), TimeSpan.FromMilliseconds(state.Period)); -- 2.34.1