wj_eventmachine 1.3.0.dev.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +179 -0
  3. data/GNU +281 -0
  4. data/LICENSE +60 -0
  5. data/README.md +110 -0
  6. data/docs/DocumentationGuidesIndex.md +27 -0
  7. data/docs/GettingStarted.md +520 -0
  8. data/docs/old/ChangeLog +211 -0
  9. data/docs/old/DEFERRABLES +246 -0
  10. data/docs/old/EPOLL +141 -0
  11. data/docs/old/INSTALL +13 -0
  12. data/docs/old/KEYBOARD +42 -0
  13. data/docs/old/LEGAL +25 -0
  14. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  15. data/docs/old/PURE_RUBY +75 -0
  16. data/docs/old/RELEASE_NOTES +94 -0
  17. data/docs/old/SMTP +4 -0
  18. data/docs/old/SPAWNED_PROCESSES +148 -0
  19. data/docs/old/TODO +8 -0
  20. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  21. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  22. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  23. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  24. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  25. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  26. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  27. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  28. data/examples/old/ex_channel.rb +43 -0
  29. data/examples/old/ex_queue.rb +2 -0
  30. data/examples/old/ex_tick_loop_array.rb +15 -0
  31. data/examples/old/ex_tick_loop_counter.rb +32 -0
  32. data/examples/old/helper.rb +2 -0
  33. data/ext/binder.cpp +124 -0
  34. data/ext/binder.h +52 -0
  35. data/ext/cmain.cpp +1046 -0
  36. data/ext/ed.cpp +2238 -0
  37. data/ext/ed.h +460 -0
  38. data/ext/em.cpp +2378 -0
  39. data/ext/em.h +266 -0
  40. data/ext/eventmachine.h +152 -0
  41. data/ext/extconf.rb +285 -0
  42. data/ext/fastfilereader/extconf.rb +120 -0
  43. data/ext/fastfilereader/mapper.cpp +214 -0
  44. data/ext/fastfilereader/mapper.h +59 -0
  45. data/ext/fastfilereader/rubymain.cpp +126 -0
  46. data/ext/kb.cpp +79 -0
  47. data/ext/page.cpp +107 -0
  48. data/ext/page.h +51 -0
  49. data/ext/pipe.cpp +354 -0
  50. data/ext/project.h +174 -0
  51. data/ext/rubymain.cpp +1610 -0
  52. data/ext/ssl.cpp +627 -0
  53. data/ext/ssl.h +103 -0
  54. data/ext/wait_for_single_fd.h +36 -0
  55. data/java/.classpath +8 -0
  56. data/java/.project +17 -0
  57. data/java/src/com/rubyeventmachine/EmReactor.java +625 -0
  58. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  59. data/java/src/com/rubyeventmachine/EmReactorInterface.java +70 -0
  60. data/java/src/com/rubyeventmachine/EventableChannel.java +72 -0
  61. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +201 -0
  62. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +415 -0
  63. data/java/src/com/rubyeventmachine/NullEmReactor.java +157 -0
  64. data/java/src/com/rubyeventmachine/NullEventableChannel.java +81 -0
  65. data/lib/em/buftok.rb +59 -0
  66. data/lib/em/callback.rb +58 -0
  67. data/lib/em/channel.rb +69 -0
  68. data/lib/em/completion.rb +307 -0
  69. data/lib/em/connection.rb +776 -0
  70. data/lib/em/deferrable.rb +210 -0
  71. data/lib/em/deferrable/pool.rb +2 -0
  72. data/lib/em/file_watch.rb +73 -0
  73. data/lib/em/future.rb +61 -0
  74. data/lib/em/io_streamer.rb +68 -0
  75. data/lib/em/iterator.rb +252 -0
  76. data/lib/em/messages.rb +66 -0
  77. data/lib/em/pool.rb +151 -0
  78. data/lib/em/process_watch.rb +45 -0
  79. data/lib/em/processes.rb +123 -0
  80. data/lib/em/protocols.rb +37 -0
  81. data/lib/em/protocols/header_and_content.rb +138 -0
  82. data/lib/em/protocols/httpclient.rb +303 -0
  83. data/lib/em/protocols/httpclient2.rb +602 -0
  84. data/lib/em/protocols/line_and_text.rb +125 -0
  85. data/lib/em/protocols/line_protocol.rb +33 -0
  86. data/lib/em/protocols/linetext2.rb +179 -0
  87. data/lib/em/protocols/memcache.rb +331 -0
  88. data/lib/em/protocols/object_protocol.rb +46 -0
  89. data/lib/em/protocols/postgres3.rb +246 -0
  90. data/lib/em/protocols/saslauth.rb +175 -0
  91. data/lib/em/protocols/smtpclient.rb +394 -0
  92. data/lib/em/protocols/smtpserver.rb +666 -0
  93. data/lib/em/protocols/socks4.rb +66 -0
  94. data/lib/em/protocols/stomp.rb +205 -0
  95. data/lib/em/protocols/tcptest.rb +54 -0
  96. data/lib/em/pure_ruby.rb +1299 -0
  97. data/lib/em/queue.rb +80 -0
  98. data/lib/em/resolver.rb +232 -0
  99. data/lib/em/spawnable.rb +84 -0
  100. data/lib/em/streamer.rb +118 -0
  101. data/lib/em/threaded_resource.rb +90 -0
  102. data/lib/em/tick_loop.rb +85 -0
  103. data/lib/em/timers.rb +61 -0
  104. data/lib/em/version.rb +3 -0
  105. data/lib/eventmachine.rb +1602 -0
  106. data/lib/jeventmachine.rb +318 -0
  107. data/rakelib/package.rake +120 -0
  108. data/rakelib/test.rake +6 -0
  109. data/rakelib/test_pure.rake +11 -0
  110. data/tests/client.crt +31 -0
  111. data/tests/client.key +51 -0
  112. data/tests/dhparam.pem +13 -0
  113. data/tests/em_ssl_handlers.rb +153 -0
  114. data/tests/em_test_helper.rb +198 -0
  115. data/tests/jruby/test_jeventmachine.rb +38 -0
  116. data/tests/test_attach.rb +199 -0
  117. data/tests/test_basic.rb +321 -0
  118. data/tests/test_channel.rb +75 -0
  119. data/tests/test_completion.rb +178 -0
  120. data/tests/test_connection_count.rb +83 -0
  121. data/tests/test_connection_write.rb +35 -0
  122. data/tests/test_defer.rb +35 -0
  123. data/tests/test_deferrable.rb +35 -0
  124. data/tests/test_epoll.rb +141 -0
  125. data/tests/test_error_handler.rb +38 -0
  126. data/tests/test_exc.rb +37 -0
  127. data/tests/test_file_watch.rb +86 -0
  128. data/tests/test_fork.rb +75 -0
  129. data/tests/test_futures.rb +170 -0
  130. data/tests/test_handler_check.rb +35 -0
  131. data/tests/test_hc.rb +155 -0
  132. data/tests/test_httpclient.rb +238 -0
  133. data/tests/test_httpclient2.rb +132 -0
  134. data/tests/test_idle_connection.rb +31 -0
  135. data/tests/test_inactivity_timeout.rb +102 -0
  136. data/tests/test_io_streamer.rb +47 -0
  137. data/tests/test_ipv4.rb +96 -0
  138. data/tests/test_ipv6.rb +107 -0
  139. data/tests/test_iterator.rb +122 -0
  140. data/tests/test_kb.rb +28 -0
  141. data/tests/test_keepalive.rb +113 -0
  142. data/tests/test_line_protocol.rb +33 -0
  143. data/tests/test_ltp.rb +155 -0
  144. data/tests/test_ltp2.rb +332 -0
  145. data/tests/test_many_fds.rb +21 -0
  146. data/tests/test_next_tick.rb +104 -0
  147. data/tests/test_object_protocol.rb +36 -0
  148. data/tests/test_pause.rb +109 -0
  149. data/tests/test_pending_connect_timeout.rb +52 -0
  150. data/tests/test_pool.rb +196 -0
  151. data/tests/test_process_watch.rb +50 -0
  152. data/tests/test_processes.rb +128 -0
  153. data/tests/test_proxy_connection.rb +180 -0
  154. data/tests/test_pure.rb +156 -0
  155. data/tests/test_queue.rb +64 -0
  156. data/tests/test_resolver.rb +129 -0
  157. data/tests/test_running.rb +14 -0
  158. data/tests/test_sasl.rb +46 -0
  159. data/tests/test_send_file.rb +217 -0
  160. data/tests/test_servers.rb +32 -0
  161. data/tests/test_shutdown_hooks.rb +23 -0
  162. data/tests/test_smtpclient.rb +75 -0
  163. data/tests/test_smtpserver.rb +90 -0
  164. data/tests/test_sock_opt.rb +53 -0
  165. data/tests/test_spawn.rb +290 -0
  166. data/tests/test_ssl_args.rb +41 -0
  167. data/tests/test_ssl_dhparam.rb +57 -0
  168. data/tests/test_ssl_ecdh_curve.rb +57 -0
  169. data/tests/test_ssl_extensions.rb +24 -0
  170. data/tests/test_ssl_methods.rb +31 -0
  171. data/tests/test_ssl_protocols.rb +190 -0
  172. data/tests/test_ssl_verify.rb +52 -0
  173. data/tests/test_stomp.rb +38 -0
  174. data/tests/test_system.rb +46 -0
  175. data/tests/test_threaded_resource.rb +68 -0
  176. data/tests/test_tick_loop.rb +58 -0
  177. data/tests/test_timers.rb +150 -0
  178. data/tests/test_ud.rb +8 -0
  179. data/tests/test_unbind_reason.rb +40 -0
  180. metadata +384 -0
