Skip to content

Commit c12cda7

Browse files
authored
Refactor tests and code for consistency, readability, and minor improvements. No functional changes. (#6)
1 parent 79e863e commit c12cda7

4 files changed

Lines changed: 48 additions & 41 deletions

File tree

src/WeakEvent.Tests/WeakEventGenericTests.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-

2-
namespace ByteAether.WeakEvent.Tests;
1+
namespace ByteAether.WeakEvent.Tests;
32

43
public class WeakEventGenericTests
54
{
@@ -21,7 +20,7 @@ public void Unsubscribe_NullHandler_ThrowsArgumentNullException()
2120
public void Unsubscribe_NonExistentHandler_DoesNotThrow()
2221
{
2322
var weakEvent = new WeakEvent<string>();
24-
Action<string> handler = msg => { };
23+
Action<string> handler = _ => { };
2524
var exception = Record.Exception(() => Assert.False(weakEvent.Unsubscribe(handler)));
2625
Assert.Null(exception);
2726
}
@@ -32,12 +31,12 @@ public async Task PublishAsync_WithCancelledToken_ThrowsOperationCanceledExcepti
3231
var weakEvent = new WeakEvent<string>();
3332
var handlerInvoked = false;
3433
// Subscribe a handler that would set the flag to true.
35-
weakEvent.Subscribe(eventData => handlerInvoked = true);
34+
weakEvent.Subscribe(_ => handlerInvoked = true);
3635
// Cancel the token immediately so that any wait operation observes the cancellation
3736
using var cts = new CancellationTokenSource();
38-
cts.Cancel();
37+
await cts.CancelAsync();
3938

40-
// Assert that publishing an event with the cancelled token throws the expected exception.
39+
// Assert that publishing an event with the canceled token throws the expected exception.
4140
await Assert.ThrowsAnyAsync<OperationCanceledException>(
4241
() => weakEvent.PublishAsync("Test event", cts.Token)
4342
);
@@ -53,12 +52,14 @@ void syncHandler(int x)
5352
{
5453
count += 1 * x;
5554
}
55+
5656
async Task asyncHandler(int x)
5757
{
5858
count += 2 * x;
5959
await Task.CompletedTask;
6060
}
61-
async Task asyncHandlerCT(int x, CancellationToken ct)
61+
62+
async Task asyncHandlerCt(int x, CancellationToken ct)
6263
{
6364
count += 4 * x;
6465
await Task.CompletedTask;
@@ -68,11 +69,11 @@ async Task asyncHandlerCT(int x, CancellationToken ct)
6869

6970
weakEvent.Subscribe(syncHandler);
7071
weakEvent.Subscribe(asyncHandler);
71-
weakEvent.Subscribe(asyncHandlerCT);
72+
weakEvent.Subscribe(asyncHandlerCt);
7273

7374
weakEvent.Unsubscribe(syncHandler);
7475
weakEvent.Unsubscribe(asyncHandler);
75-
weakEvent.Unsubscribe(asyncHandlerCT);
76+
weakEvent.Unsubscribe(asyncHandlerCt);
7677

7778
await weakEvent.PublishAsync(2);
7879

@@ -88,12 +89,14 @@ void syncHandler(int x)
8889
{
8990
count += 1 * x;
9091
}
92+
9193
async Task asyncHandler(int x)
9294
{
9395
count += 2 * x;
9496
await Task.CompletedTask;
9597
}
96-
async Task asyncHandlerCT(int x, CancellationToken ct)
98+
99+
async Task asyncHandlerCt(int x, CancellationToken ct)
97100
{
98101
count += 4 * x;
99102
await Task.CompletedTask;
@@ -103,7 +106,7 @@ async Task asyncHandlerCT(int x, CancellationToken ct)
103106

104107
weakEvent.Subscribe(syncHandler);
105108
weakEvent.Subscribe(asyncHandler);
106-
weakEvent.Subscribe(asyncHandlerCT);
109+
weakEvent.Subscribe(asyncHandlerCt);
107110

108111
await weakEvent.PublishAsync(2);
109112

@@ -123,13 +126,14 @@ static async Task createSubscriberAndInvoke(WeakEvent<string> weakEvent, Action
123126
await weakEvent.PublishAsync("Test");
124127
// The subscriber goes out of scope after this method, allowing it to be GC’d.
125128
}
129+
126130
await createSubscriberAndInvoke(weakEvent, () => callCount++);
127131

128132
// Force garbage collection to reclaim the subscriber instance.
129133
GC.Collect();
130134
GC.WaitForPendingFinalizers();
131135

132-
// Invoke second time
136+
// Invoke a second time
133137
await weakEvent.PublishAsync("Test");
134138

135139
// Just one call should happen instead of 2
@@ -146,6 +150,7 @@ public void MultipleHandlers_CorrectCount()
146150
// Stage 2 - Add local handler
147151
void syncHandler(string _)
148152
{ }
153+
149154
weakEvent.Subscribe(syncHandler);
150155

151156
// Stage 3 - Add a garbage-collected handler
@@ -155,6 +160,7 @@ void createSubscriberAndAssert()
155160
weakEvent.Subscribe(subscriber.Handler);
156161
Assert.Equal(2, weakEvent.SubscriberCount);
157162
}
163+
158164
createSubscriberAndAssert();
159165

160166
// Stage 4 - Force garbage collection to reclaim the garbage-collectable subscriber instance.
@@ -171,9 +177,6 @@ private class GenericSubscriber(Action onEvent)
171177
{
172178
private readonly Action _onEvent = onEvent;
173179

174-
public void Handler(string _)
175-
{
176-
_onEvent();
177-
}
180+
public void Handler(string _) => _onEvent();
178181
}
179-
}
182+
}

src/WeakEvent.Tests/WeakEventHandlerTests.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
namespace ByteAether.WeakEvent.Tests;
2+
23
public class WeakEventHandlerTests
34
{
45
[Fact]
@@ -27,10 +28,7 @@ public async Task StaticHandler_InvokeAsync_Void()
2728
}
2829

2930
private static bool _staticInvoked;
30-
private static void StaticHandler()
31-
{
32-
_staticInvoked = true;
33-
}
31+
private static void StaticHandler() => _staticInvoked = true;
3432

3533
[Fact]
3634
public async Task InstanceHandler_InvokeAsync_Void()
@@ -121,7 +119,7 @@ private static WeakEventHandler CreateWeakSubscriber()
121119
private class Subscriber
122120
{
123121
public bool Invoked { get; private set; }
124-
public bool ThrowIfInvoked { get; set; }
122+
public bool ThrowIfInvoked { get; init; }
125123

126124
public void InstanceHandler()
127125
{

src/WeakEvent.Tests/WeakEventNonGenericTests.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
namespace ByteAether.WeakEvent.Tests;
2+
23
public class WeakEventNonGenericTests
34
{
45
[Fact]
@@ -19,7 +20,7 @@ public void Unsubscribe_NullHandler_ThrowsArgumentNullException()
1920
public void Unsubscribe_NonExistentHandler_DoesNotThrow()
2021
{
2122
var weakEvent = new WeakEvent();
22-
Action handler = () => { };
23+
var handler = () => { };
2324
var exception = Record.Exception(() => Assert.False(weakEvent.Unsubscribe(handler)));
2425
Assert.Null(exception);
2526
}
@@ -33,9 +34,9 @@ public async Task PublishAsync_WithCancelledToken_ThrowsOperationCanceledExcepti
3334
weakEvent.Subscribe(() => handlerInvoked = true);
3435
// Cancel the token immediately so that any wait operation observes the cancellation
3536
using var cts = new CancellationTokenSource();
36-
cts.Cancel();
37+
await cts.CancelAsync();
3738

38-
// Assert that publishing an event with the cancelled token throws the expected exception.
39+
// Assert that publishing an event with the canceled token throws the expected exception.
3940
await Assert.ThrowsAnyAsync<OperationCanceledException>(
4041
() => weakEvent.PublishAsync(cts.Token)
4142
);
@@ -51,12 +52,14 @@ void syncHandler()
5152
{
5253
count += 1;
5354
}
55+
5456
async Task asyncHandler()
5557
{
5658
count += 2;
5759
await Task.CompletedTask;
5860
}
59-
async Task asyncHandlerCT(CancellationToken ct)
61+
62+
async Task asyncHandlerCt(CancellationToken ct)
6063
{
6164
count += 4;
6265
await Task.CompletedTask;
@@ -66,11 +69,11 @@ async Task asyncHandlerCT(CancellationToken ct)
6669

6770
weakEvent.Subscribe(syncHandler);
6871
weakEvent.Subscribe(asyncHandler);
69-
weakEvent.Subscribe(asyncHandlerCT);
72+
weakEvent.Subscribe(asyncHandlerCt);
7073

7174
weakEvent.Unsubscribe(syncHandler);
7275
weakEvent.Unsubscribe(asyncHandler);
73-
weakEvent.Unsubscribe(asyncHandlerCT);
76+
weakEvent.Unsubscribe(asyncHandlerCt);
7477

7578
await weakEvent.PublishAsync();
7679

@@ -86,12 +89,14 @@ void syncHandler()
8689
{
8790
count += 1;
8891
}
92+
8993
async Task asyncHandler()
9094
{
9195
count += 2;
9296
await Task.CompletedTask;
9397
}
94-
async Task asyncHandlerCT(CancellationToken ct)
98+
99+
async Task asyncHandlerCt(CancellationToken ct)
95100
{
96101
count += 4;
97102
await Task.CompletedTask;
@@ -101,7 +106,7 @@ async Task asyncHandlerCT(CancellationToken ct)
101106

102107
weakEvent.Subscribe(syncHandler);
103108
weakEvent.Subscribe(asyncHandler);
104-
weakEvent.Subscribe(asyncHandlerCT);
109+
weakEvent.Subscribe(asyncHandlerCt);
105110

106111
await weakEvent.PublishAsync();
107112

@@ -121,13 +126,14 @@ static async Task createSubscriberAndInvoke(WeakEvent weakEvent, Action onEvent)
121126
await weakEvent.PublishAsync();
122127
// The subscriber goes out of scope after this method, allowing it to be GC’d.
123128
}
129+
124130
await createSubscriberAndInvoke(weakEvent, () => callCount++);
125131

126132
// Force garbage collection to reclaim the subscriber instance.
127133
GC.Collect();
128134
GC.WaitForPendingFinalizers();
129135

130-
// Invoke second time
136+
// Invoke a second time
131137
await weakEvent.PublishAsync();
132138

133139
// Just one call should happen instead of 2
@@ -144,6 +150,7 @@ public void MultipleHandlers_CorrectCount()
144150
// Stage 2 - Add local handler
145151
void syncHandler()
146152
{ }
153+
147154
weakEvent.Subscribe(syncHandler);
148155
Assert.Equal(1, weakEvent.SubscriberCount);
149156

@@ -154,6 +161,7 @@ void createSubscriberAndAssert()
154161
weakEvent.Subscribe(subscriber.Handler);
155162
Assert.Equal(2, weakEvent.SubscriberCount);
156163
}
164+
157165
createSubscriberAndAssert();
158166

159167
// Stage 4 - Force garbage collection to reclaim the garbage-collectable subscriber instance.
@@ -170,9 +178,6 @@ private class NonGenericSubscriber(Action onEvent)
170178
{
171179
private readonly Action _onEvent = onEvent;
172180

173-
public void Handler()
174-
{
175-
_onEvent();
176-
}
181+
public void Handler() => _onEvent();
177182
}
178-
}
183+
}

src/WeakEvent/WeakEvent.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class WeakEvent<TEvent> : WeakEventBase
3232
public void Subscribe(Func<TEvent, Task> handler) => base.Subscribe(handler);
3333

3434
/// <summary>
35-
/// Subscribes the specified async handler with cancellation token to the event.
35+
/// Subscribes the specified async handler with a cancellation token to the event.
3636
/// </summary>
3737
/// <param name="handler">
3838
/// The handler to subscribe. It will be invoked when the event is raised,
@@ -68,7 +68,8 @@ public class WeakEvent<TEvent> : WeakEventBase
6868
/// </summary>
6969
/// <param name="eventData">The event data to publish to the subscribers.</param>
7070
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
71-
public Task PublishAsync(TEvent eventData, CancellationToken cancellationToken = default) => base.PublishAsync([eventData], cancellationToken);
71+
public Task PublishAsync(TEvent eventData, CancellationToken cancellationToken = default)
72+
=> base.PublishAsync([eventData], cancellationToken);
7273
}
7374

7475
/// <summary>
@@ -102,7 +103,7 @@ public class WeakEvent : WeakEventBase
102103
public void Subscribe(Func<Task> handler) => base.Subscribe(handler);
103104

104105
/// <summary>
105-
/// Subscribes the specified async handler with cancellation token to the event.
106+
/// Subscribes the specified async handler with a cancellation token to the event.
106107
/// </summary>
107108
/// <param name="handler">
108109
/// The handler to subscribe. It will be invoked when the event is raised,
@@ -150,7 +151,7 @@ public abstract class WeakEventBase
150151
private readonly ReaderWriterLockSlim _rwLock = new();
151152

152153
/// <summary>
153-
/// Number of alive subscribers currently registered to the event.
154+
/// Number of living subscribers currently registered to the event.
154155
/// </summary>
155156
public int SubscriberCount
156157
{
@@ -232,7 +233,7 @@ protected async Task PublishAsync(List<object?> args, CancellationToken cancella
232233
try
233234
{
234235
cancellationToken.ThrowIfCancellationRequested();
235-
236+
236237
if (_handlers.Any(x => !x.IsAlive))
237238
{
238239
_rwLock.EnterWriteLock();

0 commit comments

Comments
 (0)