--- /dev/null
+# $Id$\r
+#\r
+# Author:: Francis Cianfrocca (gmail: blackhedd)\r
+# Homepage:: http://rubyeventmachine.com\r
+# Date:: 8 April 2006\r
+# \r
+# See EventMachine and EventMachine::Connection for documentation and\r
+# usage examples.\r
+#\r
+#----------------------------------------------------------------------------\r
+#\r
+# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.\r
+# Gmail: blackhedd\r
+# \r
+# This program is free software; you can redistribute it and/or modify\r
+# it under the terms of either: 1) the GNU General Public License\r
+# as published by the Free Software Foundation; either version 2 of the\r
+# License, or (at your option) any later version; or 2) Ruby's License.\r
+# \r
+# See the file COPYING for complete licensing information.\r
+#\r
+#---------------------------------------------------------------------------\r
+#\r
+#\r
+# \r
+\r
+$:.unshift "../lib"\r
+require 'eventmachine'\r
+require 'test/unit'\r
+\r
+class TestPure < Test::Unit::TestCase\r
+\r
+\r
+ Host,Port = "0.0.0.0", 9060\r
+\r
+\r
+ # These tests are intended to exercise problems that come up in the\r
+ # pure-Ruby implementation. However, we DON'T constrain them such that\r
+ # they only run in pure-Ruby. These tests need to work identically in\r
+ # any implementation.\r
+\r
+ def setup\r
+ end\r
+\r
+ def teardown\r
+ end\r
+\r
+ #-------------------------------------\r
+\r
+ # The EM reactor needs to run down open connections and release other resources\r
+ # when it stops running. Make sure this happens even if user code throws a Ruby\r
+ # exception.\r
+ # One way to see this is to run identical tests that open a TCP server and throw\r
+ # an exception. (We do this twice because an exception aborts a test. We make the\r
+ # two tests identical except for the method name because we can't predict the order\r
+ # in which the test harness will run them.)\r
+ # If exception handling is incorrect, the second test will fail with a no-bind error\r
+ # because the TCP server opened in the first test will not have been closed.\r
+ #\r
+ def run_exception\r
+ EM.run {\r
+ EM.start_server Host, Port\r
+ raise "an exception"\r
+ }\r
+ end\r
+ def test_exception_1\r
+ assert_raises( RuntimeError ) { run_exception }\r
+ end\r
+ def test_exception_2\r
+ ex_class = RUBY_PLATFORM == 'java' ? NativeException : RuntimeError\r
+ assert_raises( ex_class ) { run_exception }\r
+ end\r
+\r
+\r
+ # Under some circumstances, the pure Ruby library would emit an Errno::ECONNREFUSED\r
+ # exception on certain kinds of TCP connect-errors.\r
+ # It's always been something of an open question whether EM should throw an exception\r
+ # in these cases but the defined answer has always been to catch it the unbind method.\r
+ # With a connect failure, the latter will always fire, but connection_completed will\r
+ # never fire. So even though the point is arguable, it's incorrect for the pure Ruby\r
+ # version to throw an exception.\r
+ module TestConnrefused\r
+ def unbind\r
+ EM.stop\r
+ end\r
+ def connection_completed\r
+ raise "should never get here"\r
+ end\r
+ end\r
+ def test_connrefused\r
+ EM.run {\r
+ EM.connect "0.0.0.0", 60001, TestConnrefused\r
+ }\r
+ end\r
+\r
+\r
+ # Make sure connection_completed gets called as expected with TCP clients. This is the\r
+ # opposite of test_connrefused.\r
+ # If the test fails, it will hang because EM.stop never gets called.\r
+ #\r
+ module TestConnaccepted\r
+ def connection_completed\r
+ EM.stop\r
+ end\r
+ end\r
+ def test_connaccepted\r
+ timeout = false\r
+ EM.run {\r
+ EM.start_server "0.0.0.0", 60002\r
+ EM.connect "0.0.0.0", 60002, TestConnaccepted\r
+ EM::Timer.new(1) {timeout = true; EM.stop}\r
+ }\r
+ assert_equal( false, timeout )\r
+ end\r
+\r
+ def test_reactor_running\r
+ a = false\r
+ EM.run {\r
+ a = EM.reactor_running?\r
+ EM.next_tick {EM.stop}\r
+ }\r
+ assert a\r
+ end\r
+\r
+end\r