From 34c94225216955ad9a91eba7dd1449e8f14cc496 Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Tue, 19 Feb 2019 13:30:41 -0800 Subject: [PATCH 1/3] Add a timer test - Separated from https://github.com/dotnet/corefx/pull/35502 - Added test from https://github.com/dotnet/corert/pull/7069 to CoreFX --- .../tests/System.Threading.Timer.Tests.csproj | 5 ++ .../tests/TimerFiringTests.cs | 54 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/System.Threading.Timer/tests/System.Threading.Timer.Tests.csproj b/src/System.Threading.Timer/tests/System.Threading.Timer.Tests.csproj index 2bb3c8ebefd5..df858bdb16bc 100644 --- a/src/System.Threading.Timer/tests/System.Threading.Timer.Tests.csproj +++ b/src/System.Threading.Timer/tests/System.Threading.Timer.Tests.csproj @@ -12,4 +12,9 @@ + + + CommonTest\System\Threading\ThreadTestHelpers.cs + + \ No newline at end of file diff --git a/src/System.Threading.Timer/tests/TimerFiringTests.cs b/src/System.Threading.Timer/tests/TimerFiringTests.cs index 00006f68f0df..3efd7fb17876 100644 --- a/src/System.Threading.Timer/tests/TimerFiringTests.cs +++ b/src/System.Threading.Timer/tests/TimerFiringTests.cs @@ -10,6 +10,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using System.Threading.Tests; using Xunit; using Xunit.Sdk; @@ -337,4 +338,57 @@ private static async Task PeriodAsync(int period, int iterations) await tcs.Task.ConfigureAwait(false); } } + + [Fact] + public void TimersCreatedConcurrentlyOnDifferentThreadsAllFire() + { + int processorCount = Environment.ProcessorCount; + + int timerTickCount = 0; + TimerCallback timerCallback = data => Interlocked.Increment(ref timerTickCount); + + var threadStarted = new AutoResetEvent(false); + var createTimers = new ManualResetEvent(false); + var timers = new Timer[processorCount]; + Action createTimerThreadStart = data => + { + int i = (int)data; + var sw = new Stopwatch(); + threadStarted.Set(); + createTimers.WaitOne(); + + // Use the CPU a bit around creating the timer to try to have some of these threads run concurrently + sw.Restart(); + do + { + Thread.SpinWait(1000); + } while (sw.ElapsedMilliseconds < 10); + + timers[i] = new Timer(timerCallback, null, 1, Timeout.Infinite); + + // Use the CPU a bit around creating the timer to try to have some of these threads run concurrently + sw.Restart(); + do + { + Thread.SpinWait(1000); + } while (sw.ElapsedMilliseconds < 10); + }; + + var waitsForThread = new Action[timers.Length]; + for (int i = 0; i < timers.Length; ++i) + { + var t = ThreadTestHelpers.CreateGuardedThread(out waitsForThread[i], createTimerThreadStart); + t.IsBackground = true; + t.Start(i); + threadStarted.CheckedWait(); + } + + createTimers.Set(); + ThreadTestHelpers.WaitForCondition(() => timerTickCount == timers.Length); + + foreach (var waitForThread in waitsForThread) + { + waitForThread(); + } + } } From 48d32fbed4e4c33e8a2684f8646a8bc72b12d484 Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Fri, 5 Apr 2019 03:27:28 -0700 Subject: [PATCH 2/3] Address feedback --- src/System.Threading.Timer/tests/TimerFiringTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Threading.Timer/tests/TimerFiringTests.cs b/src/System.Threading.Timer/tests/TimerFiringTests.cs index 3efd7fb17876..375df334653b 100644 --- a/src/System.Threading.Timer/tests/TimerFiringTests.cs +++ b/src/System.Threading.Timer/tests/TimerFiringTests.cs @@ -345,7 +345,7 @@ public void TimersCreatedConcurrentlyOnDifferentThreadsAllFire() int processorCount = Environment.ProcessorCount; int timerTickCount = 0; - TimerCallback timerCallback = data => Interlocked.Increment(ref timerTickCount); + TimerCallback timerCallback = _ => Interlocked.Increment(ref timerTickCount); var threadStarted = new AutoResetEvent(false); var createTimers = new ManualResetEvent(false); From 95454263558e8719b794e97f1caa390d97ff2cbc Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Fri, 5 Apr 2019 04:35:24 -0700 Subject: [PATCH 3/3] Include remote executor for project --- .../tests/System.Threading.Timer.Tests.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/System.Threading.Timer/tests/System.Threading.Timer.Tests.csproj b/src/System.Threading.Timer/tests/System.Threading.Timer.Tests.csproj index df858bdb16bc..d4b83860cfa4 100644 --- a/src/System.Threading.Timer/tests/System.Threading.Timer.Tests.csproj +++ b/src/System.Threading.Timer/tests/System.Threading.Timer.Tests.csproj @@ -3,6 +3,7 @@ {ac20a28f-fda8-45e8-8728-058ead16e44c} netcoreapp-Debug;netcoreapp-Release;netstandard-Debug;netstandard-Release true + true