File tree 3 files changed +58
-16
lines changed
lib/concurrent/actor/behaviour
3 files changed +58
-16
lines changed Original file line number Diff line number Diff line change @@ -41,7 +41,8 @@ def process_envelope
41
41
end
42
42
43
43
def on_event ( event )
44
- if event == :terminated
44
+ case event
45
+ when :terminated , :restarted
45
46
@buffer . each { |envelope | reject_envelope envelope }
46
47
@buffer . clear
47
48
end
Original file line number Diff line number Diff line change @@ -22,8 +22,8 @@ def on_envelope(envelope)
22
22
from_supervisor? ( envelope ) { resume! }
23
23
when :reset!
24
24
from_supervisor? ( envelope ) { reset! }
25
- # when :restart! TODO
26
- # from_supervisor?(envelope) { reset ! }
25
+ when :restart!
26
+ from_supervisor? ( envelope ) { restart ! }
27
27
else
28
28
if @paused
29
29
@buffer << envelope
@@ -34,40 +34,48 @@ def on_envelope(envelope)
34
34
end
35
35
end
36
36
37
+ def from_supervisor? ( envelope )
38
+ if behaviour! ( Supervised ) . supervisor == envelope . sender
39
+ yield
40
+ else
41
+ false
42
+ end
43
+ end
44
+
37
45
def pause! ( error = nil )
38
46
@paused = true
39
47
broadcast ( error || :paused )
40
48
true
41
49
end
42
50
43
51
def resume! ( broadcast = true )
44
- @buffer . each { |envelope | core . schedule_execution { pass envelope } }
45
- @buffer . clear
46
52
@paused = false
47
53
broadcast ( :resumed ) if broadcast
48
54
true
49
55
end
50
56
51
- def from_supervisor? ( envelope )
52
- if behaviour! ( Supervised ) . supervisor == envelope . sender
53
- yield
54
- else
55
- false
56
- end
57
- end
58
-
59
- def reset!
57
+ def reset! ( broadcast = true )
60
58
core . allocate_context
61
59
core . build_context
62
60
resume! ( false )
63
- broadcast ( :reset )
61
+ broadcast ( :reset ) if broadcast
62
+ true
63
+ end
64
+
65
+ def restart!
66
+ reset! false
67
+ broadcast ( :restarted )
64
68
true
65
69
end
66
70
67
71
def on_event ( event )
68
- if event == :terminated
72
+ case event
73
+ when :terminated , :restarted
69
74
@buffer . each { |envelope | reject_envelope envelope }
70
75
@buffer . clear
76
+ when :resumed , :reset
77
+ @buffer . each { |envelope | core . schedule_execution { pass envelope } }
78
+ @buffer . clear
71
79
end
72
80
super event
73
81
end
Original file line number Diff line number Diff line change @@ -336,6 +336,39 @@ def on_message(message)
336
336
expect ( queue . pop ) . to eq :init # rebuilds context
337
337
expect ( queue . pop ) . to eq :reset
338
338
terminate_actors test
339
+
340
+ queue = Queue . new
341
+ resuming_behaviour = Behaviour . restarting_behaviour_definition . map do |c , args |
342
+ if Behaviour ::Supervising == c
343
+ [ c , [ :restart! , :one_for_one ] ]
344
+ else
345
+ [ c , args ]
346
+ end
347
+ end
348
+
349
+ test = AdHoc . spawn name : :tester , behaviour_definition : resuming_behaviour do
350
+ actor = AdHoc . spawn name : :pausing ,
351
+ behaviour_definition : Behaviour . restarting_behaviour_definition do
352
+ queue << :init
353
+ -> m { m == :add ? 1 : pass }
354
+ end
355
+
356
+ actor << :supervise
357
+ queue << actor . ask! ( :supervisor )
358
+ actor << nil
359
+ queue << actor . ask ( :add )
360
+
361
+ -> m do
362
+ queue << m
363
+ end
364
+ end
365
+
366
+ expect ( queue . pop ) . to eq :init
367
+ expect ( queue . pop ) . to eq test
368
+ expect ( queue . pop . wait . reason ) . to be_a_kind_of ( ActorTerminated )
369
+ expect ( queue . pop ) . to eq :init
370
+ expect ( queue . pop ) . to eq :restarted
371
+ terminate_actors test
339
372
end
340
373
341
374
end
You can’t perform that action at this time.
0 commit comments