Skip to content

Commit 69d72a2

Browse files
eabaArkatufus
andauthored
Port Akka.Tests.Event tests to async/await - EventStreamSpec (#5794)
* Port `Akka.Tests.Event` tests to `async/await` - `EventStreamSpec` * Revert `ForEach` await * Changed the last `XAssert` to `Assert` Co-authored-by: Gregorius Soedharmo <[email protected]>
1 parent 3a22cd0 commit 69d72a2

File tree

1 file changed

+44
-45
lines changed

1 file changed

+44
-45
lines changed

src/core/Akka.Tests/Event/EventStreamSpec.cs

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Linq;
1515
using Akka.Util.Internal;
1616
using Xunit;
17+
using System.Threading.Tasks;
1718

1819
namespace Akka.Tests.Event
1920
{
@@ -61,25 +62,25 @@ private class CC { }
6162
private class CCATBT : CC, ATT, BTT { }
6263

6364
[Fact]
64-
public void Manage_subscriptions()
65+
public async Task Manage_subscriptions()
6566
{
6667

6768
var bus = new EventStream(true);
6869
bus.StartUnsubscriber(Sys.AsInstanceOf<ActorSystemImpl>());
6970
bus.Subscribe(TestActor, typeof(M));
7071

7172
bus.Publish(new M { Value = 42 });
72-
ExpectMsg(new M { Value = 42 });
73+
await ExpectMsgAsync(new M { Value = 42 });
7374
bus.Unsubscribe(TestActor);
7475
bus.Publish(new M { Value = 43 });
75-
ExpectNoMsg(TimeSpan.FromSeconds(1));
76+
await ExpectNoMsgAsync(TimeSpan.FromSeconds(1));
7677
}
7778

7879
[Fact]
7980
public void Not_allow_null_as_subscriber()
8081
{
8182
var bus = new EventStream(true);
82-
XAssert.Throws<ArgumentNullException>(() =>
83+
Assert.Throws<ArgumentNullException>(() =>
8384
{
8485
bus.Subscribe(null, typeof(M));
8586
});
@@ -89,18 +90,18 @@ public void Not_allow_null_as_subscriber()
8990
public void Not_allow_null_as_unsubscriber()
9091
{
9192
var bus = new EventStream(true);
92-
XAssert.Throws<ArgumentNullException>(() =>
93+
Assert.Throws<ArgumentNullException>(() =>
9394
{
9495
bus.Unsubscribe(null, typeof(M));
9596
});
96-
XAssert.Throws<ArgumentNullException>(() =>
97+
Assert.Throws<ArgumentNullException>(() =>
9798
{
9899
bus.Unsubscribe(null);
99100
});
100101
}
101102

102103
[Fact]
103-
public void Be_able_to_log_unhandled_messages()
104+
public async Task Be_able_to_log_unhandled_messages()
104105
{
105106
using (var system = ActorSystem.Create("EventStreamSpecUnhandled", GetDebugUnhandledMessagesConfig()))
106107
{
@@ -110,7 +111,7 @@ public void Be_able_to_log_unhandled_messages()
110111

111112
system.EventStream.Publish(msg);
112113

113-
var debugMsg = ExpectMsg<Debug>();
114+
var debugMsg = await ExpectMsgAsync<Debug>();
114115

115116
debugMsg.Message.ToString().StartsWith("Unhandled message from").ShouldBeTrue();
116117
debugMsg.Message.ToString().EndsWith(": 42").ShouldBeTrue();
@@ -121,7 +122,7 @@ public void Be_able_to_log_unhandled_messages()
121122
/// Reproduction spec for https://github.com/akkadotnet/akka.net/issues/3267
122123
/// </summary>
123124
[Fact]
124-
public void Bugfix3267_able_to_log_unhandled_messages_with_nosender()
125+
public async Task Bugfix3267_able_to_log_unhandled_messages_with_nosender()
125126
{
126127
using (var system = ActorSystem.Create("EventStreamSpecUnhandled", GetDebugUnhandledMessagesConfig()))
127128
{
@@ -132,15 +133,15 @@ public void Bugfix3267_able_to_log_unhandled_messages_with_nosender()
132133

133134
system.EventStream.Publish(msg);
134135

135-
var debugMsg = ExpectMsg<Debug>();
136+
var debugMsg = await ExpectMsgAsync<Debug>();
136137

137138
debugMsg.Message.ToString().StartsWith("Unhandled message from").ShouldBeTrue();
138139
debugMsg.Message.ToString().EndsWith(": 42").ShouldBeTrue();
139140
}
140141
}
141142

142143
[Fact]
143-
public void Manage_sub_channels_using_classes()
144+
public async Task Manage_sub_channels_using_classes()
144145
{
145146
var a = new A();
146147
var b1 = new B1();
@@ -150,24 +151,24 @@ public void Manage_sub_channels_using_classes()
150151
bus.Subscribe(TestActor, typeof(B2));
151152
bus.Publish(c);
152153
bus.Publish(b2);
153-
ExpectMsg(b2);
154+
await ExpectMsgAsync(b2);
154155
bus.Subscribe(TestActor, typeof(A));
155156
bus.Publish(c);
156-
ExpectMsg(c);
157+
await ExpectMsgAsync(c);
157158
bus.Publish(b1);
158-
ExpectMsg(b1);
159+
await ExpectMsgAsync(b1);
159160

160161
bus.Unsubscribe(TestActor, typeof(B1));
161162
bus.Publish(c); //should not publish
162163
bus.Publish(b2); //should publish
163164
bus.Publish(a); //should publish
164-
ExpectMsg(b2);
165-
ExpectMsg(a);
166-
ExpectNoMsg(TimeSpan.FromSeconds(1));
165+
await ExpectMsgAsync(b2);
166+
await ExpectMsgAsync(a);
167+
await ExpectNoMsgAsync(TimeSpan.FromSeconds(1));
167168
}
168169

169170
[Fact(DisplayName = "manage sub-channels using classes and traits (update on subscribe)")]
170-
public void Manage_sub_channels_using_classes_and_interfaces_update_on_subscribe()
171+
public async Task Manage_sub_channels_using_classes_and_interfaces_update_on_subscribe()
171172
{
172173
var es = new EventStream(false);
173174
var tm1 = new CC();
@@ -183,11 +184,11 @@ public void Manage_sub_channels_using_classes_and_interfaces_update_on_subscribe
183184
es.Subscribe(a4.Ref, typeof(CCATBT)).ShouldBeTrue();
184185
es.Publish(tm1);
185186
es.Publish(tm2);
186-
a1.ExpectMsg((object)tm2);
187-
a2.ExpectMsg((object)tm2);
188-
a3.ExpectMsg((object)tm1);
189-
a3.ExpectMsg((object)tm2);
190-
a4.ExpectMsg((object)tm2);
187+
await a1.ExpectMsgAsync((object)tm2);
188+
await a2.ExpectMsgAsync((object)tm2);
189+
await a3.ExpectMsgAsync((object)tm1);
190+
await a3.ExpectMsgAsync((object)tm2);
191+
await a4.ExpectMsgAsync((object)tm2);
191192
es.Unsubscribe(a1.Ref, typeof(AT)).ShouldBeTrue();
192193
es.Unsubscribe(a2.Ref, typeof(BT)).ShouldBeTrue();
193194
es.Unsubscribe(a3.Ref, typeof(CC)).ShouldBeTrue();
@@ -196,7 +197,7 @@ public void Manage_sub_channels_using_classes_and_interfaces_update_on_subscribe
196197

197198
//"manage sub-channels using classes and traits (update on unsubscribe)"
198199
[Fact]
199-
public void Manage_sub_channels_using_classes_and_interfaces_update_on_unsubscribe()
200+
public async Task Manage_sub_channels_using_classes_and_interfaces_update_on_unsubscribe()
200201
{
201202
var es = new EventStream(false);
202203
var tm1 = new CC();
@@ -213,18 +214,18 @@ public void Manage_sub_channels_using_classes_and_interfaces_update_on_unsubscri
213214
es.Unsubscribe(a3.Ref, typeof(CC));
214215
es.Publish(tm1);
215216
es.Publish(tm2);
216-
a1.ExpectMsg((object)tm2);
217-
a2.ExpectMsg((object)tm2);
218-
a3.ExpectNoMsg(TimeSpan.FromSeconds(1));
219-
a4.ExpectMsg((object)tm2);
217+
await a1.ExpectMsgAsync((object)tm2);
218+
await a2.ExpectMsgAsync((object)tm2);
219+
await a3.ExpectNoMsgAsync(TimeSpan.FromSeconds(1));
220+
await a4.ExpectMsgAsync((object)tm2);
220221
es.Unsubscribe(a1.Ref, typeof(AT)).ShouldBeTrue();
221222
es.Unsubscribe(a2.Ref, typeof(BT)).ShouldBeTrue();
222223
es.Unsubscribe(a3.Ref, typeof(CC)).ShouldBeFalse();
223224
es.Unsubscribe(a4.Ref, typeof(CCATBT)).ShouldBeTrue();
224225
}
225226

226227
[Fact]
227-
public void Manage_sub_channels_using_classes_and_interfaces_update_on_unsubscribe_all()
228+
public async Task Manage_sub_channels_using_classes_and_interfaces_update_on_unsubscribe_all()
228229
{
229230
var es = new EventStream(false);
230231
var tm1 = new CC();
@@ -241,10 +242,10 @@ public void Manage_sub_channels_using_classes_and_interfaces_update_on_unsubscri
241242
es.Unsubscribe(a3.Ref).ShouldBeTrue();
242243
es.Publish(tm1);
243244
es.Publish(tm2);
244-
a1.ExpectMsg((object)tm2);
245-
a2.ExpectMsg((object)tm2);
246-
a3.ExpectNoMsg(TimeSpan.FromSeconds(1));
247-
a4.ExpectMsg((object)tm2);
245+
await a1.ExpectMsgAsync((object)tm2);
246+
await a2.ExpectMsgAsync((object)tm2);
247+
await a3.ExpectNoMsgAsync(TimeSpan.FromSeconds(1));
248+
await a4.ExpectMsgAsync((object)tm2);
248249
es.Unsubscribe(a1.Ref, typeof(AT)).ShouldBeTrue();
249250
es.Unsubscribe(a2.Ref, typeof(BT)).ShouldBeTrue();
250251
es.Unsubscribe(a3.Ref, typeof(CC)).ShouldBeFalse();
@@ -262,12 +263,12 @@ public SetTarget(IActorRef @ref)
262263
}
263264

264265
[Fact]
265-
public void Manage_log_levels()
266+
public async Task Manage_log_levels()
266267
{
267268
var bus = new EventStream(false);
268269
bus.StartDefaultLoggers((ActorSystemImpl)Sys);
269270
bus.Publish(new SetTarget(TestActor));
270-
ExpectMsg("OK", TimeSpan.FromSeconds(5));
271+
await ExpectMsgAsync("OK", TimeSpan.FromSeconds(5));
271272

272273
verifyLevel(bus, LogLevel.InfoLevel);
273274
bus.SetLogLevel(LogLevel.WarningLevel);
@@ -304,28 +305,26 @@ private static string GetDebugUnhandledMessagesConfig()
304305
".Replace("%logger%", typeof(MyLog).AssemblyQualifiedName);
305306
}
306307

307-
public class MyLog : UntypedActor
308+
public class MyLog : ReceiveActor
308309
{
309310
private IActorRef dst = Context.System.DeadLetters;
310-
311-
protected override void OnReceive(object message)
311+
public MyLog()
312312
{
313-
PatternMatch.Match(message)
314-
.With<InitializeLogger>(m =>
313+
Receive<InitializeLogger>(m =>
315314
{
316315
var bus = m.LoggingBus;
317316
bus.Subscribe(this.Self, typeof(SetTarget));
318317
bus.Subscribe(this.Self, typeof(UnhandledMessage));
319318

320319
Sender.Tell(new LoggerInitialized());
321-
})
322-
.With<SetTarget>(m =>
320+
});
321+
Receive<SetTarget>(m =>
323322
{
324323
dst = m.Ref;
325324
dst.Tell("OK");
326-
})
327-
.With<LogEvent>(m => dst.Tell(m))
328-
.With<UnhandledMessage>(m => dst.Tell(m));
325+
});
326+
Receive<LogEvent>(m => dst.Tell(m));
327+
Receive<UnhandledMessage>(m => dst.Tell(m));
329328
}
330329
}
331330

0 commit comments

Comments
 (0)