Skip to content

Commit 334a0da

Browse files
authored
Add predicates for system message expectations (#2312)
1 parent b8b2af9 commit 334a0da

File tree

7 files changed

+31
-31
lines changed

7 files changed

+31
-31
lines changed

src/Proto.TestKit/TestProbeSystemMessageExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ public static class TestProbeSystemMessageExtensions
1616
public static Task<T> ExpectSystemMessageAsync<T>(this ITestProbe probe, TimeSpan? timeAllowed = null,
1717
CancellationToken cancellationToken = default)
1818
where T : SystemMessage => probe.GetNextMessageAsync<T>(timeAllowed, cancellationToken);
19+
20+
/// <summary>
21+
/// Asynchronously retrieves the next system message of type <typeparamref name="T"/> that satisfies
22+
/// the given predicate.
23+
/// </summary>
24+
public static Task<T> ExpectSystemMessageAsync<T>(this ITestProbe probe, Func<T, bool> when,
25+
TimeSpan? timeAllowed = null, CancellationToken cancellationToken = default)
26+
where T : SystemMessage => probe.GetNextMessageAsync(when, timeAllowed, cancellationToken);
1927
}

tests/Proto.Actor.Tests/SchedulerTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public async Task SendOnceWorksWithTimeProvider()
2424
scheduler.SendOnce(TimeSpan.FromSeconds(10), pid, "Wakeup");
2525
await hook.WaitAsync();
2626
timeProvider.Advance(TimeSpan.FromMinutes(10));
27-
var msg = await probe.GetNextMessageAsync<string>(TimeSpan.FromMilliseconds(10));
28-
Assert.Equal("Wakeup", msg);
27+
await probe.GetNextMessageAsync<string>(x => x == "Wakeup", TimeSpan.FromMilliseconds(100));
2928
}
3029

3130
[Fact]
@@ -43,7 +42,7 @@ public async Task SendRepeatedlyCanBeCancelled()
4342

4443
await hook.WaitAsync();
4544
timeProvider.Advance(TimeSpan.FromMinutes(1));
46-
await probe.GetNextMessageAsync<string>(TimeSpan.FromMilliseconds(10));
45+
await probe.GetNextMessageAsync<string>(TimeSpan.FromMilliseconds(100));
4746

4847
cts.Cancel();
4948
timeProvider.Advance(TimeSpan.FromMinutes(1));
@@ -114,7 +113,7 @@ public async Task RequestRepeatedlyCanBeCancelled()
114113

115114
await hook.WaitAsync();
116115
timeProvider.Advance(TimeSpan.FromSeconds(5));
117-
await probe.GetNextMessageAsync<string>(TimeSpan.FromMilliseconds(10));
116+
await probe.GetNextMessageAsync<string>(TimeSpan.FromMilliseconds(100));
118117

119118
context.Send(requester, "Cancel");
120119

tests/Proto.Actor.Tests/TestProbeAsyncTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public async Task Probe_can_await_next_message()
2323
}));
2424

2525
system.Root.Send(target, "hello");
26-
var msg = await probe.GetNextMessageAsync<string>();
27-
Assert.Equal("hello", msg);
26+
await probe.GetNextMessageAsync<string>(s => s == "hello");
2827
}
2928

3029
[Fact]

tests/Proto.Actor.Tests/TimerExtensionsTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public async Task SchedulerSchedulesMessageAfterDelay()
2323
// ensure message isn't delivered immediately
2424
await probe.ExpectNoMessageAsync(TimeSpan.FromMilliseconds(100));
2525

26-
var msg = await probe.GetNextMessageAsync<string>(TimeSpan.FromSeconds(5));
27-
Assert.Equal("Wakeup", msg);
26+
await probe.GetNextMessageAsync<string>(s => s == "Wakeup", TimeSpan.FromSeconds(5));
2827
}
2928

3029
[Fact]
@@ -43,8 +42,7 @@ public async Task SchedulerWithTimeProviderSchedulesMessageAfterDelay()
4342
await Task.Delay(50);
4443
timeProvider.Advance(TimeSpan.FromMinutes(1));
4544

46-
var msg = await probe.GetNextMessageAsync<string>(TimeSpan.FromMilliseconds(10));
47-
Assert.Equal("Wakeup", msg);
45+
await probe.GetNextMessageAsync<string>(s => s == "Wakeup", TimeSpan.FromMilliseconds(100));
4846
}
4947
}
5048

tests/Proto.Actor.Tests/WatchTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task MultipleStopsTriggerSingleTerminated()
3636

3737
context.Send(child, "stop");
3838

39-
await probe.GetNextMessageAsync<Terminated>();
39+
await probe.ExpectSystemMessageAsync<Terminated>(t => Equals(t.Who, child));
4040
await probe.ExpectNoMessageAsync(TimeSpan.FromMilliseconds(100));
4141
}
4242

@@ -54,7 +54,7 @@ public async Task CanWatchLocalActors()
5454

5555
await context.StopAsync(watchee);
5656

57-
await probe.GetNextMessageAsync<Terminated>();
57+
await probe.ExpectSystemMessageAsync<Terminated>(t => Equals(t.Who, watchee));
5858
}
5959

