summaryrefslogtreecommitdiff
path: root/test/ruby/test_fiber.rb
diff options
context:
space:
mode:
authorko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-05-27 19:12:43 +0000
committerko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-05-27 19:12:43 +0000
commit57062d91b9bfb1688151cd2393011504fd26f0d8 (patch)
tree586958dc0f4d60d96f50d9a30ebfcc20b88f9d5a /test/ruby/test_fiber.rb
parentb69b1af65002e9a70a4f33bafa35593b4618e7a4 (diff)
* 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
Diffstat (limited to 'test/ruby/test_fiber.rb')
-rw-r--r--test/ruby/test_fiber.rb66
1 files changed, 66 insertions, 0 deletions
diff --git a/test/ruby/test_fiber.rb b/test/ruby/test_fiber.rb
new file mode 100644
index 0000000000..38d7cd9b62
--- /dev/null
+++ b/test/ruby/test_fiber.rb
@@ -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
+