Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add crank_until, add example using EM.run_machine, linear tests!

  • Loading branch information...
commit 2741ee179d07e0a95003ba1fbb734e33ae804ae7 1 parent 48be293
@raggi raggi authored
Showing with 41 additions and 10 deletions.
  1. +13 −5 lib/eventmachine.rb
  2. +28 −5 tests/test_crank.rb
View
18 lib/eventmachine.rb
@@ -243,17 +243,25 @@ def self.run blk=nil, tail=nil, &block
raise @wrapped_exception if @wrapped_exception
end
-
+
def self.start_crank
ruby_setup
_start_crank
end
-
+
def self.stop_crank
_stop_crank
ruby_teardown
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
@conns = {}
@acceptors = {}
@@ -266,7 +274,7 @@ def self.ruby_setup
end
@reactor_thread = Thread.current
end
- # private_module_method :ruby_setup
+ private_class_method :ruby_setup
def self.ruby_teardown
@@ -287,7 +295,7 @@ def self.ruby_teardown
@reactor_running = false
@reactor_thread = nil
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
# the reactor loop and exit the function as soon as the code in the block completes.
View
33 tests/test_crank.rb
@@ -3,17 +3,40 @@
require 'test/unit'
class TestEventMachineCrank < Test::Unit::TestCase
- def test_crank
- count = 0
+ def setup
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
EM.crank
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
+ 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
Please sign in to comment.
Something went wrong with that request. Please try again.