From 57062d91b9bfb1688151cd2393011504fd26f0d8 Mon Sep 17 00:00:00 2001 From: ko1 Date: Sun, 27 May 2007 19:12:43 +0000 Subject: * 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: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_fiber.rb | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 test/ruby/test_fiber.rb (limited to 'test') diff --git a/test/ruby/test_fiber.rb b/test/ruby/test_fiber.rb new file mode 100644 index 0000000000..38d7cd9b62 --- /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 + -- cgit v1.2.3