Skip to content

Commit 352971a

Browse files
Merge pull request #118 from EvotecIT/empty-_watcheventid-on-dispose
2 parents 11cc1fb + 335a200 commit 352971a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Reflection;
5+
using Xunit;
6+
7+
namespace EventViewerX.Tests {
8+
public class TestWatchEvents {
9+
private static ConcurrentBag<int> GetIds(WatchEvents watcher) {
10+
var field = typeof(WatchEvents).GetField("_watchEventId", BindingFlags.NonPublic | BindingFlags.Instance);
11+
Assert.NotNull(field);
12+
return (ConcurrentBag<int>)field!.GetValue(watcher)!;
13+
}
14+
15+
[Fact]
16+
public void DisposeClearsWatchEventIds() {
17+
var watcher = new WatchEvents();
18+
watcher.Watch(Environment.MachineName, "Application", new List<int> { 1 });
19+
watcher.Dispose();
20+
var ids = GetIds(watcher);
21+
Assert.Empty(ids);
22+
}
23+
24+
[Fact]
25+
public void SubsequentWatchesUseNewIdsOnly() {
26+
var watcher = new WatchEvents();
27+
watcher.Watch(Environment.MachineName, "Application", new List<int> { 1 });
28+
watcher.Watch(Environment.MachineName, "Application", new List<int> { 2 });
29+
var ids = GetIds(watcher);
30+
Assert.DoesNotContain(1, ids);
31+
Assert.Contains(2, ids);
32+
}
33+
}
34+
}

Sources/EventViewerX/WatchEvents.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public void Dispose() {
8383
_eventLogWatcher.Dispose();
8484
_eventLogWatcher = null;
8585
}
86+
_watchEventId = new ConcurrentBag<int>();
8687
_eventLogSession?.Dispose();
8788
_eventLogSession = null;
8889
}

0 commit comments

Comments
 (0)