Fix TimeProvider Test (#90448)
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Sun, 13 Aug 2023 16:41:52 +0000 (09:41 -0700)
committerGitHub <noreply@github.com>
Sun, 13 Aug 2023 16:41:52 +0000 (09:41 -0700)
src/libraries/Common/tests/System/TimeProviderTests.cs

index a4209e8..ecc7e93 100644 (file)
@@ -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));