mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
b69b1af650
commit
57062d91b9
9 changed files with 442 additions and 90 deletions
66
test/ruby/test_fiber.rb
Normal file
66
test/ruby/test_fiber.rb
Normal file
|
|
@ -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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue