Skip to content

Commit bbd050d

Browse files
committed
Removing old #terminated? and #terminated methods
replacing with messages :terminated?, :terminated_event, :terminate!
1 parent 3cf3be7 commit bbd050d

File tree

5 files changed

+34
-54
lines changed

5 files changed

+34
-54
lines changed

lib/concurrent/actor/behaviour/termination.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ def terminated?
2222
end
2323

2424
def on_envelope(envelope)
25-
if terminated?
26-
reject_envelope envelope
27-
MESSAGE_PROCESSED
25+
case envelope.message
26+
when :terminated?
27+
terminated?
28+
when :terminate!
29+
terminate!
30+
when :terminated_event
31+
terminated
2832
else
29-
if envelope.message == :terminate!
30-
terminate!
33+
if terminated?
34+
reject_envelope envelope
35+
MESSAGE_PROCESSED
3136
else
3237
pass envelope
3338
end
@@ -37,11 +42,11 @@ def on_envelope(envelope)
3742
# Terminates the actor. Any Envelope received after termination is rejected.
3843
# Terminates all its children, does not wait until they are terminated.
3944
def terminate!
40-
return nil if terminated?
41-
@terminated.set
45+
return true if terminated?
46+
terminated.set
4247
broadcast(:terminated)
4348
parent << :remove_child if parent
44-
nil
49+
true
4550
end
4651
end
4752
end

lib/concurrent/actor/core.rb

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,12 @@ def initialize(opts = {}, &block)
8787
initialized.set true if initialized
8888
rescue => ex
8989
log ERROR, ex
90-
terminate!
90+
@first_behaviour.terminate!
9191
initialized.fail ex if initialized
9292
end
9393
end
9494
end
9595

96-
def allocate_context
97-
@context = @context_class.allocate
98-
end
99-
100-
def build_context
101-
@context.send :initialize_core, self
102-
@context.send :initialize, *@args, &@block
103-
end
104-
10596
# @return [Reference, nil] of parent actor
10697
def parent
10798
@parent_core && @parent_core.reference
@@ -142,21 +133,6 @@ def on_envelope(envelope)
142133
nil
143134
end
144135

145-
# @see Behaviour::Termination#terminated?
146-
def terminated?
147-
behaviour!(Behaviour::Termination).terminated?
148-
end
149-
150-
# @see Behaviour::Termination#terminated!
151-
def terminate!
152-
behaviour!(Behaviour::Termination).terminate!
153-
end
154-
155-
# @see Behaviour::Termination#terminated
156-
def terminated
157-
behaviour!(Behaviour::Termination).terminated
158-
end
159-
160136
# ensures that we are inside of the executor
161137
def guard!
162138
unless Actor.current == reference
@@ -202,6 +178,17 @@ def behaviour!(behaviour_class)
202178
@behaviours.fetch behaviour_class
203179
end
204180

181+
# @api private
182+
def allocate_context
183+
@context = @context_class.allocate
184+
end
185+
186+
# @api private
187+
def build_context
188+
@context.send :initialize_core, self
189+
@context.send :initialize, *@args, &@block
190+
end
191+
205192
private
206193

207194
def handle_envelope(envelope)

lib/concurrent/actor/internal_delegations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def children
1010

1111
# @see Core#terminate!
1212
def terminate!
13-
core.terminate!
13+
behaviour!(Behaviour::Termination).terminate!
1414
end
1515

1616
# delegates to core.log

lib/concurrent/actor/public_delegations.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ def parent
1818
core.parent
1919
end
2020

21-
# @see Behaviour::Termination#terminated?
22-
def terminated?
23-
core.terminated?
24-
end
25-
26-
# @see Behaviour::Termination#terminated
27-
def terminated
28-
core.terminated
29-
end
30-
3121
# @see Core#reference
3222
def reference
3323
core.reference

spec/concurrent/actor_spec.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ module Actor
2020

2121
def terminate_actors(*actors)
2222
actors.each do |actor|
23-
unless actor.terminated?
24-
actor << :terminate!
25-
actor.terminated.wait(2) or
26-
raise 'timeout'
23+
unless actor.ask!(:terminated?)
24+
actor.ask!(:terminate!)
2725
end
2826
end
2927
end
@@ -134,7 +132,7 @@ def on_message(message)
134132
it 'terminates on failed initialization' do
135133
a = AdHoc.spawn(name: :fail, logger: Concurrent.configuration.no_logger) { raise }
136134
expect(a.ask(nil).wait.rejected?).to be_truthy
137-
expect(a.terminated?).to be_truthy
135+
expect(a.ask!(:terminated?)).to be_truthy
138136
end
139137

140138
it 'terminates on failed initialization and raises with spawn!' do
@@ -146,7 +144,7 @@ def on_message(message)
146144
it 'terminates on failed message processing' do
147145
a = AdHoc.spawn(name: :fail, logger: Concurrent.configuration.no_logger) { -> _ { raise } }
148146
expect(a.ask(nil).wait.rejected?).to be_truthy
149-
expect(a.terminated?).to be_truthy
147+
expect(a.ask!(:terminated?)).to be_truthy
150148
end
151149
end
152150

@@ -205,11 +203,11 @@ def on_message(message)
205203

206204
it 'terminates with all its children' do
207205
child = subject.ask! :child
208-
expect(subject.terminated?).to be_falsey
206+
expect(subject.ask!(:terminated?)).to be_falsey
209207
subject.ask(:terminate!).wait
210-
expect(subject.terminated?).to be_truthy
211-
child.terminated.wait
212-
expect(child.terminated?).to be_truthy
208+
expect(subject.ask!(:terminated?)).to be_truthy
209+
child.ask!(:terminated_event).wait
210+
expect(child.ask!(:terminated?)).to be_truthy
213211

214212
terminate_actors subject, child
215213
end

0 commit comments

Comments
 (0)