Skip to content

Commit 0f9a63d

Browse files
committed
Improved ActorContext construction.
1 parent 5181f45 commit 0f9a63d

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

lib/concurrent/actor_context.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ def on_shutdown
1515

1616
def self.included(base)
1717

18-
def base.spawn
19-
Concurrent::SimpleActorRef.new(self.new)
18+
class << base
19+
protected :new
20+
21+
def spawn(opts = {})
22+
args = opts.fetch(:args, [])
23+
Concurrent::SimpleActorRef.new(self.new(*args))
24+
end
2025
end
2126
end
2227
end

spec/concurrent/actor_context_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ module Concurrent
1010
end
1111
end
1212

13+
it 'protects #initialize' do
14+
expect {
15+
described_class.new
16+
}.to raise_error(NoMethodError)
17+
end
18+
1319
context 'callbacks' do
1420

15-
subject { described_class.new }
21+
subject { described_class.send(:new) }
1622

1723
specify { subject.should respond_to :on_start }
1824

spec/concurrent/actor_ref_shared.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
def shared_actor_test_class
44
Class.new do
55
include Concurrent::ActorContext
6+
attr_reader :argv
7+
def initialize(*args)
8+
@argv = args
9+
end
610
def receive(*msg)
711
case msg.first
812
when :poison

spec/concurrent/simple_actor_ref_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ module Concurrent
1616

1717
it_should_behave_like :actor_ref
1818

19+
context 'construction' do
20+
21+
it 'supports :args being nil' do
22+
subject = shared_actor_test_class.spawn
23+
actor = subject.instance_variable_get(:@actor)
24+
actor.argv.should be_empty
25+
end
26+
27+
it 'passes all :args option to the constructor' do
28+
subject = shared_actor_test_class.spawn(args: [1, 2, 3, 4])
29+
actor = subject.instance_variable_get(:@actor)
30+
actor.argv.should eq [1, 2, 3, 4]
31+
end
32+
end
33+
1934
context 'supervision' do
2035

2136
it 'does not start a new thread on construction' do

0 commit comments

Comments
 (0)