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 cf16f0de0f * test/ruby/test_fiber.rb: fix to require 'continuation'.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13274 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-25 20:54:50 +00:00

135 lines
2.4 KiB
Ruby

require 'test/unit'
require 'fiber'
require 'continuation'
class TestFiber < Test::Unit::TestCase
def test_normal
f = Fiber.current
assert_equal(:ok2,
Fiber.new{|e|
assert_equal(:ok1, e)
Fiber.yield :ok2
}.resume(:ok1)
)
assert_equal([:a, :b], Fiber.new{|a, b| [a, b]}.resume(:a, :b))
end
def test_term
assert_equal(:ok, Fiber.new{:ok}.resume)
assert_equal([:a, :b, :c, :d, :e],
Fiber.new{
Fiber.new{
Fiber.new{
Fiber.new{
[:a]
}.resume + [:b]
}.resume + [:c]
}.resume + [:d]
}.resume + [:e])
end
def test_many_fibers
max = 10000
assert_equal(max, max.times{
Fiber.new{}
})
assert_equal(max,
max.times{|i|
Fiber.new{
}.resume
}
)
end
def test_many_fibers_with_threads
max = 1000
@cnt = 0
(1..100).map{|ti|
Thread.new{
max.times{|i|
Fiber.new{
@cnt += 1
}.resume
}
}
}.each{|t|
t.join
}
assert_equal(:ok, :ok)
end
def test_error
assert_raise(ArgumentError){
Fiber.new # Fiber without block
}
assert_raise(FiberError){
f = Fiber.new{}
Thread.new{f.resume}.join # Fiber yielding across thread
}
assert_raise(FiberError){
f = Fiber.new{}
f.resume
f.resume
}
assert_raise(RuntimeError){
f = Fiber.new{
@c = callcc{|c| @c = c}
}.resume
@c.call # cross fiber callcc
}
assert_raise(RuntimeError){
Fiber.new{
raise
}.resume
}
assert_raise(FiberError){
Fiber.yield
}
assert_raise(FiberError){
fib = Fiber.new{
fib.resume
}
fib.resume
}
assert_raise(FiberError){
fib = Fiber.new{
Fiber.new{
fib.resume
}.resume
}
fib.resume
}
end
def test_return
assert_raise(LocalJumpError){
Fiber.new do
return
end.resume
}
end
def test_throw
assert_raise(NameError){
Fiber.new do
throw :a
end.resume
}
end
def test_transfer
ary = []
f2 = nil
f1 = Fiber.new{
ary << f2.transfer(:foo)
:ok
}
f2 = Fiber.new{
ary << f1.transfer(:baz)
:ng
}
assert_equal(:ok, f1.transfer)
assert_equal([:baz], ary)
end
end