root/trunk/tests/test_basic.rb

Revision 788, 6.0 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 $:.unshift File.expand_path(File.dirname(__FILE__) + "/../lib")
28 require 'eventmachine'
29 require 'test/unit'
30
31 class TestBasic < Test::Unit::TestCase
32
33   def setup
34     assert(!EM.reactor_running?)
35   end
36
37   def teardown
38     assert(!EM.reactor_running?)
39   end
40
41   #-------------------------------------
42
43   def test_libtype
44     lt = EventMachine.library_type
45                 em_lib = (ENV["EVENTMACHINE_LIBRARY"] || $eventmachine_library || :xxx).to_sym
46                
47                 # Running from test runner, under jruby.
48                 if RUBY_PLATFORM == 'java'
49                         unless em_lib == :pure_ruby
50                                 assert_equal( :java, lt )
51                                 return
52                         end
53                 end
54                
55     case em_lib
56     when :pure_ruby
57       assert_equal( :pure_ruby, lt )
58     when :extension
59       assert_equal( :extension, lt )
60     when :java
61       assert_equal( :java, lt )
62     else
63                         # Running from jruby as a standalone test.
64                         if RUBY_PLATFORM == 'java'
65                                 assert_equal( :java, lt )
66                         else
67         assert_equal( :extension, lt )
68                         end
69     end
70   end
71
72   #-------------------------------------
73
74
75   def test_em
76     EventMachine.run {
77       EventMachine.add_timer 0 do
78         EventMachine.stop
79       end
80     }
81   end
82
83   #-------------------------------------
84
85   def test_timer
86     n = 0
87     EventMachine.run {
88       EventMachine.add_periodic_timer(0.1) {
89         n += 1
90         EventMachine.stop if n == 2
91       }
92     }
93   end
94
95   #-------------------------------------
96
97   # This test once threw an already-running exception.
98   module Trivial
99     def post_init
100       EventMachine.stop
101     end
102   end
103
104   def test_server
105     EventMachine.run {
106       EventMachine.start_server "localhost", 9000, Trivial
107       EventMachine.connect "localhost", 9000
108     }
109     assert( true ) # make sure it halts
110   end
111
112   #--------------------------------------
113
114   # EventMachine#run_block starts the reactor loop, runs the supplied block, and then STOPS
115   # the loop automatically. Contrast with EventMachine#run, which keeps running the reactor
116   # even after the supplied block completes.
117   def test_run_block
118     assert !EM.reactor_running?
119       a = nil
120       EM.run_block { a = "Worked" }
121       assert a
122       assert !EM.reactor_running?
123   end
124
125
126   #--------------------------------------
127
128   # TODO! This is an unfinished edge case.
129   # EM mishandles uncaught Ruby exceptions that fire from within #unbind handlers.
130   # A uncaught Ruby exception results in a call to EM::release_machine (which is in an ensure
131   # block in EM::run). But if EM is processing an unbind request, the release_machine call
132   # will cause a segmentation fault.
133   #
134
135   TestHost = "127.0.0.1"
136   TestPort = 9070
137
138   class UnbindError < EM::Connection
139           def initialize *args
140                   super
141           end
142           def connection_completed
143                   close_connection_after_writing
144           end
145           def unbind
146                   raise "Blooey"
147           end
148   end
149
150   def xxx_test_unbind_error
151           assert_raise( RuntimeError ) {
152                   EM.run {
153                           EM.start_server TestHost, TestPort
154                           EM.connect TestHost, TestPort, UnbindError
155                   }
156           }
157   end
158
159   #------------------------------------
160   #
161   # TODO. This is an unfinished bug fix.
162   # This case was originally reported by Dan Aquino. If you throw a Ruby exception
163   # in a post_init handler, it gets rethrown as a confusing reactor exception.
164   # The problem is in eventmachine.rb, which calls post_init within the private
165   # initialize method of the EM::Connection class. This happens in both the EM::connect
166   # method and in the code that responds to connection-accepted events.
167   # What happens is that we instantiate the new connection object, which calls
168   # initialize, and then after initialize returns, we stick the new connection object
169   # into EM's @conns hashtable.
170   # But the problem is that Connection::initialize calls #post_init before it returns,
171   # and this may be user-written code that may throw an uncaught Ruby exception.
172   # If that happens, the reactor will abort, and it will then try to run down open
173   # connections. Because @conns never got a chance to properly reflect the new connection
174   # (because initialize never returned), we throw a ConnectionNotBound error
175   # (eventmachine.rb line 1080).
176   # When the bug is fixed, activate this test case.
177   #
178
179   class PostInitError < EM::Connection
180           def post_init
181                   aaa bbb # should produce a Ruby exception
182           end
183   end
184   # This test causes issues, the machine becomes unreleasable after
185   # release_machine suffers an exception in event_callback.
186   def xxx_test_post_init_error
187           assert_raise( EventMachine::ConnectionNotBound ) {
188                   EM.run {
189                         EM::Timer.new(1) {EM.stop}
190                         EM.start_server TestHost, TestPort
191                         EM.connect TestHost, TestPort, PostInitError
192                   }
193           }
194           EM.run {
195             EM.stop
196           }
197           assert !EM.reactor_running?
198   end
199  
200   module BrsTestSrv
201     def receive_data data
202       $received << data
203     end
204     def unbind
205       EM.stop
206     end
207   end
208   module BrsTestCli
209     def post_init
210       send_data $sent
211       close_connection_after_writing
212     end
213   end
214  
215   # From ticket #50
216   def test_byte_range_send
217     $received = ''
218     $sent = (0..255).to_a.pack('C*')
219     EM::run {
220      
221       EM::start_server TestHost, TestPort, BrsTestSrv
222      
223       EM::connect TestHost, TestPort, BrsTestCli
224      
225       EM::add_timer(0.5) { assert(false, 'test timed out'); EM.stop; Kernel.warn "test timed out!" }
226     }
227     assert_equal($sent, $received)
228   end
229
230 end
231
Note: See TracBrowser for help on using the browser.