-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced Timeout::timeout with Concurrent::Timeout.
- Loading branch information
Showing
8 changed files
with
85 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ rvm: | |
- 1.9.2 | ||
- ruby-head | ||
- jruby-19mode | ||
- jruby-head | ||
- rbx-19mode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'thread' | ||
|
||
module Concurrent | ||
|
||
TimeoutError = Class.new(StandardError) | ||
|
||
def timeout(seconds) | ||
|
||
thread = Thread.new do | ||
Thread.current[:result] = yield | ||
end | ||
success = thread.join(seconds) | ||
|
||
if success | ||
return thread[:result] | ||
else | ||
raise TimeoutError | ||
end | ||
ensure | ||
Thread.kill(thread) unless thread.nil? | ||
end | ||
module_function :timeout | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'spec_helper' | ||
|
||
module Concurrent | ||
|
||
describe '#timeout' do | ||
|
||
it 'raises an exception if no block is given' do | ||
expect { | ||
Concurrent::timeout(1) | ||
}.to raise_error | ||
end | ||
|
||
it 'returns the value of the block on success' do | ||
result = Concurrent::timeout(1) { 42 } | ||
result.should eq 42 | ||
end | ||
|
||
it 'raises an exception if the timeout value is reached' do | ||
expect { | ||
Concurrent::timeout(1){ sleep } | ||
}.to raise_error(Concurrent::TimeoutError) | ||
end | ||
|
||
it 'bubbles thread exceptions' do | ||
expect { | ||
Concurrent::timeout(1){ raise NotImplementedError } | ||
}.to raise_error | ||
end | ||
|
||
it 'kills the thread on success' do | ||
result = Concurrent::timeout(1) { 42 } | ||
Thread.should_receive(:kill).with(any_args()) | ||
Concurrent::timeout(1){ 42 } | ||
end | ||
|
||
it 'kills the thread on timeout' do | ||
Thread.should_receive(:kill).with(any_args()) | ||
expect { | ||
Concurrent::timeout(1){ sleep } | ||
}.to raise_error | ||
end | ||
|
||
it 'kills the thread on exception' do | ||
Thread.should_receive(:kill).with(any_args()) | ||
expect { | ||
Concurrent::timeout(1){ raise NotImplementedError } | ||
}.to raise_error | ||
end | ||
end | ||
end |