-
-
Notifications
You must be signed in to change notification settings - Fork 986
/
Copy pathWakeLockTests.cs
179 lines (158 loc) · 6.11 KB
/
WakeLockTests.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
176
177
178
179
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Helpers;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Tests.Loggers;
using BenchmarkDotNet.Tests.XUnit;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace BenchmarkDotNet.IntegrationTests;
public class WakeLockTests : BenchmarkTestExecutor
{
private const string PingEventName = @"Global\WakeLockTests-ping";
private const string PongEventName = @"Global\WakeLockTests-pong";
private static readonly TimeSpan testTimeout = TimeSpan.FromMinutes(1);
private readonly OutputLogger logger;
public WakeLockTests(ITestOutputHelper output) : base(output)
{
logger = new OutputLogger(Output);
}
[Fact]
public void ConfigurationDefaultValue()
{
Assert.Equal(WakeLockType.System, DefaultConfig.Instance.WakeLock);
Assert.Equal(WakeLockType.None, new DebugBuildConfig().WakeLock);
Assert.Equal(WakeLockType.None, new DebugInProcessConfig().WakeLock);
}
[TheoryEnvSpecific(EnvRequirement.NonWindows)]
[InlineData(WakeLockType.None)]
[InlineData(WakeLockType.System)]
[InlineData(WakeLockType.Display)]
public void WakeLockIsWindowsOnly(WakeLockType wakeLockType)
{
using IDisposable wakeLock = WakeLock.Request(wakeLockType, "dummy", logger);
Assert.Null(wakeLock);
}
[FactEnvSpecific(EnvRequirement.WindowsOnly)]
public void WakeLockSleepOrDisplayIsAllowed()
{
using IDisposable wakeLock = WakeLock.Request(WakeLockType.None, "dummy", logger);
Assert.Null(wakeLock);
}
[FactEnvSpecific(EnvRequirement.WindowsOnly, EnvRequirement.NeedsPrivilegedProcess)]
public void WakeLockRequireSystem()
{
using (IDisposable wakeLock = WakeLock.Request(WakeLockType.System, "WakeLockTests", logger))
{
Assert.NotNull(wakeLock);
Assert.Equal("SYSTEM", GetPowerRequests("WakeLockTests"));
}
Assert.Equal("", GetPowerRequests());
}
[FactEnvSpecific(EnvRequirement.WindowsOnly, EnvRequirement.NeedsPrivilegedProcess)]
public void WakeLockRequireDisplay()
{
using (IDisposable wakeLock = WakeLock.Request(WakeLockType.Display, "WakeLockTests", logger))
{
Assert.NotNull(wakeLock);
Assert.Equal("DISPLAY, SYSTEM", GetPowerRequests("WakeLockTests"));
}
Assert.Equal("", GetPowerRequests());
}
[FactEnvSpecific(EnvRequirement.NonWindows)]
public void BenchmarkRunnerIgnoresWakeLock() =>
_ = CanExecute<IgnoreWakeLock>(fullValidation: false);
[WakeLock(WakeLockType.Display)]
public class IgnoreWakeLock
{
[Benchmark] public void Sleep() { }
}
#if !NET462
[SupportedOSPlatform("windows")]
#endif
[TheoryEnvSpecific(EnvRequirement.WindowsOnly, EnvRequirement.NeedsPrivilegedProcess)]
[InlineData(typeof(Default), "SYSTEM")]
[InlineData(typeof(None), "")]
[InlineData(typeof(RequireSystem), "SYSTEM")]
[InlineData(typeof(RequireDisplay), "DISPLAY, SYSTEM")]
public async Task BenchmarkRunnerAcquiresWakeLock(Type type, string expected)
{
using EventWaitHandle
ping = new EventWaitHandle(false, EventResetMode.AutoReset, PingEventName),
pong = new EventWaitHandle(false, EventResetMode.AutoReset, PongEventName);
string pwrRequests = null;
Task task = WaitForBenchmarkRunningAndGetPowerRequests();
_ = CanExecute(type, fullValidation: false);
await task;
Assert.Equal(expected, pwrRequests);
async Task WaitForBenchmarkRunningAndGetPowerRequests()
{
await AsTask(ping, testTimeout);
pwrRequests = GetPowerRequests("BenchmarkDotNet Running Benchmarks");
pong.Set();
}
}
public class Default : Base { }
[WakeLock(WakeLockType.None)] public class None : Base { }
[WakeLock(WakeLockType.System)] public class RequireSystem : Base { }
[WakeLock(WakeLockType.Display)] public class RequireDisplay : Base { }
public class Base
{
[Benchmark]
#if !NET462
[SupportedOSPlatform("windows")]
#endif
public void SignalBenchmarkRunningAndWaitForGetPowerRequests()
{
using EventWaitHandle
ping = EventWaitHandle.OpenExisting(PingEventName),
pong = EventWaitHandle.OpenExisting(PongEventName);
ping.Set();
pong.WaitOne(testTimeout);
}
}
private string GetPowerRequests(string? expectedReason = null)
{
string pwrRequests = ProcessHelper.RunAndReadOutput("powercfg", "/requests");
Output.WriteLine(pwrRequests); // Useful to analyse failing tests.
string fileName = Process.GetCurrentProcess().MainModule.FileName;
string mustEndWith = fileName.Substring(Path.GetPathRoot(fileName).Length);
return string.Join(", ",
from pr in PowerRequestsParser.Parse(pwrRequests)
where
pr.RequesterName.EndsWith(mustEndWith, StringComparison.InvariantCulture) &&
string.Equals(pr.RequesterType, "PROCESS", StringComparison.InvariantCulture) &&
(expectedReason == null || string.Equals(pr.Reason, expectedReason, StringComparison.InvariantCulture))
select pr.RequestType);
}
private Task AsTask(WaitHandle waitHandle, TimeSpan timeout)
{
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
RegisteredWaitHandle rwh = null;
rwh = ThreadPool.RegisterWaitForSingleObject(
waitHandle,
(object state, bool timedOut) =>
{
rwh.Unregister(null);
if (timedOut)
{
tcs.SetException(new TimeoutException());
}
else
{
tcs.SetResult(true);
}
},
null,
timeout,
true);
return tcs.Task;
}
}