Skip to content

Commit

Permalink
Replaced Timeout::timeout with Concurrent::Timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdantonio committed Oct 12, 2013
1 parent 4981877 commit e8371c0
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ rvm:
- 1.9.2
- ruby-head
- jruby-19mode
- jruby-head
- rbx-19mode
5 changes: 2 additions & 3 deletions lib/concurrent.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
require 'thread'

require 'concurrent/version'

require 'concurrent/event'

require 'concurrent/actor'
require 'concurrent/agent'
require 'concurrent/event'
require 'concurrent/executor'
require 'concurrent/future'
require 'concurrent/goroutine'
require 'concurrent/obligation'
require 'concurrent/promise'
require 'concurrent/runnable'
require 'concurrent/supervisor'
require 'concurrent/utilities'

require 'concurrent/cached_thread_pool'
require 'concurrent/fixed_thread_pool'
Expand Down
3 changes: 2 additions & 1 deletion lib/concurrent/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'observer'

require 'concurrent/global_thread_pool'
require 'concurrent/utilities'

module Concurrent

Expand Down Expand Up @@ -93,7 +94,7 @@ def try_rescue(ex) # :nodoc:
def work(&handler) # :nodoc:
begin
@mutex.synchronize do
result = Timeout.timeout(@timeout) do
result = Concurrent::timeout(@timeout) do
handler.call(@value)
end
if @validator.nil? || @validator.call(result)
Expand Down
11 changes: 4 additions & 7 deletions lib/concurrent/event.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'thread'
require 'timeout'
require 'concurrent/utilities'

module Concurrent

Expand Down Expand Up @@ -39,15 +39,12 @@ def wait(timeout = nil)
if timeout.nil?
@notifier.pop
else
countdown = timeout.ceil
while @notifier.empty? && countdown > 0
sleep(1)
countdown -= 1
Concurrent::timeout(timeout) do
@notifier.pop
end
@notifier.pop(true)
end
return true
rescue ThreadError
rescue
@mutex.synchronize { @waiting -= 1 }
return false
end
Expand Down
24 changes: 24 additions & 0 deletions lib/concurrent/utilities.rb
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
2 changes: 1 addition & 1 deletion spec/concurrent/obligation_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
end

it 'returns immediately when timeout is zero' do
Timeout.should_not_receive(:timeout).with(any_args())
Concurrent.should_not_receive(:timeout).with(any_args())
f = pending_subject
f.value(0).should be_nil
f.should be_pending
Expand Down
2 changes: 1 addition & 1 deletion spec/concurrent/supervisor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def mock_thread(status = 'run')
@thread = Thread.new{ sleep(0.5); supervisor.stop }
sleep(0.1)
lambda {
Timeout::timeout(1){ supervisor.run }
Concurrent::timeout(1){ supervisor.run }
}.should_not raise_error
Thread.kill(@thread) unless @thread.nil?
end
Expand Down
50 changes: 50 additions & 0 deletions spec/concurrent/utilities_spec.rb
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

0 comments on commit e8371c0

Please sign in to comment.