/
ActorLifeCycleSpec.cs
411 lines (358 loc) · 14.6 KB
/
ActorLifeCycleSpec.cs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//-----------------------------------------------------------------------
// <copyright file="ActorLifeCycleSpec.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.TestKit;
using Akka.Tests.TestUtils;
using Akka.Util.Internal;
using Xunit;
namespace Akka.Tests
{
public class ActorLifeCycleSpec : AkkaSpec
{
public class LifeCycleTestActor : UntypedActor
{
private AtomicCounter generationProvider;
private string id;
private IActorRef testActor;
private int CurrentGeneration;
public LifeCycleTestActor(IActorRef testActor,string id,AtomicCounter generationProvider)
{
this.testActor = testActor;
this.id = id;
this.generationProvider = generationProvider;
this.CurrentGeneration = generationProvider.Next();
}
private void Report(object message)
{
testActor.Tell(((string)message,id,CurrentGeneration));
}
protected override void OnReceive(object message)
{
if (message is string s && s == "status")
{
testActor.Tell(("OK",id,CurrentGeneration));
}
}
protected override void PostStop()
{
Report("postStop");
}
protected override void PreStart()
{
Report("preStart");
}
protected override void PreRestart(Exception cause, object message)
{
Report("preRestart");
}
protected override void PostRestart(Exception cause)
{
Report("postRestart");
}
}
public class LifeCycleTest2Actor : UntypedActor
{
private AtomicCounter generationProvider;
private string id;
private IActorRef testActor;
private int CurrentGeneration;
public LifeCycleTest2Actor(IActorRef testActor, string id, AtomicCounter generationProvider)
{
this.testActor = testActor;
this.id = id;
this.generationProvider = generationProvider;
this.CurrentGeneration = generationProvider.Next();
}
private void Report(object message)
{
testActor.Tell(((string)message, id, CurrentGeneration));
}
protected override void OnReceive(object message)
{
if (message is string s && s == "status")
{
testActor.Tell(("OK", id, CurrentGeneration));
}
}
protected override void PostStop()
{
Report("postStop");
}
protected override void PreStart()
{
Report("preStart");
}
}
[Fact(DisplayName = "invoke preRestart, preStart, postRestart when using OneForOneStrategy")]
public async Task Actor_lifecycle_test1()
{
var generationProvider = new AtomicCounter();
string id = Guid.NewGuid().ToString();
var supervisor = Sys.ActorOf(Props.Create(() => new Supervisor(new OneForOneStrategy(3, TimeSpan.FromSeconds(1000), x => Directive.Restart))));
var restarterProps = Props.Create(() => new LifeCycleTestActor(TestActor, id, generationProvider));
var restarter = await supervisor.Ask<IActorRef>(restarterProps);
await ExpectMsgAsync(("preStart", id, 0));
restarter.Tell(Kill.Instance);
await ExpectMsgAsync(("preRestart", id, 0));
await ExpectMsgAsync(("postRestart", id, 1));
restarter.Tell("status");
await ExpectMsgAsync(("OK", id, 1));
restarter.Tell(Kill.Instance);
await ExpectMsgAsync(("preRestart", id, 1));
await ExpectMsgAsync(("postRestart", id, 2));
restarter.Tell("status");
await ExpectMsgAsync(("OK", id, 2));
restarter.Tell(Kill.Instance);
await ExpectMsgAsync(("preRestart", id, 2));
await ExpectMsgAsync(("postRestart", id, 3));
restarter.Tell("status");
await ExpectMsgAsync(("OK", id, 3));
restarter.Tell(Kill.Instance);
await ExpectMsgAsync(("postStop", id, 3));
await ExpectNoMsgAsync(TimeSpan.FromSeconds(1));
Sys.Stop(supervisor);
}
[Fact(DisplayName="default for preRestart and postRestart is to call postStop and preStart respectively")]
public async Task Actor_lifecycle_test2()
{
var generationProvider = new AtomicCounter();
string id = Guid.NewGuid().ToString();
var supervisor = Sys.ActorOf(Props.Create(() => new Supervisor(new OneForOneStrategy(3, TimeSpan.FromSeconds(1000), x => Directive.Restart))));
var restarterProps = Props.Create(() => new LifeCycleTest2Actor(TestActor, id, generationProvider));
var restarter = await supervisor.Ask<IActorRef>(restarterProps);
await ExpectMsgAsync(("preStart", id, 0));
restarter.Tell(Kill.Instance);
await ExpectMsgAsync(("postStop", id, 0));
await ExpectMsgAsync(("preStart", id, 1));
restarter.Tell("status");
await ExpectMsgAsync(("OK", id, 1));
restarter.Tell(Kill.Instance);
await ExpectMsgAsync(("postStop", id, 1));
await ExpectMsgAsync(("preStart", id, 2));
restarter.Tell("status");
await ExpectMsgAsync(("OK", id, 2));
restarter.Tell(Kill.Instance);
await ExpectMsgAsync(("postStop", id, 2));
await ExpectMsgAsync(("preStart", id, 3));
restarter.Tell("status");
await ExpectMsgAsync(("OK", id, 3));
restarter.Tell(Kill.Instance);
await ExpectMsgAsync(("postStop", id, 3));
await ExpectNoMsgAsync(TimeSpan.FromSeconds(1));
Sys.Stop(supervisor);
}
[Fact(DisplayName="not invoke preRestart and postRestart when never restarted using OneForOneStrategy")]
public async Task Actor_lifecycle_test3()
{
var generationProvider = new AtomicCounter();
string id = Guid.NewGuid().ToString();
var supervisor = Sys.ActorOf(Props.Create(() => new Supervisor(new OneForOneStrategy(3, TimeSpan.FromSeconds(1000), x => Directive.Restart))));
var restarterProps = Props.Create(() => new LifeCycleTest2Actor(TestActor, id, generationProvider));
var restarter = await supervisor.Ask<IInternalActorRef>(restarterProps);
await ExpectMsgAsync(("preStart", id, 0));
restarter.Tell("status");
await ExpectMsgAsync(("OK", id, 0));
restarter.Stop();
await ExpectMsgAsync(("postStop", id, 0));
await ExpectNoMsgAsync(TimeSpan.FromSeconds(1));
}
public class EmptyActor : UntypedActor
{
protected override void OnReceive(object message)
{
}
protected override void PostStop()
{
throw new Exception("hurrah");
}
}
[Fact(DisplayName="log failures in postStop")]
public async Task Log_failures_in_PostStop()
{
var a = Sys.ActorOf<EmptyActor>();
await EventFilter.Exception<Exception>(message: "hurrah").ExpectOneAsync(() => {
a.Tell(PoisonPill.Instance);
return Task.CompletedTask;
});
}
public class Become
{
}
public class BecomeActor : UntypedActor
{
private readonly IActorRef _testActor;
public BecomeActor(IActorRef testActor)
{
_testActor = testActor;
}
protected override void OnReceive(object message)
{
if (message is Become)
{
Context.Become(OnBecome);
_testActor.Tell("ok");
}
else
{
_testActor.Tell(42);
}
}
protected void OnBecome(object message)
{
if (message.ToString() == "fail")
{
throw new Exception("buh");
}
else
{
_testActor.Tell(43);
}
}
}
[Fact]
public async Task Clear_behavior_stack_upon_restart()
{
var a = Sys.ActorOf(Props.Create(() => new BecomeActor(TestActor)));
a.Tell("hello");
await ExpectMsgAsync(42);
a.Tell(new Become());
await ExpectMsgAsync("ok");
a.Tell("hello");
await ExpectMsgAsync(43);
await EventFilter.Exception<Exception>("buh").ExpectOneAsync(async () =>
{
a.Tell("fail");
await Task.CompletedTask;
});
a.Tell("hello");
await ExpectMsgAsync(42);
}
public class SupervisorTestActor : UntypedActor
{
private IActorRef testActor;
public SupervisorTestActor(IActorRef testActor)
{
this.testActor = testActor;
}
protected override void OnReceive(object message)
{
switch (message)
{
case Spawn m:
Context.ActorOf(Props.Create(() => new KillableActor(testActor)), m.Name);
testActor.Tell(("Created", m.Name));
break;
case ContextStop m:
{
var child = Context.Child(m.Name);
Context.Stop(child);
break;
}
case Stop m:
{
var child = Context.Child(m.Name);
((IInternalActorRef)child).Stop();
break;
}
case Count _:
testActor.Tell(Context.GetChildren().Count());
break;
}
}
public class Spawn
{
public string Name { get; set; }
}
public class ContextStop
{
public string Name { get; set; }
}
public class Stop
{
public string Name { get; set; }
}
public class Count { }
}
public class KillableActor : UntypedActor
{
private IActorRef testActor;
public KillableActor(IActorRef testActor)
{
this.testActor = testActor;
}
protected override void PostStop()
{
Debug.WriteLine("inside poststop");
testActor.Tell(("Terminated", Self.Path.Name));
}
protected override void OnReceive(object message)
{
}
}
[Fact(DisplayName="If a parent receives a Terminated event for a child actor, the parent should no longer supervise it")]
public async Task Clear_child_upon_terminated()
{
var names = new[] {"Bob", "Jameson", "Natasha"};
var supervisor = Sys.ActorOf(Props.Create(() => new SupervisorTestActor(TestActor)));
supervisor.Tell(new SupervisorTestActor.Spawn(){ Name = names[0] });
await ExpectMsgAsync(("Created",names[0]));
supervisor.Tell(new SupervisorTestActor.Count());
await ExpectMsgAsync(1);
supervisor.Tell(new SupervisorTestActor.Spawn() { Name = names[1] });
await ExpectMsgAsync(("Created", names[1]));
supervisor.Tell(new SupervisorTestActor.Count());
await ExpectMsgAsync(2);
supervisor.Tell(new SupervisorTestActor.ContextStop() { Name = names[1] });
await ExpectMsgAsync(("Terminated", names[1]));
//we need to wait for the child actor to unregister itself from the parent.
//this is done after PostStop so we have no way to wait for it
//ideas?
await Task.Delay(100);
supervisor.Tell(new SupervisorTestActor.Count());
await ExpectMsgAsync(1);
supervisor.Tell(new SupervisorTestActor.Spawn() { Name = names[2] });
await ExpectMsgAsync(("Created", names[2]));
await Task.Delay(100);
supervisor.Tell(new SupervisorTestActor.Count());
await ExpectMsgAsync(2);
supervisor.Tell(new SupervisorTestActor.Stop() { Name = names[0] });
await ExpectMsgAsync(("Terminated", names[0]));
supervisor.Tell(new SupervisorTestActor.Stop() { Name = names[2] });
await ExpectMsgAsync(("Terminated", names[2]));
await Task.Delay(100);
supervisor.Tell(new SupervisorTestActor.Count());
await ExpectMsgAsync(0);
}
class MyCustomException : Exception {}
[Fact(DisplayName="PreRestart should receive correct cause, message and sender")]
public async Task Call_PreStart_with_correct_message_and_sender()
{
var broken = ActorOf(c =>
{
c.Receive<string>((_, _) =>
{
throw new MyCustomException();
});
c.OnPreRestart = (ex, mess, context) =>
{
TestActor.Tell(ex);
TestActor.Tell(mess);
TestActor.Tell(context.Sender);
};
});
const string message = "hello";
broken.Tell(message);
await ExpectMsgAsync<MyCustomException>();
await ExpectMsgAsync(message);
await ExpectMsgAsync(TestActor);
}
}
}