-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathEventManagerConcurrencyTests.cs
More file actions
87 lines (73 loc) · 2.7 KB
/
Copy pathEventManagerConcurrencyTests.cs
File metadata and controls
87 lines (73 loc) · 2.7 KB
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
using System;
using System.Threading;
using System.Threading.Tasks;
using GeneralUpdate.Core.Event;
using Xunit;
namespace CoreTest.Event;
public class EventManagerConcurrencyTests
{
[Fact]
public async Task ConcurrentAddRemoveDispatch_NoExceptions()
{
var completed = new TaskCompletionSource<bool>();
var errors = 0;
// Concurrent writers
Parallel.For(0, 100, i =>
{
try
{
Action<object, ExceptionEventArgs> handler = (s, e) => { };
EventManager.Instance.AddListener(handler);
EventManager.Instance.RemoveListener(handler);
}
catch { Interlocked.Increment(ref errors); }
});
// Concurrent dispatchers
Parallel.For(0, 50, i =>
{
try
{
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(new Exception("test"), "test"));
}
catch { Interlocked.Increment(ref errors); }
});
// Concurrent readers (dispatch + add)
Parallel.For(0, 50, i =>
{
try
{
Action<object, ProgressEventArgs> handler = (s, e) => { };
EventManager.Instance.AddListener(handler);
EventManager.Instance.RemoveListener(handler);
}
catch { Interlocked.Increment(ref errors); }
});
await Task.Delay(100); // Let all tasks finish
Assert.Equal(0, errors);
}
[Fact]
public void Dispatch_HandlerException_DoesNotBlockOthers()
{
var handler2Called = false;
Action<object, ExceptionEventArgs> handler1 = (s, e) => throw new InvalidOperationException("boom");
Action<object, ExceptionEventArgs> handler2 = (s, e) => handler2Called = true;
EventManager.Instance.AddListener(handler1);
EventManager.Instance.AddListener(handler2);
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(null, "test"));
Assert.True(handler2Called);
// Cleanup
EventManager.Instance.RemoveListener(handler1);
EventManager.Instance.RemoveListener(handler2);
}
[Fact]
public void AddRemove_Dispatch_DoesNotThrow()
{
var callCount = 0;
Action<object, ExceptionEventArgs> handler = (s, e) => Interlocked.Increment(ref callCount);
EventManager.Instance.AddListener(handler);
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(null, "test"));
EventManager.Instance.RemoveListener(handler);
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(null, "test"));
Assert.Equal(1, callCount); // Only first dispatch should trigger
}
}