* 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'
|
2007-08-24 22:03:44 -04:00
|
|
|
require 'fiber'
|
2007-08-25 16:54:50 -04:00
|
|
|
require 'continuation'
|
* 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
|
|
|
|
|
|
|
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)
|
2007-06-02 03:48:29 -04:00
|
|
|
Fiber.yield :ok2
|
2007-08-21 14:51:39 -04:00
|
|
|
}.resume(:ok1)
|
2007-06-02 03:48:29 -04:00
|
|
|
)
|
2007-08-21 14:51:39 -04:00
|
|
|
assert_equal([:a, :b], Fiber.new{|a, b| [a, b]}.resume(: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-08-21 14:51:39 -04:00
|
|
|
assert_equal(:ok, Fiber.new{:ok}.resume)
|
* 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-08-21 14:51:39 -04:00
|
|
|
}.resume + [:b]
|
|
|
|
}.resume + [:c]
|
|
|
|
}.resume + [:d]
|
|
|
|
}.resume + [: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-08-21 14:51:39 -04:00
|
|
|
}.resume
|
* 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
|
2007-08-21 14:51:39 -04:00
|
|
|
}.resume
|
2007-06-06 14:19:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}.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{}
|
2007-08-21 14:51:39 -04:00
|
|
|
Thread.new{f.resume}.join # Fiber yielding across thread
|
2007-06-02 03:48:29 -04:00
|
|
|
}
|
|
|
|
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-08-21 14:51:39 -04:00
|
|
|
f.resume
|
|
|
|
f.resume
|
* 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}
|
2007-08-21 14:51:39 -04:00
|
|
|
}.resume
|
2007-06-14 23:20:13 -04:00
|
|
|
@c.call # cross fiber callcc
|
|
|
|
}
|
2007-08-21 14:51:39 -04:00
|
|
|
assert_raise(RuntimeError){
|
|
|
|
Fiber.new{
|
|
|
|
raise
|
|
|
|
}.resume
|
* 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-08-21 14:51:39 -04:00
|
|
|
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
|
* 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-05 13:55:07 -04:00
|
|
|
|
|
|
|
def test_return
|
|
|
|
assert_raise(LocalJumpError){
|
|
|
|
Fiber.new do
|
|
|
|
return
|
2007-08-21 14:51:39 -04:00
|
|
|
end.resume
|
2007-06-05 13:55:07 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_throw
|
2007-09-07 14:00:44 -04:00
|
|
|
assert_raise(ArgumentError){
|
2007-06-05 13:55:07 -04:00
|
|
|
Fiber.new do
|
|
|
|
throw :a
|
2007-08-21 14:51:39 -04:00
|
|
|
end.resume
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_transfer
|
|
|
|
ary = []
|
|
|
|
f2 = nil
|
2007-08-24 22:03:44 -04:00
|
|
|
f1 = Fiber.new{
|
2007-08-21 14:51:39 -04:00
|
|
|
ary << f2.transfer(:foo)
|
|
|
|
:ok
|
|
|
|
}
|
2007-08-24 22:03:44 -04:00
|
|
|
f2 = Fiber.new{
|
2007-08-21 14:51:39 -04:00
|
|
|
ary << f1.transfer(:baz)
|
|
|
|
:ng
|
2007-06-05 13:55:07 -04:00
|
|
|
}
|
2007-08-21 14:51:39 -04:00
|
|
|
assert_equal(:ok, f1.transfer)
|
|
|
|
assert_equal([:baz], ary)
|
2007-06-05 13:55:07 -04:00
|
|
|
end
|
2007-09-28 15:04:45 -04:00
|
|
|
|
|
|
|
def test_tls
|
|
|
|
#
|
|
|
|
def tvar(var, val)
|
|
|
|
old = Thread.current[var]
|
|
|
|
begin
|
|
|
|
Thread.current[var] = val
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Thread.current[var] = old
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
fb = Fiber.new {
|
|
|
|
assert_equal(nil, Thread.current[:v]); tvar(:v, :x) {
|
|
|
|
assert_equal(:x, Thread.current[:v]); Fiber.yield
|
|
|
|
assert_equal(:x, Thread.current[:v]); }
|
|
|
|
assert_equal(nil, Thread.current[:v]); Fiber.yield
|
|
|
|
raise # unreachable
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal(nil, Thread.current[:v]); tvar(:v,1) {
|
|
|
|
assert_equal(1, Thread.current[:v]); tvar(:v,3) {
|
|
|
|
assert_equal(3, Thread.current[:v]); fb.resume
|
|
|
|
assert_equal(3, Thread.current[:v]); }
|
|
|
|
assert_equal(1, Thread.current[:v]); }
|
|
|
|
assert_equal(nil, Thread.current[:v]); fb.resume
|
|
|
|
assert_equal(nil, Thread.current[:v]);
|
|
|
|
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
|
|
|
|
|