* 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 15:12:43 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestFiber < Test::Unit::TestCase
|
|
|
|
def test_normal
|
|
|
|
f = Fiber.current
|
|
|
|
assert_equal(:ok2,
|
2007-06-02 03:48:29 -04:00
|
|
|
Fiber.new{|e|
|
* 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 15:12:43 -04:00
|
|
|
assert_equal(:ok1, e)
|
|
|
|
assert_equal(f, Fiber.prev)
|
2007-06-02 03:48:29 -04:00
|
|
|
Fiber.yield :ok2
|
|
|
|
}.yield(:ok1)
|
|
|
|
)
|
|
|
|
assert_equal([:a, :b], Fiber.new{|a, b| [a, b]}.yield(:a, :b))
|
* 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 15:12:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_term
|
2007-06-02 03:48:29 -04:00
|
|
|
assert_equal(:ok, Fiber.new{:ok}.yield)
|
* 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 15:12:43 -04:00
|
|
|
assert_equal([:a, :b, :c, :d, :e],
|
|
|
|
Fiber.new{
|
|
|
|
Fiber.new{
|
|
|
|
Fiber.new{
|
|
|
|
Fiber.new{
|
|
|
|
[:a]
|
2007-06-02 03:48:29 -04:00
|
|
|
}.yield + [:b]
|
|
|
|
}.yield + [:c]
|
|
|
|
}.yield + [:d]
|
|
|
|
}.yield + [:e])
|
* 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 15:12:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_many_fibers
|
|
|
|
max = 10000
|
|
|
|
assert_equal(max, max.times{
|
|
|
|
Fiber.new{}
|
|
|
|
})
|
|
|
|
assert_equal(max,
|
|
|
|
max.times{|i|
|
|
|
|
Fiber.new{
|
2007-06-02 03:48:29 -04:00
|
|
|
}.yield
|
* 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 15:12:43 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2007-06-06 14:19:42 -04:00
|
|
|
def test_many_fibers_with_threads
|
|
|
|
max = 1000
|
|
|
|
@cnt = 0
|
|
|
|
(1..100).map{|ti|
|
|
|
|
Thread.new{
|
|
|
|
max.times{|i|
|
|
|
|
Fiber.new{
|
|
|
|
@cnt += 1
|
|
|
|
}.yield
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.each{|t|
|
|
|
|
t.join
|
|
|
|
}
|
|
|
|
assert_equal(:ok, :ok)
|
|
|
|
end
|
|
|
|
|
* 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 15:12:43 -04:00
|
|
|
def test_error
|
|
|
|
assert_raise(ArgumentError){
|
|
|
|
Fiber.new # Fiber without block
|
|
|
|
}
|
2007-06-02 03:48:29 -04:00
|
|
|
assert_raise(FiberError){
|
|
|
|
f = Fiber.new{}
|
|
|
|
Thread.new{f.yield}.join # Fiber yielding across thread
|
|
|
|
}
|
|
|
|
assert_raise(FiberError){
|
* 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 15:12:43 -04:00
|
|
|
f = Fiber.new{}
|
2007-06-02 03:48:29 -04:00
|
|
|
f.yield
|
|
|
|
f.yield
|
* 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 15:12:43 -04:00
|
|
|
}
|
2007-06-14 23:20:13 -04:00
|
|
|
assert_raise(RuntimeError){
|
|
|
|
f = Fiber.new{
|
|
|
|
@c = callcc{|c| @c = c}
|
|
|
|
}.yield
|
|
|
|
@c.call # cross fiber callcc
|
|
|
|
}
|
* 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 15:12:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_loop
|
|
|
|
ary = []
|
|
|
|
f2 = nil
|
|
|
|
f1 = Fiber.new{
|
2007-06-02 03:48:29 -04:00
|
|
|
ary << f2.yield(:foo)
|
* 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 15:12:43 -04:00
|
|
|
:bar
|
|
|
|
}
|
|
|
|
f2 = Fiber.new{
|
2007-06-02 03:48:29 -04:00
|
|
|
ary << f1.yield(:baz)
|
* 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 15:12:43 -04:00
|
|
|
:ok
|
|
|
|
}
|
2007-06-02 03:48:29 -04:00
|
|
|
assert_equal(:ok, f1.yield)
|
* 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 15:12:43 -04:00
|
|
|
assert_equal([:baz, :bar], ary)
|
|
|
|
end
|
2007-06-05 13:55:07 -04:00
|
|
|
|
|
|
|
def test_return
|
|
|
|
assert_raise(LocalJumpError){
|
|
|
|
Fiber.new do
|
|
|
|
return
|
|
|
|
end.yield
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_throw
|
|
|
|
assert_raise(RuntimeError){
|
|
|
|
Fiber.new do
|
|
|
|
throw :a
|
|
|
|
end.yield
|
|
|
|
}
|
|
|
|
end
|
* 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 15:12:43 -04:00
|
|
|
end
|
|
|
|
|