summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/eventmachine-1.2.7/tests/test_timers.rb
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/bundle/ruby/3.4.0/gems/eventmachine-1.2.7/tests/test_timers.rb')
-rw-r--r--vendor/bundle/ruby/3.4.0/gems/eventmachine-1.2.7/tests/test_timers.rb130
1 files changed, 130 insertions, 0 deletions
diff --git a/vendor/bundle/ruby/3.4.0/gems/eventmachine-1.2.7/tests/test_timers.rb b/vendor/bundle/ruby/3.4.0/gems/eventmachine-1.2.7/tests/test_timers.rb
new file mode 100644
index 0000000..88b3b78
--- /dev/null
+++ b/vendor/bundle/ruby/3.4.0/gems/eventmachine-1.2.7/tests/test_timers.rb
@@ -0,0 +1,130 @@
+require 'em_test_helper'
+
+class TestTimers < Test::Unit::TestCase
+
+ def test_timer_with_block
+ x = false
+ EM.run {
+ EM::Timer.new(0) {
+ x = true
+ EM.stop
+ }
+ }
+ assert x
+ end
+
+ def test_timer_with_proc
+ x = false
+ EM.run {
+ EM::Timer.new(0, proc {
+ x = true
+ EM.stop
+ })
+ }
+ assert x
+ end
+
+ def test_timer_cancel
+ assert_nothing_raised do
+ EM.run {
+ timer = EM::Timer.new(0.01) { flunk "Timer was not cancelled." }
+ timer.cancel
+
+ EM.add_timer(0.02) { EM.stop }
+ }
+ end
+ end
+
+ def test_periodic_timer
+ x = 0
+ EM.run {
+ EM::PeriodicTimer.new(0.01) do
+ x += 1
+ EM.stop if x == 4
+ end
+ }
+
+ assert_equal 4, x
+ end
+
+ def test_add_periodic_timer
+ x = 0
+ EM.run {
+ t = EM.add_periodic_timer(0.01) do
+ x += 1
+ EM.stop if x == 4
+ end
+ assert t.respond_to?(:cancel)
+ }
+ assert_equal 4, x
+ end
+
+ def test_periodic_timer_cancel
+ x = 0
+ EM.run {
+ pt = EM::PeriodicTimer.new(0.01) { x += 1 }
+ pt.cancel
+ EM::Timer.new(0.02) { EM.stop }
+ }
+ assert_equal 0, x
+ end
+
+ def test_add_periodic_timer_cancel
+ x = 0
+ EM.run {
+ pt = EM.add_periodic_timer(0.01) { x += 1 }
+ EM.cancel_timer(pt)
+ EM.add_timer(0.02) { EM.stop }
+ }
+ assert_equal 0, x
+ end
+
+ def test_periodic_timer_self_cancel
+ x = 0
+ EM.run {
+ pt = EM::PeriodicTimer.new(0) {
+ x += 1
+ if x == 4
+ pt.cancel
+ EM.stop
+ end
+ }
+ }
+ assert_equal 4, x
+ end
+
+ def test_oneshot_timer_large_future_value
+ large_value = 11948602000
+ EM.run {
+ EM.add_timer(large_value) { EM.stop }
+ EM.add_timer(0.02) { EM.stop }
+ }
+ end
+
+ # This test is only applicable to compiled versions of the reactor.
+ # Pure ruby and java versions have no built-in limit on the number of outstanding timers.
+ unless [:pure_ruby, :java].include? EM.library_type
+ def test_timer_change_max_outstanding
+ defaults = EM.get_max_timers
+ EM.set_max_timers(100)
+
+ one_hundred_one_timers = lambda do
+ 101.times { EM.add_timer(0.01) {} }
+ EM.stop
+ end
+
+ assert_raises(RuntimeError) do
+ EM.run( &one_hundred_one_timers )
+ end
+
+ EM.set_max_timers( 101 )
+
+ assert_nothing_raised do
+ EM.run( &one_hundred_one_timers )
+ end
+ ensure
+ EM.set_max_timers(defaults)
+ end
+ end
+
+end