SystemTimer 1.0 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +13 -0
- data/README +7 -0
- data/ext/system_timer/extconf.rb +0 -0
- data/ext/system_timer/system_timer_native.c +204 -127
- data/lib/system_timer.rb +65 -30
- data/lib/system_timer/concurrent_timer_pool.rb +83 -0
- data/lib/system_timer/thread_timer.rb +21 -0
- data/lib/system_timer_stub.rb +5 -0
- data/test/all_tests.rb +3 -1
- data/test/system_timer/concurrent_timer_pool_unit_test.rb +274 -0
- data/test/system_timer/thread_timer_test.rb +20 -0
- data/test/system_timer_functional_test.rb +260 -0
- data/test/system_timer_unit_test.rb +96 -0
- data/test/test_helper.rb +10 -0
- metadata +10 -4
- data/test/system_timer_test.rb +0 -121
@@ -0,0 +1,96 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
unit_tests do
|
4
|
+
|
5
|
+
test "timeout_after registers a new timer in the timer pool" do
|
6
|
+
pool = stub_everything
|
7
|
+
Thread.stubs(:current).returns(:the_current_thread)
|
8
|
+
SystemTimer.stubs(:timer_pool).returns(pool)
|
9
|
+
SystemTimer.stubs(:install_next_timer)
|
10
|
+
SystemTimer.stubs(:restore_original_configuration)
|
11
|
+
|
12
|
+
pool.expects(:add_timer).with(5).returns(stub_everything)
|
13
|
+
SystemTimer.timeout_after(5) {}
|
14
|
+
end
|
15
|
+
|
16
|
+
test "timeout_after installs a system timer saving the previous " +
|
17
|
+
"configuration when there is only one timer" do
|
18
|
+
|
19
|
+
now = Time.now
|
20
|
+
Time.stubs(:now).returns(now)
|
21
|
+
SystemTimer.stubs(:restore_original_configuration)
|
22
|
+
|
23
|
+
SystemTimer.expects(:install_first_timer_and_save_original_configuration).with(24)
|
24
|
+
SystemTimer.timeout_after(24) {}
|
25
|
+
end
|
26
|
+
|
27
|
+
test "timeout_after installs a system timer without saving the previous " +
|
28
|
+
"configuration when there is more than one timer" do
|
29
|
+
|
30
|
+
now = Time.now
|
31
|
+
Time.stubs(:now).returns(now)
|
32
|
+
SystemTimer.timer_pool.register_timer now.to_i + 100, :a_thread
|
33
|
+
SystemTimer.stubs(:restore_original_configuration)
|
34
|
+
SystemTimer.stubs(:install_next_timer)
|
35
|
+
|
36
|
+
SystemTimer.expects(:install_next_timer).with(24)
|
37
|
+
SystemTimer.timeout_after(24) {}
|
38
|
+
end
|
39
|
+
|
40
|
+
test "timeout_after installs a system timer with the interval before " +
|
41
|
+
"the next timer to expire" do
|
42
|
+
|
43
|
+
now = Time.now
|
44
|
+
Time.stubs(:now).returns(now)
|
45
|
+
SystemTimer.timer_pool.register_timer now.to_i + 24, :a_thread
|
46
|
+
SystemTimer.stubs(:restore_original_configuration)
|
47
|
+
SystemTimer.stubs(:install_next_timer)
|
48
|
+
|
49
|
+
SystemTimer.expects(:install_next_timer).with(24)
|
50
|
+
SystemTimer.timeout_after(100) {}
|
51
|
+
end
|
52
|
+
|
53
|
+
test "timeout_after cancels the timer when the block completes without " +
|
54
|
+
"timeout" do
|
55
|
+
|
56
|
+
now = Time.now
|
57
|
+
the_timer = stub_everything
|
58
|
+
Time.stubs(:now).returns(now)
|
59
|
+
SystemTimer.stubs(:restore_original_configuration)
|
60
|
+
SystemTimer.stubs(:install_first_timer_and_save_original_configuration)
|
61
|
+
SystemTimer.timer_pool.stubs(:add_timer).returns(the_timer)
|
62
|
+
SystemTimer.timer_pool.stubs(:first_timer?).returns(true)
|
63
|
+
|
64
|
+
SystemTimer.timer_pool.expects(:cancel).with(the_timer)
|
65
|
+
SystemTimer.timeout_after(24) {}
|
66
|
+
end
|
67
|
+
|
68
|
+
test "debug does not output to stdout when debug is disabled" do
|
69
|
+
SystemTimer.stubs(:debug_enabled?).returns(false)
|
70
|
+
original_stdout = $stdout
|
71
|
+
begin
|
72
|
+
stdout = StringIO.new
|
73
|
+
$stdout = stdout
|
74
|
+
|
75
|
+
SystemTimer.send :debug, "a log message"
|
76
|
+
assert stdout.string.empty?
|
77
|
+
ensure
|
78
|
+
$stdout = original_stdout
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
test "debug logs messaget to stdout when debug is enabled" do
|
83
|
+
SystemTimer.stubs(:debug_enabled?).returns(true)
|
84
|
+
original_stdout = $stdout
|
85
|
+
begin
|
86
|
+
stdout = StringIO.new
|
87
|
+
$stdout = stdout
|
88
|
+
|
89
|
+
SystemTimer.send :debug, "a log message"
|
90
|
+
assert_match /a log message/, stdout.string
|
91
|
+
ensure
|
92
|
+
$stdout = original_stdout
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
2
|
+
$:.unshift File.dirname(__FILE__) + '/../ext/system_timer'
|
3
|
+
$: << File.dirname(__FILE__) + "/../vendor/gems/dust-0.1.6/lib"
|
4
|
+
$: << File.dirname(__FILE__) + "/../vendor/gems/mocha-0.9.1/lib"
|
5
|
+
require 'test/unit'
|
6
|
+
require 'dust'
|
7
|
+
require 'mocha'
|
8
|
+
require 'stringio'
|
9
|
+
require "open-uri"
|
10
|
+
require 'system_timer'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: SystemTimer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "1.
|
4
|
+
version: "1.1"
|
5
5
|
platform: ruby
|
6
6
|
authors: []
|
7
7
|
|
@@ -9,7 +9,7 @@ autorequire: system_timer
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-11-16 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -27,10 +27,16 @@ files:
|
|
27
27
|
- ChangeLog
|
28
28
|
- ext/system_timer/system_timer_native.c
|
29
29
|
- ext/system_timer/extconf.rb
|
30
|
+
- lib/system_timer/concurrent_timer_pool.rb
|
31
|
+
- lib/system_timer/thread_timer.rb
|
30
32
|
- lib/system_timer.rb
|
31
33
|
- lib/system_timer_stub.rb
|
32
34
|
- test/all_tests.rb
|
33
|
-
- test/
|
35
|
+
- test/system_timer/concurrent_timer_pool_unit_test.rb
|
36
|
+
- test/system_timer/thread_timer_test.rb
|
37
|
+
- test/system_timer_functional_test.rb
|
38
|
+
- test/system_timer_unit_test.rb
|
39
|
+
- test/test_helper.rb
|
34
40
|
- README
|
35
41
|
has_rdoc: true
|
36
42
|
homepage:
|
@@ -58,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
64
|
requirements: []
|
59
65
|
|
60
66
|
rubyforge_project:
|
61
|
-
rubygems_version: 1.0
|
67
|
+
rubygems_version: 1.2.0
|
62
68
|
signing_key:
|
63
69
|
specification_version: 2
|
64
70
|
summary: Set a Timeout based on signals, which are more reliable than Timeout. Timeout is based on green threads.
|
data/test/system_timer_test.rb
DELETED
@@ -1,121 +0,0 @@
|
|
1
|
-
# $: << File.dirname(__FILE__) + '/../ext/xray'
|
2
|
-
$: << File.dirname(__FILE__) + '/../lib'
|
3
|
-
$: << File.dirname(__FILE__) + '/../ext/system_timer'
|
4
|
-
$: << File.dirname(__FILE__) + "/../../../vendor/gems/dust-0.1.4/lib"
|
5
|
-
$: << File.dirname(__FILE__) + "/../../../vendor/gems/mocha-0.5.3/lib"
|
6
|
-
require 'test/unit'
|
7
|
-
require 'system_timer'
|
8
|
-
require 'dust'
|
9
|
-
require 'mocha'
|
10
|
-
require 'stringio'
|
11
|
-
|
12
|
-
functional_tests do
|
13
|
-
|
14
|
-
test "original_ruby_sigalrm_handler is nil after reset" do
|
15
|
-
SystemTimer.send(:install_ruby_sigalrm_handler)
|
16
|
-
SystemTimer.send(:reset_original_ruby_sigalrm_handler)
|
17
|
-
assert_nil SystemTimer.send(:original_ruby_sigalrm_handler)
|
18
|
-
end
|
19
|
-
|
20
|
-
test "original_ruby_sigalrm_handler is set to existing handler after install_ruby_sigalrm_handler" do
|
21
|
-
SystemTimer.expects(:trap).with('SIGALRM').returns(:an_existing_handler)
|
22
|
-
SystemTimer.send(:install_ruby_sigalrm_handler)
|
23
|
-
assert_equal :an_existing_handler, SystemTimer.send(:original_ruby_sigalrm_handler)
|
24
|
-
end
|
25
|
-
|
26
|
-
test "restore_original_ruby_sigalrm_handler traps sigalrm using original_ruby_sigalrm_handler" do
|
27
|
-
SystemTimer.stubs(:original_ruby_sigalrm_handler).returns(:the_original_handler)
|
28
|
-
SystemTimer.expects(:trap).with('SIGALRM', :the_original_handler)
|
29
|
-
SystemTimer.send :restore_original_ruby_sigalrm_handler
|
30
|
-
end
|
31
|
-
|
32
|
-
test "restore_original_ruby_sigalrm_handler resets original_ruby_sigalrm_handler" do
|
33
|
-
SystemTimer.stubs(:trap)
|
34
|
-
SystemTimer.expects(:reset_original_ruby_sigalrm_handler)
|
35
|
-
SystemTimer.send :restore_original_ruby_sigalrm_handler
|
36
|
-
end
|
37
|
-
|
38
|
-
test "restore_original_ruby_sigalrm_handler reset SIGALRM handler to default when original_ruby_sigalrm_handler is nil" do
|
39
|
-
SystemTimer.stubs(:original_ruby_sigalrm_handler)
|
40
|
-
SystemTimer.expects(:trap).with('SIGALRM', 'DEFAULT')
|
41
|
-
SystemTimer.stubs(:reset_original_ruby_sigalrm_handler)
|
42
|
-
SystemTimer.send :restore_original_ruby_sigalrm_handler
|
43
|
-
end
|
44
|
-
|
45
|
-
test "restore_original_ruby_sigalrm_handler resets original_ruby_sigalrm_handler when trap raises" do
|
46
|
-
SystemTimer.stubs(:trap).returns(:the_original_handler)
|
47
|
-
SystemTimer.send(:install_ruby_sigalrm_handler)
|
48
|
-
SystemTimer.expects(:trap).raises("next time maybe...")
|
49
|
-
SystemTimer.expects(:reset_original_ruby_sigalrm_handler)
|
50
|
-
|
51
|
-
SystemTimer.send(:restore_original_ruby_sigalrm_handler) rescue nil
|
52
|
-
end
|
53
|
-
|
54
|
-
test "timeout_after raises TimeoutError if block takes too long" do
|
55
|
-
assert_raises(Timeout::Error) do
|
56
|
-
SystemTimer.timeout_after(1) {sleep 5}
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
test "timeout_after does not raises Timeout Error if block completes in time" do
|
61
|
-
SystemTimer.timeout_after(5) {sleep 1}
|
62
|
-
end
|
63
|
-
|
64
|
-
test "timeout_after returns the value returned by the black" do
|
65
|
-
assert_equal :block_value, SystemTimer.timeout_after(1) {:block_value}
|
66
|
-
end
|
67
|
-
|
68
|
-
test "timeout_after raises TimeoutError in thread that called timeout_after" do
|
69
|
-
raised_thread = nil
|
70
|
-
other_thread = Thread.new do
|
71
|
-
begin
|
72
|
-
SystemTimer.timeout_after(1) {sleep 5}
|
73
|
-
flunk "Should have timed out"
|
74
|
-
rescue Timeout::Error
|
75
|
-
raised_thread = Thread.current
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
other_thread.join
|
80
|
-
assert_equal other_thread, raised_thread
|
81
|
-
end
|
82
|
-
|
83
|
-
test "cancelling a timer that was installed restores previous ruby handler for SIG_ALRM" do
|
84
|
-
begin
|
85
|
-
fake_original_ruby_handler = proc {}
|
86
|
-
initial_ruby_handler = trap "SIGALRM", fake_original_ruby_handler
|
87
|
-
SystemTimer.install_timer(3)
|
88
|
-
SystemTimer.cleanup_timer
|
89
|
-
assert_equal fake_original_ruby_handler, trap("SIGALRM", "IGNORE")
|
90
|
-
ensure # avoid interfering with test infrastructure
|
91
|
-
trap("SIGALRM", initial_ruby_handler) if initial_ruby_handler
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
test "debug_enabled returns true after enabling debug" do
|
96
|
-
begin
|
97
|
-
SystemTimer.disable_debug
|
98
|
-
SystemTimer.enable_debug
|
99
|
-
assert_equal true, SystemTimer.debug_enabled?
|
100
|
-
ensure
|
101
|
-
SystemTimer.disable_debug
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
test "debug_enabled returns false after disable debug" do
|
106
|
-
begin
|
107
|
-
SystemTimer.enable_debug
|
108
|
-
SystemTimer.disable_debug
|
109
|
-
assert_equal false, SystemTimer.debug_enabled?
|
110
|
-
ensure
|
111
|
-
SystemTimer.disable_debug
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
test "timeout offers an API fully compatible with timeout.rb" do
|
116
|
-
assert_raises(Timeout::Error) do
|
117
|
-
SystemTimer.timeout(1) {sleep 5}
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|