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_continuation.rb

135 lines
2.6 KiB
Ruby
Raw Normal View History

require 'test/unit'
EnvUtil.suppress_warning {require 'continuation'}
require 'fiber'
class TestContinuation < Test::Unit::TestCase
def test_create
assert_equal(:ok, callcc{:ok})
assert_equal(:ok, callcc{|c| c.call :ok})
end
def test_call
assert_equal(:ok, callcc{|c| c.call :ok})
ary = []
ary << callcc{|c|
@cont = c
:a
}
@cont.call :b if ary.length < 3
assert_equal([:a, :b, :b], ary)
end
def test_check_localvars
vv = 0
@v = 0
@ary = []
[1, 2, 3].each{|i|
callcc {|k| @k = k}
@v += 1
vv += 1
}
@ary << [vv, @v]
@k.call if @v < 10
assert_equal((3..10).map{|e| [e, e]}, @ary)
end
def test_error
cont = callcc{|c| c}
assert_raise(RuntimeError){
Thread.new{cont.call}.join
}
assert_raise(LocalJumpError){
callcc
}
assert_raise(RuntimeError){
c = nil
Fiber.new do
callcc {|c2| c = c2 }
end.resume
c.call
}
end
def test_ary_flatten
assert_normal_exit %q{
require 'continuation'
n = 0
o = Object.new
def o.to_ary() callcc {|k| $k = k; [1,2,3]} end
[10,20,o,30,o,40].flatten.inspect
n += 1
$k.call if n < 100
}, '[ruby-dev:34798]'
end
def test_marshal_dump
assert_normal_exit %q{
require 'continuation'
n = 0
o = Object.new
def o.marshal_dump() callcc {|k| $k = k }; "fofof" end
a = [1,2,3,o,4,5,6]
Marshal.dump(a).inspect
n += 1
$k.call if n < 100
}, '[ruby-dev:34802]'
end
def tracing_with_set_trace_func
orig_thread = Thread.current
cont = nil
func = lambda do |*args|
if orig_thread == Thread.current
* vm_trace.c, vm_core.h: simplify tracing mechanism. (1) add rb_hook_list_t data structure which includes hooks, events (flag) and `need_clean' flag. If the last flag is true, then clean the hooks list. In other words, deleted hooks are contained by `hooks'. Cleanup process should run before traversing the list. (2) Change check mechanism See EXEC_EVENT_HOOK() in vm_core.h. (3) Add `raw' hooks APIs Normal hooks are guarded from exception by rb_protect(). However, this protection is overhead for too simple functions which never cause exceptions. `raw' hooks are executed without protection and faster. Now, we only provide registration APIs. All `raw' hooks are kicked under protection (same as normal hooks). * include/ruby/ruby.h: remove internal data definition and macros. * internal.h (ruby_suppress_tracing), vm_trace.c: rename ruby_suppress_tracing() to rb_suppress_tracing() and remove unused function parameter. * parse.y: fix to use renamed rb_suppress_tracing(). * thread.c (thread_create_core): no need to set RUBY_VM_VM. * vm.c (mark_event_hooks): move definition to vm_trace.c. * vm.c (ruby_vm_event_flags): add a global variable. This global variable represents all of Threads and VM's event masks (T1#events | T2#events | ... | VM#events). You can check the possibility kick trace func or not with ruby_vm_event_flags. ruby_vm_event_flags is maintained by vm_trace.c. * cont.c (fiber_switch, rb_cont_call): restore tracing status. [Feature #4347] * test/ruby/test_continuation.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36715 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-08-16 07:41:24 -04:00
if cont
@memo += 1
c = cont
cont = nil
c.call(nil)
end
end
end
cont = callcc { |cc| cc }
if cont
set_trace_func(func)
else
set_trace_func(nil)
end
end
def test_tracing_with_set_trace_func
@memo = 0
tracing_with_set_trace_func
tracing_with_set_trace_func
tracing_with_set_trace_func
assert_equal 3, @memo
end
def tracing_with_thread_set_trace_func
cont = nil
func = lambda do |*args|
* vm_trace.c, vm_core.h: simplify tracing mechanism. (1) add rb_hook_list_t data structure which includes hooks, events (flag) and `need_clean' flag. If the last flag is true, then clean the hooks list. In other words, deleted hooks are contained by `hooks'. Cleanup process should run before traversing the list. (2) Change check mechanism See EXEC_EVENT_HOOK() in vm_core.h. (3) Add `raw' hooks APIs Normal hooks are guarded from exception by rb_protect(). However, this protection is overhead for too simple functions which never cause exceptions. `raw' hooks are executed without protection and faster. Now, we only provide registration APIs. All `raw' hooks are kicked under protection (same as normal hooks). * include/ruby/ruby.h: remove internal data definition and macros. * internal.h (ruby_suppress_tracing), vm_trace.c: rename ruby_suppress_tracing() to rb_suppress_tracing() and remove unused function parameter. * parse.y: fix to use renamed rb_suppress_tracing(). * thread.c (thread_create_core): no need to set RUBY_VM_VM. * vm.c (mark_event_hooks): move definition to vm_trace.c. * vm.c (ruby_vm_event_flags): add a global variable. This global variable represents all of Threads and VM's event masks (T1#events | T2#events | ... | VM#events). You can check the possibility kick trace func or not with ruby_vm_event_flags. ruby_vm_event_flags is maintained by vm_trace.c. * cont.c (fiber_switch, rb_cont_call): restore tracing status. [Feature #4347] * test/ruby/test_continuation.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36715 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-08-16 07:41:24 -04:00
if cont
@memo += 1
c = cont
cont = nil
c.call(nil)
end
end
cont = callcc { |cc| cc }
if cont
Thread.current.set_trace_func(func)
else
Thread.current.set_trace_func(nil)
end
end
def test_tracing_with_thread_set_trace_func
@memo = 0
tracing_with_thread_set_trace_func
tracing_with_thread_set_trace_func
tracing_with_thread_set_trace_func
assert_equal 3, @memo
end
end