@@ -0,0 +1,38 @@
1
+ if !(RUBY_PLATFORM =~ /java/)
2
+ puts "Ignoring tests in #{__FILE__}. They must be run in JRuby "
3
+ else
4
+ require 'test/unit'
5
+ require 'jeventmachine'
6
+
7
+ class TestJEventmachine < Test::Unit::TestCase
8
+
9
+ def setup
10
+ EventMachine.instance_variable_set("@em", nil)
11
+ load 'jeventmachine.rb'
12
+ end
13
+
14
+ def test_can_make_calls_without_errors_before_initialization
15
+ assert_equal nil, EventMachine.signal_loopbreak
16
+ assert_equal nil, EventMachine.stop_tcp_server(123)
17
+ assert_equal nil, EventMachine.send_data(123, "rewr", 4)
18
+ assert_equal nil, EventMachine.close_connection(332, nil)
19
+ end
20
+
21
+ def test_create
22
+ EventMachine::initialize_event_machine
23
+ em = EventMachine::instance_variable_get("@em")
24
+ assert_equal true, em.is_a?(Java::com.rubyeventmachine.EmReactor)
25
+ end
26
+
27
+ def test_can_make_calls_without_errors_after_release
28
+ EventMachine.initialize_event_machine
29
+ EventMachine.release_machine
30
+
31
+ assert_equal nil, EventMachine.signal_loopbreak
32
+ assert_equal nil, EventMachine.stop_tcp_server(123)
33
+ assert_equal nil, EventMachine.send_data(123, "rewr", 4)
34
+ assert_equal nil, EventMachine.close_connection(332, nil)
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,199 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ class TestAttach < Test::Unit::TestCase
4
+ class EchoServer < EM::Connection
5
+ def receive_data data
6
+ $received_data << data
7
+ send_data data
8
+ end
9
+ end
10
+
11
+ class EchoClient < EM::Connection
12
+ def initialize socket
13
+ self.notify_readable = true
14
+ @socket = socket
15
+ @socket.write("abc\n")
16
+ end
17
+
18
+ def notify_readable
19
+ $read = @socket.readline
20
+ $fd = detach
21
+ end
22
+
23
+ def unbind
24
+ EM.next_tick do
25
+ @socket.write("def\n")
26
+ EM.add_timer(0.1) { EM.stop }
27
+ end
28
+ end
29
+ end
30
+
31
+ def setup
32
+ @port = next_port
33
+ $read, $r, $w, $fd = nil
34
+ $received_data = ""
35
+ end
36
+
37
+ def teardown
38
+ [$r, $w].each do |io|
39
+ io.close rescue nil
40
+ end
41
+ $received_data = nil
42
+ end
43
+
44
+ def test_attach
45
+ socket = nil
46
+
47
+ EM.run {
48
+ EM.start_server "127.0.0.1", @port, EchoServer
49
+ socket = TCPSocket.new "127.0.0.1", @port
50
+ EM.watch socket, EchoClient, socket
51
+ }
52
+
53
+ assert_equal $read, "abc\n"
54
+ unless jruby? # jruby filenos are not real
55
+ assert_equal $fd, socket.fileno
56
+ end
57
+ assert_equal false, socket.closed?
58
+ assert_equal socket.readline, "def\n"
59
+ end
60
+
61
+ module PipeWatch
62
+ def notify_readable
63
+ $read = $r.readline
64
+ EM.stop
65
+ end
66
+ end
67
+
68
+ def test_attach_server
69
+ omit_if(jruby?)
70
+ $before = TCPServer.new("127.0.0.1", @port)
71
+ sig = nil
72
+ EM.run {
73
+ sig = EM.attach_server $before, EchoServer
74
+
75
+ handler = Class.new(EM::Connection) do
76
+ def initialize
77
+ send_data "hello world"
78
+ close_connection_after_writing
79
+ EM.add_timer(0.1) { EM.stop }
80
+ end
81
+ end
82
+ EM.connect("127.0.0.1", @port, handler)
83
+ }
84
+
85
+ assert_equal false, $before.closed?
86
+ assert_equal "hello world", $received_data
87
+ assert sig.is_a?(Integer)
88
+ end
89
+
90
+ def test_attach_pipe
91
+ EM.run{
92
+ $r, $w = IO.pipe
93
+ EM.watch $r, PipeWatch do |c|
94
+ c.notify_readable = true
95
+ end
96
+ $w.write("ghi\n")
97
+ }
98
+
99
+ assert_equal $read, "ghi\n"
100
+ end
101
+
102
+ def test_set_readable
103
+ before, after = nil
104
+
105
+ EM.run{
106
+ $r, $w = IO.pipe
107
+ c = EM.watch $r, PipeWatch do |con|
108
+ con.notify_readable = false
109
+ end
110
+
111
+ EM.next_tick{
112
+ before = c.notify_readable?
113
+ c.notify_readable = true
114
+ after = c.notify_readable?
115
+ }
116
+
117
+ $w.write("jkl\n")
118
+ }
119
+
120
+ assert !before
121
+ assert after
122
+ assert_equal $read, "jkl\n"
123
+ end
124
+
125
+ def test_read_write_pipe
126
+ result = nil
127
+
128
+ pipe_reader = Module.new do
129
+ define_method :receive_data do |data|
130
+ result = data
131
+ EM.stop
132
+ end
133
+ end
134
+
135
+ r,w = IO.pipe
136
+
137
+ EM.run {
138
+ EM.attach r, pipe_reader
139
+ writer = EM.attach(w)
140
+ writer.send_data 'ghi'
141
+
142
+ # XXX: Process will hang in Windows without this line
143
+ writer.close_connection_after_writing
144
+ }
145
+
146
+ assert_equal "ghi", result
147
+ ensure
148
+ [r,w].each {|io| io.close rescue nil }
149
+ end
150
+
151
+ # This test shows that watch_only? is true for EM.watch
152
+ def test_watch_only
153
+ r, w = IO.pipe
154
+ $watch_only = nil
155
+
156
+ EM.run do
157
+ EM.watch r do |c|
158
+ assert_true(c.watch_only?)
159
+ c.notify_readable = true
160
+ def c.receive_data data
161
+ fail('this method should not be called')
162
+ end
163
+ def c.notify_readable
164
+ $watch_only = watch_only?
165
+ end
166
+ end
167
+ w.write 'hello'
168
+ EM.next_tick { EM.stop }
169
+ end
170
+
171
+ assert_true($watch_only)
172
+ end
173
+
174
+ # This test shows that watch_only? is false for EM.attach
175
+ def test_attach_data
176
+ pend("\nFIXME: Freezes Windows testing as of 2018-07-31") if windows?
177
+ r, w = IO.pipe
178
+ $watch_only = nil
179
+ $read = []
180
+
181
+ EM.run do
182
+ EM.attach r do |c|
183
+ assert_false(c.watch_only?)
184
+ def c.receive_data data
185
+ $watch_only = watch_only?
186
+ $read << data
187
+ end
188
+ def c.notify_readable
189
+ fail('this method should not be called')
190
+ end
191
+ end
192
+ w.write 'world'
193
+ EM.next_tick { EM.stop }
194
+ end
195
+
196
+ assert_false($watch_only)
197
+ assert_equal('world', $read.first)
198
+ end
199
+ end
@@ -0,0 +1,321 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ class TestBasic < Test::Unit::TestCase
4
+ def setup
5
+ @port = next_port
6
+ end
7
+
8
+ INVALID = "(not known|no data of the requested|No such host is known|Temporary failure in name resolution)"
9
+
10
+ def test_connection_class_cache
11
+ mod = Module.new
12
+ a, b = nil, nil
13
+ EM.run {
14
+ EM.start_server '127.0.0.1', @port, mod
15
+ a = EM.connect '127.0.0.1', @port, mod
16
+ b = EM.connect '127.0.0.1', @port, mod
17
+ EM.stop
18
+ }
19
+ assert_equal a.class, b.class
20
+ assert_kind_of EM::Connection, a
21
+ end
22
+
23
+ #-------------------------------------
24
+
25
+ def test_em
26
+ assert_nothing_raised do
27
+ EM.run {
28
+ setup_timeout
29
+ EM.add_timer 0 do
30
+ EM.stop
31
+ end
32
+ }
33
+ end
34
+ end
35
+
36
+ #-------------------------------------
37
+
38
+ def test_timer
39
+ assert_nothing_raised do
40
+ EM.run {
41
+ setup_timeout(darwin? ? 0.6 : 0.4)
42
+ n = 0
43
+ EM.add_periodic_timer(0.1) {
44
+ n += 1
45
+ EM.stop if n == 2
46
+ }
47
+ }
48
+ end
49
+ end
50
+
51
+ #-------------------------------------
52
+
53
+ # This test once threw an already-running exception.
54
+ module Trivial
55
+ def post_init
56
+ EM.stop
57
+ end
58
+ end
59
+
60
+ def test_server
61
+ assert_nothing_raised do
62
+ EM.run {
63
+ setup_timeout
64
+ EM.start_server "127.0.0.1", @port, Trivial
65
+ EM.connect "127.0.0.1", @port
66
+ }
67
+ end
68
+ end
69
+
70
+ #--------------------------------------
71
+
72
+ # EM#run_block starts the reactor loop, runs the supplied block, and then STOPS
73
+ # the loop automatically. Contrast with EM#run, which keeps running the reactor
74
+ # even after the supplied block completes.
75
+ def test_run_block
76
+ assert !EM.reactor_running?
77
+ a = nil
78
+ EM.run_block { a = "Worked" }
79
+ assert a
80
+ assert !EM.reactor_running?
81
+ end
82
+
83
+ class UnbindError < EM::Connection
84
+ ERR = Class.new(StandardError)
85
+ def initialize *args
86
+ super
87
+ end
88
+ def connection_completed
89
+ close_connection_after_writing
90
+ end
91
+ def unbind
92
+ raise ERR
93
+ end
94
+ end
95
+
96
+ def test_unbind_error_during_stop
97
+ assert_raises( UnbindError::ERR ) {
98
+ EM.run {
99
+ EM.start_server "127.0.0.1", @port
100
+ EM.connect "127.0.0.1", @port, UnbindError do
101
+ EM.stop
102
+ end
103
+ }
104
+ }
105
+ end
106
+
107
+ def test_unbind_error
108
+ EM.run {
109
+ EM.error_handler do |e|
110
+ assert(e.is_a?(UnbindError::ERR))
111
+ EM.stop
112
+ end
113
+ EM.start_server "127.0.0.1", @port
114
+ EM.connect "127.0.0.1", @port, UnbindError
115
+ }
116
+
117
+ # Remove the error handler before the next test
118
+ EM.error_handler(nil)
119
+ end
120
+
121
+ module BrsTestSrv
122
+ def receive_data data
123
+ $received << data
124
+ end
125
+ def unbind
126
+ EM.stop
127
+ end
128
+ end
129
+ module BrsTestCli
130
+ def post_init
131
+ send_data $sent
132
+ close_connection_after_writing
133
+ end
134
+ end
135
+
136
+ # From ticket #50
137
+ def test_byte_range_send
138
+ $received = ''
139
+ $sent = (0..255).to_a.pack('C*')
140
+ EM::run {
141
+ EM::start_server "127.0.0.1", @port, BrsTestSrv
142
+ EM::connect "127.0.0.1", @port, BrsTestCli
143
+
144
+ setup_timeout
145
+ }
146
+ assert_equal($sent, $received)
147
+ end
148
+
149
+ def test_bind_connect
150
+ local_ip = UDPSocket.open {|s| s.connect('localhost', 80); s.addr.last }
151
+
152
+ bind_port = next_port
153
+
154
+ port, ip = nil
155
+ bound_server = Module.new do
156
+ define_method :post_init do
157
+ begin
158
+ port, ip = Socket.unpack_sockaddr_in(get_peername)
159
+ ensure
160
+ EM.stop
161
+ end
162
+ end
163
+ end
164
+
165
+ EM.run do
166
+ darwin? ? setup_timeout(0.3) : setup_timeout
167
+ EM.start_server "127.0.0.1", @port, bound_server
168
+ EM.bind_connect local_ip, bind_port, "127.0.0.1", @port
169
+ end
170
+
171
+ assert_equal bind_port, port
172
+ assert_equal local_ip, ip
173
+ end
174
+
175
+ def test_invalid_address_bind_connect_dst
176
+ pend("\nFIXME: Windows as of 2018-06-23 on 32 bit >= 2.4 (#{RUBY_VERSION} #{RUBY_PLATFORM})") if RUBY_PLATFORM[/i386-mingw/] && RUBY_VERSION >= '2.4'
177
+ e = nil
178
+ EM.run do
179
+ begin
180
+ EM.bind_connect('localhost', nil, 'invalid.invalid', 80)
181
+ rescue Exception => e
182
+ # capture the exception
183
+ ensure
184
+ EM.stop
185
+ end
186
+ end
187
+
188
+ assert_kind_of(EventMachine::ConnectionError, e)
189
+ assert_match(/unable to resolve address:.*#{INVALID}/, e.message)
190
+ end
191
+
192
+ def test_invalid_address_bind_connect_src
193
+ pend("\nFIXME: Windows as of 2018-06-23 on 32 bit >= 2.4 (#{RUBY_VERSION} #{RUBY_PLATFORM})") if RUBY_PLATFORM[/i386-mingw/] && RUBY_VERSION >= '2.4'
194
+ e = nil
195
+ EM.run do
196
+ begin
197
+ EM.bind_connect('invalid.invalid', nil, 'localhost', 80)
198
+ rescue Exception => e
199
+ # capture the exception
200
+ ensure
201
+ EM.stop
202
+ end
203
+ end
204
+
205
+ assert_kind_of(EventMachine::ConnectionError, e)
206
+ assert_match(/invalid bind address:.*#{INVALID}/, e.message)
207
+ end
208
+
209
+ def test_reactor_thread?
210
+ assert !EM.reactor_thread?
211
+ EM.run { assert EM.reactor_thread?; EM.stop }
212
+ assert !EM.reactor_thread?
213
+ end
214
+
215
+ def test_schedule_on_reactor_thread
216
+ x = false
217
+ EM.run do
218
+ EM.schedule { x = true }
219
+ EM.stop
220
+ end
221
+ assert x
222
+ end
223
+
224
+ def test_schedule_from_thread
225
+ x = false
226
+ EM.run do
227
+ Thread.new { EM.schedule { x = true; EM.stop } }.join
228
+ end
229
+ assert x
230
+ end
231
+
232
+ def test_set_heartbeat_interval
233
+ omit_if(jruby?)
234
+ interval = 0.5
235
+ EM.run {
236
+ EM.set_heartbeat_interval interval
237
+ $interval = EM.get_heartbeat_interval
238
+ EM.stop
239
+ }
240
+ assert_equal(interval, $interval)
241
+ end
242
+
243
+ module PostInitRaiser
244
+ ERR = Class.new(StandardError)
245
+ def post_init
246
+ raise ERR
247
+ end
248
+ end
249
+
250
+ def test_bubble_errors_from_post_init
251
+ assert_raises(PostInitRaiser::ERR) do
252
+ EM.run do
253
+ EM.start_server "127.0.0.1", @port
254
+ EM.connect "127.0.0.1", @port, PostInitRaiser
255
+ end
256
+ end
257
+ end
258
+
259
+ module InitializeRaiser
260
+ ERR = Class.new(StandardError)
261
+ def initialize
262
+ raise ERR
263
+ end
264
+ end
265
+
266
+ def test_bubble_errors_from_initialize
267
+ assert_raises(InitializeRaiser::ERR) do
268
+ EM.run do
269
+ EM.start_server "127.0.0.1", @port
270
+ EM.connect "127.0.0.1", @port, InitializeRaiser
271
+ end
272
+ end
273
+ end
274
+
275
+ def test_schedule_close
276
+ omit_if(jruby?)
277
+ localhost, port = '127.0.0.1', 9000
278
+ timer_ran = false
279
+ num_close_scheduled = nil
280
+ EM.run do
281
+ assert_equal 0, EM.num_close_scheduled
282
+ EM.add_timer(1) { timer_ran = true; EM.stop }
283
+ EM.start_server localhost, port do |s|
284
+ s.close_connection
285
+ num_close_scheduled = EM.num_close_scheduled
286
+ end
287
+ EM.connect localhost, port do |c|
288
+ def c.unbind
289
+ EM.stop
290
+ end
291
+ end
292
+ end
293
+ assert !timer_ran
294
+ assert_equal 1, num_close_scheduled
295
+ end
296
+
297
+ def test_error_handler_idempotent # issue 185
298
+ errors = []
299
+ ticks = []
300
+ EM.error_handler do |e|
301
+ errors << e
302
+ end
303
+
304
+ EM.run do
305
+ EM.next_tick do
306
+ ticks << :first
307
+ raise
308
+ end
309
+ EM.next_tick do
310
+ ticks << :second
311
+ end
312
+ EM.add_timer(0.001) { EM.stop }
313
+ end
314
+
315
+ # Remove the error handler before the next test
316
+ EM.error_handler(nil)
317
+
318
+ assert_equal 1, errors.size
319
+ assert_equal [:first, :second], ticks
320
+ end
321
+ end