6060
[Fact]

tests/Proto.Remote.Tests/RemoteTests.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,9 @@ public async Task CanWatchRemoteActor()
169169
probe.Context.Watch(remoteActor);
170170
await Task.Delay(20); // allow RemoteWatch to propagate
171171

172-
await System.Root.StopAsync(remoteActor);
172+
await System.Root.PoisonAsync(remoteActor);
173173

174-
var terminated = await probe.ExpectSystemMessageAsync<Terminated>(TimeSpan.FromSeconds(10));
175-
Assert.Equal(remoteActor, terminated.Who);
174+
await probe.ExpectSystemMessageAsync<Terminated>(t => Equals(t.Who, remoteActor), TimeSpan.FromSeconds(10));
176175
}
177176

178177
[Fact]
@@ -188,11 +187,13 @@ public async Task CanWatchMultipleRemoteActors()
188187
probe.Context.Watch(remoteActor2);
189188
await Task.Delay(20); // allow RemoteWatch to propagate
190189

191-
await System.Root.StopAsync(remoteActor1);
192-
await System.Root.StopAsync(remoteActor2);
190+
await System.Root.PoisonAsync(remoteActor1);
191+
await System.Root.PoisonAsync(remoteActor2);
193192

194-
var term1 = await probe.ExpectSystemMessageAsync<Terminated>(TimeSpan.FromSeconds(10));
195-
var term2 = await probe.ExpectSystemMessageAsync<Terminated>(TimeSpan.FromSeconds(10));
193+
var term1 = await probe.ExpectSystemMessageAsync<Terminated>(t =>
194+
Equals(t.Who, remoteActor1) || Equals(t.Who, remoteActor2), TimeSpan.FromSeconds(10));
195+
var term2 = await probe.ExpectSystemMessageAsync<Terminated>(t =>
196+
Equals(t.Who, remoteActor1) || Equals(t.Who, remoteActor2), TimeSpan.FromSeconds(10));
196197
new[] { term1.Who, term2.Who }.Should().BeEquivalentTo(new[] { remoteActor1, remoteActor2 });
197198
}
198199

@@ -209,12 +210,10 @@ public async Task MultipleLocalActorsCanWatchRemoteActor()
209210
probe2.Context.Watch(remoteActor);
210211
await Task.Delay(20); // allow RemoteWatch to propagate
211212

212-
await System.Root.StopAsync(remoteActor);
213+
await System.Root.PoisonAsync(remoteActor);
213214

214-
var t1 = await probe1.ExpectSystemMessageAsync<Terminated>(TimeSpan.FromSeconds(10));
215-
var t2 = await probe2.ExpectSystemMessageAsync<Terminated>(TimeSpan.FromSeconds(10));
216-
Assert.Equal(remoteActor, t1.Who);
217-
Assert.Equal(remoteActor, t2.Who);
215+
await probe1.ExpectSystemMessageAsync<Terminated>(t => Equals(t.Who, remoteActor), TimeSpan.FromSeconds(10));
216+
await probe2.ExpectSystemMessageAsync<Terminated>(t => Equals(t.Who, remoteActor), TimeSpan.FromSeconds(10));
218217
}
219218

220219
[Fact]
@@ -233,10 +232,9 @@ public async Task CanUnwatchRemoteActor()
233232
probe2.Context.Unwatch(remoteActor);
234233
await Task.Delay(TimeSpan.FromSeconds(3));
235234

236-
await System.Root.StopAsync(remoteActor);
235+
await System.Root.PoisonAsync(remoteActor);
237236

238-
var term = await probe1.ExpectSystemMessageAsync<Terminated>(TimeSpan.FromSeconds(10));
239-
Assert.Equal(remoteActor, term.Who);
237+
await probe1.ExpectSystemMessageAsync<Terminated>(t => Equals(t.Who, remoteActor), TimeSpan.FromSeconds(10));
240238

241239
await probe2.ExpectNoMessageAsync(TimeSpan.FromSeconds(1));
242240
}
@@ -254,8 +252,7 @@ public async Task WhenRemoteTerminated_LocalWatcherReceivesNotification()
254252

255253
System.Root.Send(remoteActor, new Die());
256254

257-
var term = await probe.ExpectSystemMessageAsync<Terminated>(TimeSpan.FromSeconds(10));
258-
Assert.Equal(remoteActor, term.Who);
255+
await probe.ExpectSystemMessageAsync<Terminated>(t => Equals(t.Who, remoteActor), TimeSpan.FromSeconds(10));
259256

260257
await probe.ExpectNoMessageAsync(TimeSpan.FromMilliseconds(200));
261258
}

tests/Proto.TestKit.Tests/TestProbeAsyncTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ public async Task GetNextMessageAsync_returns_message()
1515
var (probe, pid) = system.CreateTestProbe();
1616

1717
system.Root.Send(pid, "hello");
18-
var msg = await probe.GetNextMessageAsync<string>();
19-
msg.Should().Be("hello");
18+
await probe.GetNextMessageAsync<string>(s => s == "hello");
2019
}
2120

2221
[Fact]

0 commit comments

Comments
 (0)