Skip to content

Commit

Permalink
Add crank_until, add example using EM.run_machine, linear tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
raggi committed Jun 11, 2009
1 parent 48be293 commit 2741ee1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
18 changes: 13 additions & 5 deletions lib/eventmachine.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -243,17 +243,25 @@ def self.run blk=nil, tail=nil, &block


raise @wrapped_exception if @wrapped_exception raise @wrapped_exception if @wrapped_exception
end end

def self.start_crank def self.start_crank
ruby_setup ruby_setup
_start_crank _start_crank
end end

def self.stop_crank def self.stop_crank
_stop_crank _stop_crank
ruby_teardown ruby_teardown
end end


def self.crank_until(timeout = nil)
timedout = false
crank_until_timer = Timer.new(timeout) { timedout = true } if timeout
EM.crank until timedout || yield
crank_until_timer && crank_until_timer.cancel
!timedout
end

def self.ruby_setup def self.ruby_setup
@conns = {} @conns = {}
@acceptors = {} @acceptors = {}
Expand All @@ -266,7 +274,7 @@ def self.ruby_setup
end end
@reactor_thread = Thread.current @reactor_thread = Thread.current
end end
# private_module_method :ruby_setup private_class_method :ruby_setup




def self.ruby_teardown def self.ruby_teardown
Expand All @@ -287,7 +295,7 @@ def self.ruby_teardown
@reactor_running = false @reactor_running = false
@reactor_thread = nil @reactor_thread = nil
end end
# private_module_method :ruby_cleanup private_class_method :ruby_teardown


# Sugars a common use case. Will pass the given block to #run, but will terminate # Sugars a common use case. Will pass the given block to #run, but will terminate
# the reactor loop and exit the function as soon as the code in the block completes. # the reactor loop and exit the function as soon as the code in the block completes.
Expand Down
33 changes: 28 additions & 5 deletions tests/test_crank.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,17 +3,40 @@
require 'test/unit' require 'test/unit'


class TestEventMachineCrank < Test::Unit::TestCase class TestEventMachineCrank < Test::Unit::TestCase
def test_crank def setup
count = 0
EM.start_crank EM.start_crank
end


EM.add_periodic_timer(0) { count += 1 } def teardown
EM.stop_crank
end

def test_crank
count = 0
EM.next_tick { count += 1 }
assert_equal 0, count assert_equal 0, count
EM.crank EM.crank
assert_equal 1, count assert_equal 1, count
EM.crank end

def test_crank_until
count = 0
EM.add_periodic_timer(0) { count += 1 }
EM.crank_until { count > 1 }
assert_equal 2, count assert_equal 2, count
end


EM.stop_crank def test_crank_until_timeout
count = 0
EM.add_periodic_timer(0) { count += 1 }
EM.crank_until(0) { count > 10 }
assert_equal 1, count
end

def test_run_machine
ran = false
EM.next_tick { ran = true; EM.stop }
EM.run_machine
assert ran
end end
end end

0 comments on commit 2741ee1

Please sign in to comment.