root/trunk/tests/test_timers.rb

Revision 788, 2.9 kB (checked in by raggi, 9 months ago)

Merge of branches/raggi
Most notable work and patches by Aman Gupta, Roger Pack, and James Tucker.
Patches / Tickets also submitted by: Jeremy Evans, aanand, darix, mmmurf,
danielaquino, macournoyer.

  • Moved docs into docs/ dir
  • Major refactor of rakefile, added generic rakefile helpers in tasks
  • Added example CPP build rakefile in tasks/cpp.rake
  • Moved rake tests out to tasks/tests.rake
  • Added svn ignores where appropriate
  • Fixed jruby build on older java platforms
  • Gem now builds from Rakefile rather than directly via extconf
  • Gem unified for jruby, C++ and pure ruby.
  • Correction for pure C++ build, removing ruby dependency
  • Fix for CYGWIN builds on ipv6
  • Major refactor for extconf.rb
  • Working mingw builds
  • extconf optionally uses pkg_config over manual configuration
  • extconf builds for 1.9 on any system that has 1.9
  • extconf no longer links pthread explicitly
  • looks for kqueue on all *nix systems
  • better error output on std::runtime_error, now says where it came from
  • Fixed some tests on jruby
  • Added test for general send_data flaw, required for a bugfix in jruby build
  • Added timeout to epoll tests
  • Added fixes for java reactor ruby api
  • Small addition of some docs in httpclient.rb and httpcli2.rb
  • Some refactor and fixes in smtpserver.rb
  • Added parenthesis where possible to avoid excess ruby warnings
  • Refactor of $eventmachine_library logic for accuracy and maintenance, jruby
  • EM::start_server now supports unix sockets
  • EM::connect now supports unix sockets
  • EM::defer @threadqueue now handled more gracefully
  • Added better messages on exceptions raised
  • Fix edge case in timer fires
  • Explicitly require buftok.rb
  • Add protocols to autoload, rather than require them all immediately
  • Fix a bug in pr_eventmachine for outbound_q
  • Refactors to take some of the use of defer out of tests.
  • Fixes in EM.defer under start/stop conditions. Reduced scope of threads.
  • Property svn:keywords set to Id
Line 
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
28 $:.unshift "../lib"
29 require 'eventmachine'
30 require 'test/unit'
31
32
33 class TestTimers < Test::Unit::TestCase
34
35   def setup
36   end
37
38   def teardown
39   end
40
41   def test_timer_with_block
42           x = false
43           EventMachine.run {
44                   EventMachine::Timer.new(0.25) {
45                         x = true
46                         EventMachine.stop
47                 }
48           }
49           assert x
50   end
51
52   def test_timer_with_proc
53           x = false
54           EventMachine.run {
55                   EventMachine::Timer.new(0.25, proc {
56                         x = true
57                         EventMachine.stop
58                 })
59           }
60           assert x
61   end
62
63   def test_timer_cancel
64           x = true
65           EventMachine.run {
66                   timer = EventMachine::Timer.new(0.25, proc { x = false })
67                   timer.cancel
68                   EventMachine::Timer.new(0.5, proc {EventMachine.stop})
69           }
70           assert x
71   end
72
73   def test_periodic_timer
74           x = 0
75           EventMachine.run {
76                   EventMachine::PeriodicTimer.new(0.1) do
77                         x += 1
78                           EventMachine.stop if x == 4
79             end
80           }
81           assert( x == 4 )
82   end
83
84   def test_periodic_timer_cancel
85           x = 0
86           EventMachine.run {
87                 pt = EventMachine::PeriodicTimer.new(0.25, proc { x += 1 })
88                 pt.cancel
89                 EventMachine::Timer.new(0.5) {EventMachine.stop}
90           }
91           assert( x == 0 )
92   end
93
94   def test_periodic_timer_self_cancel
95           x = 0
96           EventMachine.run {
97                 pt = EventMachine::PeriodicTimer.new(0.1) {
98                   x += 1
99                   if x == 4
100                           pt.cancel
101                           EventMachine.stop
102                   end
103                 }
104           }
105           assert( x == 4 )
106   end
107
108
109         # This test is only applicable to compiled versions of the reactor.
110         # Pure ruby and java versions have no built-in limit on the number of outstanding timers.
111         #
112         def test_timer_change_max_outstanding
113                 ten_thousand_timers = proc {
114                         10000.times {
115                                 EM.add_timer(5) {}
116                         }
117                 }
118                 EM.run {
119                         if EM.library_type == :pure_ruby
120                                 ten_thousand_timers.call
121                         elsif EM.library_type == :java
122                                 ten_thousand_timers.call
123                         else
124                           begin
125                                 assert_raise( RuntimeError ) {
126                                         ten_thousand_timers.call
127                                 }
128                         rescue Object
129                           p $!
130                           assert(false, $!.message)
131                   end
132                         end
133                         EM.stop
134                 }
135
136     assert(!EM.reactor_running?, 'Reactor running when it should not be.')
137     EM.set_max_timers( 10001 )
138
139                 EM.run {
140                         ten_thousand_timers.call
141                         EM.stop
142                 }
143         end
144
145 end
146
Note: See TracBrowser for help on using the browser.