license, tests, lib.

This commit is contained in:
Denis Knauf 2013-03-06 11:58:33 +01:00
parent 6ebfbf64f9
commit c3dbaa7b71
8 changed files with 765 additions and 65 deletions

View file

@ -10,8 +10,13 @@ end
require 'test/unit'
require 'shoulda'
require 'timeout'
require 'benchmark'
require 'ffi/libc'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'ruby-timeout-interrupt'
class Test::Unit::TestCase

View file

@ -1,7 +1,80 @@
require 'helper'
class TestRubyTimeoutInterrupt < Test::Unit::TestCase
should "probably rename this file and start testing for real" do
flunk "hey buddy, you should probably rename this file and start testing for real"
end
def blocking
t = FFI::LibC.fopen '/dev/ptmx', 'r'
b = FFI::LibC.malloc 1025
s = FFI::LibC.fread b, 1, 1024, t
ensure
FFI::LibC.fclose t if t
FFI::LibC.free b if b
end
def assert_no_defined_timeout_yet
assert TimeoutInterrupt.timeouts.empty?, "For testing, no timeout should be defined, yet!"
end
should "not interrupt a long blocking call with the old Timeout" do
time = Benchmark.realtime do
begin
TimeoutInterrupt.timeout(5) do
Timeout.timeout(1) do
blocking
assert false, "Should be unreachable!"
end
end
rescue Timeout::Error
:ok
end
end
assert 3 < time, "Did timeout!"
end
should "interrupt a long blocking call with the new TimeoutInterrupt" do
time = Benchmark.realtime do
begin
TimeoutInterrupt.timeout(1) do
blocking
assert false, "Should be unreachable!"
end
rescue Timeout::Error
:ok
end
end
assert 3 > time, "Did not interrupt."
end
should "interrupt scoped timeout, but not outer timeout" do
assert_no_defined_timeout_yet
begin
TimeoutInterrupt.timeout(10) do
TimeoutInterrupt.timeout(1) do
Kernel.sleep 2
end
assert false, "Should be unreachable!"
end
rescue Timeout::Error
:ok
end
assert TimeoutInterrupt.timeouts.empty?, "There are timeouts defined, yet!"
end
should "clear timeouts, if not timed out, too." do
assert_no_defined_timeout_yet
TimeoutInterrupt.timeout(10) {}
assert TimeoutInterrupt.timeouts.empty?, "There are timeouts defined, yet!"
end
should "return a Proc if now block given, but do not create a timeout." do
assert_no_defined_timeout_yet
assert TimeoutInterrupt.timeout(10).kind_of?( Proc), "Did not return a Proc."
end
should "run a returned Proc with given timeout." do
assert_no_defined_timeout_yet
to = TimeoutInterrupt.timeout(10)
called = false
to.call { called = true }
assert called, "Did not called."
end
end