-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ManagementEventWatcherTests.cs
75 lines (65 loc) · 2.93 KB
/
ManagementEventWatcherTests.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Threading;
using Xunit;
namespace System.Management.Tests
{
[Collection("Mof Collection")]
public class ManagementEventWatcherTests
{
private const string Query = "SELECT * FROM __TimerEvent WHERE TimerID = 'MyEvent'";
[ConditionalFact(typeof(WmiTestHelper), nameof(WmiTestHelper.IsElevatedAndSupportsWmi))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34689", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[OuterLoop]
public void Receive_Events_Sync()
{
ManagementEventWatcher watcher = null;
try
{
// Setup: Timer event is already set up by mof file to fire every 0.1 seconds.
// Run: Subscribe __TimerEvent with the specified TimerID
watcher = new ManagementEventWatcher(
WmiTestHelper.Namespace,
Query,
new EventWatcherOptions(null, TimeSpan.FromSeconds(5), 1));
ManagementBaseObject evt = watcher.WaitForNextEvent();
Assert.True(evt["TimerID"].ToString() == "MyEvent", $"Unexpected TimerID value {evt["TimerID"]}");
}
finally
{
if (watcher != null)
watcher.Stop();
}
}
[ConditionalFact(typeof(WmiTestHelper), nameof(WmiTestHelper.IsElevatedAndSupportsWmi))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34689", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[OuterLoop]
public void Receive_Events_Async()
{
ManagementEventWatcher watcher = null;
var resetEvent = new ManualResetEvent(false);
try
{
// Setup: Timer event is already set up by mof file to fire every 0.1 seconds.
// Run: Subscribe __TimerEvent with the specified TimerID
watcher = new ManagementEventWatcher(
WmiTestHelper.Namespace,
Query,
new EventWatcherOptions(null, TimeSpan.FromSeconds(5), 1));
watcher.EventArrived += (object sender, EventArrivedEventArgs args) =>
{
ManagementBaseObject newEvent = args.NewEvent;
Assert.True(newEvent["TimerID"].ToString() == "MyEvent", $"Unexpected TimerID value {newEvent["TimerID"]}");
resetEvent.Set();
};
watcher.Start();
Assert.True(resetEvent.WaitOne(TimeSpan.FromSeconds(5), true), "Timeout waiting for receive event.");
}
finally
{
if (watcher != null)
watcher.Stop();
}
}
}
}