diff options
| author | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-05-27 19:12:43 +0000 |
|---|---|---|
| committer | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-05-27 19:12:43 +0000 |
| commit | e5f3b1a5cc39db25503f1d175fbd0f93c04a85c6 (patch) | |
| tree | 586958dc0f4d60d96f50d9a30ebfcc20b88f9d5a /test/ruby | |
| parent | dbe2b0e416d8d28d82849eb5891a398e160c10f6 (diff) | |
| download | ruby-e5f3b1a5cc39db25503f1d175fbd0f93c04a85c6.tar.gz ruby-e5f3b1a5cc39db25503f1d175fbd0f93c04a85c6.tar.xz ruby-e5f3b1a5cc39db25503f1d175fbd0f93c04a85c6.zip | |
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby')
| -rw-r--r-- | test/ruby/test_fiber.rb | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/test/ruby/test_fiber.rb b/test/ruby/test_fiber.rb new file mode 100644 index 000000000..38d7cd9b6 --- /dev/null +++ b/test/ruby/test_fiber.rb @@ -0,0 +1,66 @@ +require 'test/unit' + +class TestFiber < Test::Unit::TestCase + def test_normal + f = Fiber.current + assert_equal(:ok2, + Fiber.new(:ok1){|e| + assert_equal(:ok1, e) + assert_equal(f, Fiber.prev) + Fiber.pass :ok2 + }.pass) + end + + def test_term + assert_equal(:ok, Fiber.new{:ok}.pass) + assert_equal([:a, :b, :c, :d, :e], + Fiber.new{ + Fiber.new{ + Fiber.new{ + Fiber.new{ + [:a] + }.pass + [:b] + }.pass + [:c] + }.pass + [:d] + }.pass + [:e]) + end + + def test_many_fibers + max = 10000 + assert_equal(max, max.times{ + Fiber.new{} + }) + assert_equal(max, + max.times{|i| + Fiber.new{ + }.pass + } + ) + end + + def test_error + assert_raise(ArgumentError){ + Fiber.new # Fiber without block + } + assert_raise(RuntimeError){ + f = Fiber.new{} + Thread.new{f.pass}.join # Fiber passing across thread + } + end + + def test_loop + ary = [] + f2 = nil + f1 = Fiber.new{ + ary << f2.pass(:foo) + :bar + } + f2 = Fiber.new{ + ary << f1.pass(:baz) + :ok + } + assert_equal(:ok, f1.pass) + assert_equal([:baz, :bar], ary) + end +end + |
