add test for grain size
[concurrent.git] / test / test_actors.rb
bloba77537b2bf85caa95bcf3898a3fcd06de216e67f
1 require 'test/unit'
2 require 'concurrent/actors'
3 require 'thread'
5 include Concurrent::Actors
6 Channel = Concurrent::Primitives::Channel
8 class TestActors < Test::Unit::TestCase
9   class A ; end
10   class B ; end
12   def test_current
13     assert_instance_of Actor, Actor.current
14   end
16   def test_spawn
17     c = Channel.new
18     child = Actor.spawn { c << Actor.current }
19     assert_equal child, c.receive
20   end
22   def test_receive_filter
23     c = Channel.new
24     child = Actor.spawn do
25       Actor.receive do |f|
26         f.when( B ) { |m| c << m }
27       end
28       Actor.receive do |f|
29         f.when( A ) { |m| c << m }
30       end
31     end
32     a = A.new
33     b = B.new
34     child << a
35     child << b
36     assert_equal b, c.receive
37     assert_equal a, c.receive
38   end
40   def test_receive_empty_filter
41     assert_raise ArgumentError do
42       Actor.receive {}
43     end
44   end
45 end