PageRenderTime 23ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test_timers.rb

https://github.com/goking/eventmachine
Ruby | 160 lines | 114 code | 17 blank | 29 comment | 13 complexity | 4cdb94332b7272bd41024398c843497a MD5 | raw file
  1. # $Id$
  2. #
  3. # Author:: Francis Cianfrocca (gmail: blackhedd)
  4. # Homepage:: http://rubyeventmachine.com
  5. # Date:: 8 April 2006
  6. #
  7. # See EventMachine and EventMachine::Connection for documentation and
  8. # usage examples.
  9. #
  10. #----------------------------------------------------------------------------
  11. #
  12. # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
  13. # Gmail: blackhedd
  14. #
  15. # This program is free software; you can redistribute it and/or modify
  16. # it under the terms of either: 1) the GNU General Public License
  17. # as published by the Free Software Foundation; either version 2 of the
  18. # License, or (at your option) any later version; or 2) Ruby's License.
  19. #
  20. # See the file COPYING for complete licensing information.
  21. #
  22. #---------------------------------------------------------------------------
  23. #
  24. #
  25. #
  26. #
  27. $:.unshift "../lib"
  28. require 'eventmachine'
  29. require 'test/unit'
  30. class TestTimers < Test::Unit::TestCase
  31. def test_timer_with_block
  32. x = false
  33. EventMachine.run {
  34. EventMachine::Timer.new(0.25) {
  35. x = true
  36. EventMachine.stop
  37. }
  38. }
  39. assert x
  40. end
  41. def test_timer_with_proc
  42. x = false
  43. EventMachine.run {
  44. EventMachine::Timer.new(0.25, proc {
  45. x = true
  46. EventMachine.stop
  47. })
  48. }
  49. assert x
  50. end
  51. def test_timer_cancel
  52. x = true
  53. EventMachine.run {
  54. timer = EventMachine::Timer.new(0.25, proc { x = false })
  55. timer.cancel
  56. EventMachine::Timer.new(0.5, proc {EventMachine.stop})
  57. }
  58. assert x
  59. end
  60. def test_periodic_timer
  61. x = 0
  62. EventMachine.run {
  63. EventMachine::PeriodicTimer.new(0.1) do
  64. x += 1
  65. EventMachine.stop if x == 4
  66. end
  67. }
  68. assert( x == 4 )
  69. end
  70. def test_add_periodic_timer
  71. x = 0
  72. EM.run {
  73. t = EM.add_periodic_timer(0.1) do
  74. x += 1
  75. EM.stop if x == 4
  76. end
  77. assert t.respond_to?(:cancel)
  78. }
  79. end
  80. def test_periodic_timer_cancel
  81. x = 0
  82. EventMachine.run {
  83. pt = EventMachine::PeriodicTimer.new(0.25, proc { x += 1 })
  84. pt.cancel
  85. EventMachine::Timer.new(0.5) {EventMachine.stop}
  86. }
  87. assert( x == 0 )
  88. end
  89. def test_add_periodic_timer_cancel
  90. x = 0
  91. EventMachine.run {
  92. pt = EM.add_periodic_timer(0.1) { x += 1 }
  93. EM.cancel_timer(pt)
  94. EM.add_timer(0.2) { EM.stop }
  95. }
  96. assert( x == 0 )
  97. end
  98. def test_periodic_timer_self_cancel
  99. x = 0
  100. EventMachine.run {
  101. pt = EventMachine::PeriodicTimer.new(0.1) {
  102. x += 1
  103. if x == 4
  104. pt.cancel
  105. EventMachine.stop
  106. end
  107. }
  108. }
  109. assert( x == 4 )
  110. end
  111. # This test is only applicable to compiled versions of the reactor.
  112. # Pure ruby and java versions have no built-in limit on the number of outstanding timers.
  113. #
  114. def test_timer_change_max_outstanding
  115. defaults = EM.get_max_timers
  116. EM.set_max_timers(100)
  117. one_hundred_one_timers = proc { 101.times { EM.add_timer(5) {} } }
  118. EM.run {
  119. if EM.library_type == :pure_ruby
  120. one_hundred_one_timers.call
  121. elsif EM.library_type == :java
  122. one_hundred_one_timers.call
  123. else
  124. begin
  125. assert_raises( RuntimeError ) {
  126. one_hundred_one_timers.call
  127. }
  128. rescue Object
  129. p $!
  130. assert(false, $!.message)
  131. end
  132. end
  133. EM.stop
  134. }
  135. EM.set_max_timers( 101 )
  136. EM.run {
  137. one_hundred_one_timers.call
  138. EM.stop
  139. }
  140. ensure
  141. EM.set_max_timers(defaults)
  142. end
  143. end