Skip to content

Commit

Permalink
allow passing args to the deferrable timeout method which will then p…
Browse files Browse the repository at this point in the history
…ropagate to the errback

Signed-off-by: Aman Gupta <[email protected]>
  • Loading branch information
dj2 authored and tmm1 committed Mar 26, 2010
1 parent f9ad9f6 commit 2f32eac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/em/deferrable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ def set_deferred_status status, *args
# the Timeout expires (passing no arguments to the object's errbacks).
# Setting the status at any time prior to a call to the expiration of the timeout
# will cause the timer to be cancelled.
def timeout seconds
def timeout seconds, *args
cancel_timeout
me = self
@deferred_timeout = EventMachine::Timer.new(seconds) {me.fail}
@deferred_timeout = EventMachine::Timer.new(seconds) {me.fail(*args)}
end

# Cancels an outstanding timeout if any. Undoes the action of #timeout.
Expand Down
35 changes: 35 additions & 0 deletions tests/test_deferrable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
$:.unshift "../lib"
require 'eventmachine'
require 'test/unit'

class TestDeferrable < Test::Unit::TestCase
class Later
include EM::Deferrable
end

def test_timeout_without_args
$args = "unset"

EM.run {
df = Later.new
df.timeout(0.2)
df.errback { $args = "none" }
EM.add_timer(0.5) { EM.stop }
}

assert_equal("none", $args)
end

def test_timeout_with_args
$args = "unset"

EM.run {
df = Later.new
df.timeout(0.2, :timeout, :foo)
df.errback { |type, name| $args = [type, name] }
EM.add_timer(0.5) { EM.stop }
}

assert_equal([:timeout, :foo], $args)
end
end

0 comments on commit 2f32eac

Please sign in to comment.