1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/ruby/test_fiber.rb
ko1 57062d91b9 * 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
2007-05-27 19:12:43 +00:00

66 lines
1.2 KiB
Ruby

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