This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
SystemEvents.CreateTimer.cs
175 lines (153 loc) · 6.61 KB
/
SystemEvents.CreateTimer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.Win32.SystemEventsTests
{
public class CreateTimerTests
{
/// <summary>
/// Minimum permitted interval
/// </summary>
public const int TimerInterval = 10;
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
public void CreateTimerInvalidInterval()
{
Assert.Throws<ArgumentException>(() => SystemEvents.CreateTimer(0));
Assert.Throws<ArgumentException>(() => SystemEvents.CreateTimer(-1));
Assert.Throws<ArgumentException>(() => SystemEvents.CreateTimer(int.MinValue));
}
[ActiveIssue(29166)]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
public void TimerElapsedSignaled()
{
var elapsed = new AutoResetEvent(false);
object elapsedSender = null;
IntPtr timer = IntPtr.Zero;
TimerElapsedEventHandler handler = (sender, args) =>
{
if (args?.TimerId == timer)
{
elapsedSender = sender;
elapsed.Set();
}
};
SystemEvents.TimerElapsed += handler;
try
{
if (PlatformDetection.IsFullFramework)
{
// desktop has a bug where it will allow EnsureSystemEvents to proceed without actually creating the HWND
SystemEventsTest.WaitForSystemEventsWindow();
}
timer = SystemEvents.CreateTimer(TimerInterval);
Assert.True(elapsed.WaitOne(TimerInterval * SystemEventsTest.ExpectedEventMultiplier));
Assert.IsType<SystemEvents>(elapsedSender);
// Timer should fire more than once
Assert.True(elapsed.WaitOne(TimerInterval * SystemEventsTest.ExpectedEventMultiplier));
SystemEvents.KillTimer(timer);
elapsed.Reset();
// Timer should not fire once killed
Assert.False(elapsed.WaitOne(TimerInterval * SystemEventsTest.UnexpectedEventMultiplier));
}
finally
{
SystemEvents.TimerElapsed -= handler;
elapsed.Dispose();
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
public void ConcurrentTimers()
{
const int NumConcurrentTimers = 10;
var timersSignaled = new ConcurrentDictionary<IntPtr, bool>();
int numSignaled = 0;
var elapsed = new ManualResetEventSlim();
TimerElapsedEventHandler handler = (sender, args) =>
{
// A timer might fire more than once. When it fires the first time, track it by adding
// it to a set and then increment the number of timers that have ever fired. When all
// timers have fired, set the event.
if (timersSignaled.TryAdd(args.TimerId, true) &&
Interlocked.Increment(ref numSignaled) == NumConcurrentTimers)
{
elapsed.Set();
}
};
SystemEvents.TimerElapsed += handler;
try
{
if (PlatformDetection.IsFullFramework)
{
// netfx has a bug where it will allow EnsureSystemEvents to proceed without actually creating the HWND
SystemEventsTest.WaitForSystemEventsWindow();
}
// Create all the timers
var timers = new List<IntPtr>();
for (int i = 0; i < NumConcurrentTimers; i++)
{
timers.Add(SystemEvents.CreateTimer(TimerInterval));
}
// Wait for them all to fire
Assert.True(elapsed.Wait(TimerInterval * SystemEventsTest.ExpectedEventMultiplier));
// Delete them all
foreach (IntPtr timer in timers)
{
Assert.True(timersSignaled.TryGetValue(timer, out _));
SystemEvents.KillTimer(timer);
}
}
finally
{
SystemEvents.TimerElapsed -= handler;
elapsed.Dispose();
}
}
[OuterLoop]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
[InlineData(500)] // .5s
[InlineData(1000)] // 1s
[InlineData(2000)] // 2s
[InlineData(10000)] // 10s
[InlineData(30000)] // 30s
public void TimerElapsedIsRoughlyEquivalentToInterval(int interval)
{
const double permittedProportionUnder = -0.1;
const double permittedProportionOver = 0.5;
var elapsed = new AutoResetEvent(false);
IntPtr timer = IntPtr.Zero;
var stopwatch = new Stopwatch();
TimerElapsedEventHandler handler = (sender, args) =>
{
if (args?.TimerId == timer)
{
stopwatch.Stop();
elapsed.Set();
}
};
SystemEvents.TimerElapsed += handler;
try
{
timer = SystemEvents.CreateTimer(interval);
stopwatch.Start();
Assert.True(elapsed.WaitOne(interval * SystemEventsTest.ExpectedEventMultiplier));
var proportionDifference = (double)(stopwatch.ElapsedMilliseconds - interval) / interval;
Assert.True(permittedProportionUnder < proportionDifference && proportionDifference < permittedProportionOver,
$"Timer should fire less than {permittedProportionUnder * 100.0}% before and less than {permittedProportionOver * 100.0}% after expected interval {interval}, actual: {stopwatch.ElapsedMilliseconds}, difference: {proportionDifference * 100.0}%");
}
finally
{
SystemEvents.TimerElapsed -= handler;
SystemEvents.KillTimer(timer);
}
}
}
}