Skip to content

Commit d8ca345

Browse files
author
runa
committed
added tests for custom timeout exceptions
1 parent d33acb3 commit d8ca345

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

test/system_timer_functional_test.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@
5050
SystemTimer.timeout_after(1) {sleep 5}
5151
end
5252
end
53-
53+
test "timeout_after raises CustomTimeout if block takes too long" do
54+
assert_raises(CustomTimeout) do
55+
SystemTimer.timeout_after(1,CustomTimeout) {sleep 5}
56+
end
57+
end
58+
5459
test "timeout_after does not raises Timeout Error if block completes in time" do
5560
SystemTimer.timeout_after(5) {sleep 1}
5661
end
@@ -257,4 +262,4 @@ def assert_timeout_within(expected_timeout_in_seconds,
257262
"Timed out after #{elapsed} seconds, expected #{expected_timeout_in_seconds}"
258263
end
259264

260-
end
265+
end

test/system_timer_unit_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
SystemTimer.timeout_after(5) {}
1414
end
1515

16+
test "timeout_after registers a new timer with a custom timeout exception in the timer pool" do
17+
pool = stub_everything
18+
Thread.stubs(:current).returns(:the_current_thread)
19+
SystemTimer.stubs(:timer_pool).returns(pool)
20+
SystemTimer.stubs(:install_next_timer)
21+
SystemTimer.stubs(:restore_original_configuration)
22+
23+
pool.expects(:add_timer).with(5,CustomTimeout).returns(stub_everything)
24+
SystemTimer.timeout_after(5,CustomTimeout) {}
25+
end
26+
1627
test "timeout_after installs a system timer saving the previous " +
1728
"configuration when there is only one timer" do
1829

test/test_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
require 'stringio'
99
require "open-uri"
1010
require 'system_timer'
11+
class CustomTimeout < Exception
12+
end

0 commit comments

Comments
 (0)