sensu-em 2.0.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.travis.yml +12 -0
  4. data/.yardopts +7 -0
  5. data/CHANGELOG.md +33 -0
  6. data/GNU +281 -0
  7. data/Gemfile +2 -0
  8. data/LICENSE +60 -0
  9. data/README.md +109 -0
  10. data/Rakefile +20 -0
  11. data/docs/DocumentationGuidesIndex.md +27 -0
  12. data/docs/GettingStarted.md +521 -0
  13. data/docs/old/ChangeLog +211 -0
  14. data/docs/old/DEFERRABLES +246 -0
  15. data/docs/old/EPOLL +141 -0
  16. data/docs/old/INSTALL +13 -0
  17. data/docs/old/KEYBOARD +42 -0
  18. data/docs/old/LEGAL +25 -0
  19. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  20. data/docs/old/PURE_RUBY +75 -0
  21. data/docs/old/RELEASE_NOTES +94 -0
  22. data/docs/old/SMTP +4 -0
  23. data/docs/old/SPAWNED_PROCESSES +148 -0
  24. data/docs/old/TODO +8 -0
  25. data/eventmachine.gemspec +38 -0
  26. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  27. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  28. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  29. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  30. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  31. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  32. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  33. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  34. data/examples/old/ex_channel.rb +43 -0
  35. data/examples/old/ex_queue.rb +2 -0
  36. data/examples/old/ex_tick_loop_array.rb +15 -0
  37. data/examples/old/ex_tick_loop_counter.rb +32 -0
  38. data/examples/old/helper.rb +2 -0
  39. data/ext/binder.cpp +124 -0
  40. data/ext/binder.h +46 -0
  41. data/ext/cmain.cpp +887 -0
  42. data/ext/ed.cpp +1988 -0
  43. data/ext/ed.h +422 -0
  44. data/ext/em.cpp +2352 -0
  45. data/ext/em.h +253 -0
  46. data/ext/eventmachine.h +128 -0
  47. data/ext/extconf.rb +179 -0
  48. data/ext/fastfilereader/extconf.rb +103 -0
  49. data/ext/fastfilereader/mapper.cpp +214 -0
  50. data/ext/fastfilereader/mapper.h +59 -0
  51. data/ext/fastfilereader/rubymain.cpp +127 -0
  52. data/ext/kb.cpp +79 -0
  53. data/ext/page.cpp +107 -0
  54. data/ext/page.h +51 -0
  55. data/ext/pipe.cpp +347 -0
  56. data/ext/project.h +161 -0
  57. data/ext/rubymain.cpp +1318 -0
  58. data/ext/ssl.cpp +468 -0
  59. data/ext/ssl.h +94 -0
  60. data/java/.classpath +6 -0
  61. data/java/.gitignore +1 -0
  62. data/java/.project +17 -0
  63. data/java/src/com/rubyeventmachine/DatagramPacket.java +13 -0
  64. data/java/src/com/rubyeventmachine/EmReactor.java +529 -0
  65. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  66. data/java/src/com/rubyeventmachine/EventCallback.java +7 -0
  67. data/java/src/com/rubyeventmachine/EventCode.java +26 -0
  68. data/java/src/com/rubyeventmachine/EventableChannel.java +130 -0
  69. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +180 -0
  70. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +405 -0
  71. data/java/src/com/rubyeventmachine/SslBox.java +310 -0
  72. data/lib/em/buftok.rb +110 -0
  73. data/lib/em/callback.rb +58 -0
  74. data/lib/em/channel.rb +64 -0
  75. data/lib/em/completion.rb +304 -0
  76. data/lib/em/connection.rb +712 -0
  77. data/lib/em/deferrable/pool.rb +2 -0
  78. data/lib/em/deferrable.rb +210 -0
  79. data/lib/em/file_watch.rb +73 -0
  80. data/lib/em/future.rb +61 -0
  81. data/lib/em/iterator.rb +231 -0
  82. data/lib/em/messages.rb +66 -0
  83. data/lib/em/pool.rb +151 -0
  84. data/lib/em/process_watch.rb +45 -0
  85. data/lib/em/processes.rb +123 -0
  86. data/lib/em/protocols/header_and_content.rb +138 -0
  87. data/lib/em/protocols/httpclient.rb +279 -0
  88. data/lib/em/protocols/httpclient2.rb +600 -0
  89. data/lib/em/protocols/line_and_text.rb +125 -0
  90. data/lib/em/protocols/line_protocol.rb +29 -0
  91. data/lib/em/protocols/linetext2.rb +161 -0
  92. data/lib/em/protocols/memcache.rb +331 -0
  93. data/lib/em/protocols/object_protocol.rb +46 -0
  94. data/lib/em/protocols/postgres3.rb +246 -0
  95. data/lib/em/protocols/saslauth.rb +175 -0
  96. data/lib/em/protocols/smtpclient.rb +365 -0
  97. data/lib/em/protocols/smtpserver.rb +643 -0
  98. data/lib/em/protocols/socks4.rb +66 -0
  99. data/lib/em/protocols/stomp.rb +205 -0
  100. data/lib/em/protocols/tcptest.rb +54 -0
  101. data/lib/em/protocols.rb +37 -0
  102. data/lib/em/pure_ruby.rb +1017 -0
  103. data/lib/em/queue.rb +71 -0
  104. data/lib/em/resolver.rb +209 -0
  105. data/lib/em/spawnable.rb +84 -0
  106. data/lib/em/streamer.rb +118 -0
  107. data/lib/em/threaded_resource.rb +90 -0
  108. data/lib/em/tick_loop.rb +85 -0
  109. data/lib/em/timers.rb +61 -0
  110. data/lib/em/version.rb +3 -0
  111. data/lib/eventmachine.rb +1553 -0
  112. data/lib/jeventmachine.rb +321 -0
  113. data/lib/rubyeventmachine.jar +0 -0
  114. data/rakelib/cpp.rake_example +77 -0
  115. data/rakelib/package.rake +98 -0
  116. data/rakelib/test.rake +8 -0
  117. data/tests/client.crt +31 -0
  118. data/tests/client.key +51 -0
  119. data/tests/em_test_helper.rb +64 -0
  120. data/tests/server.crt +36 -0
  121. data/tests/server.key +51 -0
  122. data/tests/test_attach.rb +150 -0
  123. data/tests/test_basic.rb +294 -0
  124. data/tests/test_channel.rb +62 -0
  125. data/tests/test_completion.rb +177 -0
  126. data/tests/test_connection_count.rb +53 -0
  127. data/tests/test_defer.rb +18 -0
  128. data/tests/test_deferrable.rb +35 -0
  129. data/tests/test_epoll.rb +145 -0
  130. data/tests/test_error_handler.rb +38 -0
  131. data/tests/test_exc.rb +28 -0
  132. data/tests/test_file_watch.rb +65 -0
  133. data/tests/test_futures.rb +170 -0
  134. data/tests/test_get_sock_opt.rb +37 -0
  135. data/tests/test_handler_check.rb +35 -0
  136. data/tests/test_hc.rb +155 -0
  137. data/tests/test_httpclient.rb +190 -0
  138. data/tests/test_httpclient2.rb +133 -0
  139. data/tests/test_idle_connection.rb +25 -0
  140. data/tests/test_inactivity_timeout.rb +54 -0
  141. data/tests/test_iterator.rb +97 -0
  142. data/tests/test_kb.rb +34 -0
  143. data/tests/test_line_protocol.rb +33 -0
  144. data/tests/test_ltp.rb +138 -0
  145. data/tests/test_ltp2.rb +288 -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 +102 -0
  149. data/tests/test_pending_connect_timeout.rb +52 -0
  150. data/tests/test_pool.rb +194 -0
  151. data/tests/test_process_watch.rb +48 -0
  152. data/tests/test_processes.rb +128 -0
  153. data/tests/test_proxy_connection.rb +180 -0
  154. data/tests/test_pure.rb +88 -0
  155. data/tests/test_queue.rb +50 -0
  156. data/tests/test_resolver.rb +55 -0
  157. data/tests/test_running.rb +14 -0
  158. data/tests/test_sasl.rb +47 -0
  159. data/tests/test_send_file.rb +217 -0
  160. data/tests/test_servers.rb +33 -0
  161. data/tests/test_set_sock_opt.rb +37 -0
  162. data/tests/test_shutdown_hooks.rb +23 -0
  163. data/tests/test_smtpclient.rb +55 -0
  164. data/tests/test_smtpserver.rb +57 -0
  165. data/tests/test_spawn.rb +293 -0
  166. data/tests/test_ssl_args.rb +78 -0
  167. data/tests/test_ssl_echo_data.rb +60 -0
  168. data/tests/test_ssl_methods.rb +56 -0
  169. data/tests/test_ssl_verify.rb +82 -0
  170. data/tests/test_stomp.rb +37 -0
  171. data/tests/test_system.rb +42 -0
  172. data/tests/test_threaded_resource.rb +53 -0
  173. data/tests/test_tick_loop.rb +59 -0
  174. data/tests/test_timers.rb +123 -0
  175. data/tests/test_ud.rb +8 -0
  176. data/tests/test_unbind_reason.rb +48 -0
  177. metadata +297 -0
data/tests/server.key ADDED
@@ -0,0 +1,51 @@
1
+ -----BEGIN RSA PRIVATE KEY-----
2
+ MIIJKQIBAAKCAgEA2dJZEzr1zn0+OZ5eAa+IXFxZmHpuZH31GSvuodYU7rGxd21d
3
+ GltxsDQAYXWp9VpdKduDsEySqmel6qYCWxgBJSfOfYDO8+nlsVLD0M2HMuItuj+m
4
+ Bv6e/vdzybIYzTxVMz9jlkn/nwUsjNDh++/Of1nHIPSPq0ccnQSyjghdlDETm9Or
5
+ Y3tWjjqi86/xaYe2b+K3KW1W1cMJqwXPOveVlyG0Df3mXRPERO9pTKhOXsGHcyH3
6
+ l23XQQrwPEvgHGHA4dZDxCjPdfRrrqLiExG0P/LfPXaIq142mDoJbwfbA63WFOqx
7
+ 0+1Txu7yjLXfcatIKSeSyYOplJADgvTWZ2/AN9s3hD3r2S14GIQORnQ5rTyQlaVl
8
+ I4yM3zVLnrWmdNvcO3TD/RlxTZVfYr6pD0oREQeAW822HiA6njMnX9Ck51/JBjD4
9
+ 3gVYbXoAmPn+971R50Jy8QrDE2YEHsbsezmG/5WbmVfSBXb61p9MrEdDxV+8Z6h+
10
+ kOPJrhmIZFvAC2oYCTma23wah2AMQ+VOj0zc8bcRVwfcC8w+eABcS0lWg0jUErOy
11
+ mWqJNgzqNKbCnf58rviDLfC5K0PJaiDkh2DoNhcq2oY3Z7IH6F+FSaFNnFCYKPjY
12
+ 7N5GfLVabiVmjHV73EVmwqy8xJyf2Jw6g8cE8mZ/XlBZ1DeQUyQrLcdHQVcCAwEA
13
+ AQKCAgEAt1DK/F8rjatj2fQJI5wJw8lI8gVfsJ080AFkJLlaBoRvWZYlu8/nvyYU
14
+ h/ERp1Z0f4ypqI7ZB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nMls8ueWcWB3NzrN0mroSsF7JRAoIBAQDBJ+mvkDqdjkBZs04uPMrQdYfo
46
+ 3WGHWCHqWwoSEXKXR40J1ZEcet2q0hk2g2dAM4aATDRrAiJtAfl+qoAaSRmMmXJN
47
+ l+1kM1VEOq9aD5DskztGpkRGg9mCVJuiENB29/nc9VbteQY/K1eV3Lhpnz6S8g1R
48
+ V75vUVuso+7qorkTp8Ixtmbf3/LlNZqBkJ7wuBWmEk8WFVPueSK0hER0sHE74j1+
49
+ CM2rAbL1YLoz3YVlVfKyxhwXWL1vP/LfQSBPTxrPpktjbSKNJAPh+JPdZvu67o43
50
+ Ot9/TM6o8Y0zS5j7iXblrEef12Fga0fEwgob6xC/Nh+Twd+aSFp5ILUACfow
51
+ -----END RSA PRIVATE KEY-----
@@ -0,0 +1,150 @@
1
+ require 'em_test_helper'
2
+ require 'socket'
3
+
4
+ class TestAttach < Test::Unit::TestCase
5
+ class EchoServer < EM::Connection
6
+ def receive_data data
7
+ $received_data << data
8
+ send_data data
9
+ end
10
+ end
11
+
12
+ class EchoClient < EM::Connection
13
+ def initialize socket
14
+ self.notify_readable = true
15
+ @socket = socket
16
+ @socket.write("abc\n")
17
+ end
18
+
19
+ def notify_readable
20
+ $read = @socket.readline
21
+ $fd = detach
22
+ end
23
+
24
+ def unbind
25
+ EM.next_tick do
26
+ @socket.write("def\n")
27
+ EM.add_timer(0.1) { EM.stop }
28
+ end
29
+ end
30
+ end
31
+
32
+ def setup
33
+ @port = next_port
34
+ $read, $r, $w, $fd = nil
35
+ $received_data = ""
36
+ end
37
+
38
+ def teardown
39
+ [$r, $w].each do |io|
40
+ io.close rescue nil
41
+ end
42
+ $received_data = nil
43
+ end
44
+
45
+ def test_attach
46
+ socket = nil
47
+
48
+ EM.run {
49
+ EM.start_server "127.0.0.1", @port, EchoServer
50
+ socket = TCPSocket.new "127.0.0.1", @port
51
+ EM.watch socket, EchoClient, socket
52
+ }
53
+
54
+ assert_equal $read, "abc\n"
55
+ unless jruby? # jruby filenos are not real
56
+ assert_equal $fd, socket.fileno
57
+ end
58
+ assert_equal false, socket.closed?
59
+ assert_equal socket.readline, "def\n"
60
+ end
61
+
62
+ module PipeWatch
63
+ def notify_readable
64
+ $read = $r.readline
65
+ EM.stop
66
+ end
67
+ end
68
+
69
+ def test_attach_server
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
+ end
@@ -0,0 +1,294 @@
1
+ require 'em_test_helper'
2
+ require 'socket'
3
+
4
+ class TestBasic < Test::Unit::TestCase
5
+ def setup
6
+ @port = next_port
7
+ end
8
+
9
+ def test_connection_class_cache
10
+ mod = Module.new
11
+ a, b = nil, nil
12
+ EM.run {
13
+ EM.start_server '127.0.0.1', @port, mod
14
+ a = EM.connect '127.0.0.1', @port, mod
15
+ b = EM.connect '127.0.0.1', @port, mod
16
+ EM.stop
17
+ }
18
+ assert_equal a.class, b.class
19
+ assert_kind_of EM::Connection, a
20
+ end
21
+
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
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
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
101
+ }
102
+ }
103
+ end
104
+
105
+ module BrsTestSrv
106
+ def receive_data data
107
+ $received << data
108
+ end
109
+ def unbind
110
+ EM.stop
111
+ end
112
+ end
113
+ module BrsTestCli
114
+ def post_init
115
+ send_data $sent
116
+ close_connection_after_writing
117
+ end
118
+ end
119
+
120
+ # From ticket #50
121
+ def test_byte_range_send
122
+ $received = ''
123
+ $sent = (0..255).to_a.pack('C*')
124
+ EM::run {
125
+ EM::start_server "127.0.0.1", @port, BrsTestSrv
126
+ EM::connect "127.0.0.1", @port, BrsTestCli
127
+
128
+ setup_timeout
129
+ }
130
+ assert_equal($sent, $received)
131
+ end
132
+
133
+ def test_bind_connect
134
+ local_ip = UDPSocket.open {|s| s.connect('google.com', 80); s.addr.last }
135
+
136
+ bind_port = next_port
137
+
138
+ port, ip = nil
139
+ bound_server = Module.new do
140
+ define_method :post_init do
141
+ begin
142
+ port, ip = Socket.unpack_sockaddr_in(get_peername)
143
+ ensure
144
+ EM.stop
145
+ end
146
+ end
147
+ end
148
+
149
+ EM.run do
150
+ setup_timeout
151
+ EM.start_server "127.0.0.1", @port, bound_server
152
+ EM.bind_connect local_ip, bind_port, "127.0.0.1", @port
153
+ end
154
+
155
+ assert_equal bind_port, port
156
+ assert_equal local_ip, ip
157
+ end
158
+
159
+ def test_reactor_thread?
160
+ assert !EM.reactor_thread?
161
+ EM.run { assert EM.reactor_thread?; EM.stop }
162
+ assert !EM.reactor_thread?
163
+ end
164
+
165
+ def test_schedule_on_reactor_thread
166
+ x = false
167
+ EM.run do
168
+ EM.schedule { x = true }
169
+ EM.stop
170
+ end
171
+ assert x
172
+ end
173
+
174
+ def test_schedule_from_thread
175
+ x = false
176
+ EM.run do
177
+ Thread.new { EM.schedule { x = true; EM.stop } }.join
178
+ end
179
+ assert x
180
+ end
181
+
182
+ if EM.respond_to? :set_heartbeat_interval
183
+ def test_set_heartbeat_interval
184
+ interval = 0.5
185
+ EM.run {
186
+ EM.set_heartbeat_interval interval
187
+ $interval = EM.get_heartbeat_interval
188
+ EM.stop
189
+ }
190
+ assert_equal(interval, $interval)
191
+ end
192
+ else
193
+ warn "EM.set_heartbeat_interval not implemented, skipping a test in #{__FILE__}"
194
+ end
195
+
196
+ module PostInitRaiser
197
+ ERR = Class.new(StandardError)
198
+ def post_init
199
+ raise ERR
200
+ end
201
+ end
202
+
203
+ def test_bubble_errors_from_post_init
204
+ assert_raises(PostInitRaiser::ERR) do
205
+ EM.run do
206
+ EM.start_server "127.0.0.1", @port
207
+ EM.connect "127.0.0.1", @port, PostInitRaiser
208
+ end
209
+ end
210
+ end
211
+
212
+ module InitializeRaiser
213
+ ERR = Class.new(StandardError)
214
+ def initialize
215
+ raise ERR
216
+ end
217
+ end
218
+
219
+ def test_bubble_errors_from_initialize
220
+ assert_raises(InitializeRaiser::ERR) do
221
+ EM.run do
222
+ EM.start_server "127.0.0.1", @port
223
+ EM.connect "127.0.0.1", @port, InitializeRaiser
224
+ end
225
+ end
226
+ end
227
+
228
+ def test_schedule_close
229
+ localhost, port = '127.0.0.1', 9000
230
+ timer_ran = false
231
+ num_close_scheduled = nil
232
+ EM.run do
233
+ assert_equal 0, EM.num_close_scheduled
234
+ EM.add_timer(1) { timer_ran = true; EM.stop }
235
+ EM.start_server localhost, port do |s|
236
+ s.close_connection
237
+ num_close_scheduled = EM.num_close_scheduled
238
+ end
239
+ EM.connect localhost, port do |c|
240
+ def c.unbind
241
+ EM.stop
242
+ end
243
+ end
244
+ end
245
+ assert !timer_ran
246
+ assert_equal 1, num_close_scheduled
247
+ end
248
+
249
+ def test_fork_safe
250
+ return unless cpid = fork { exit! } rescue false
251
+
252
+ read, write = IO.pipe
253
+ EM.run do
254
+ cpid = fork do
255
+ write.puts "forked"
256
+ EM.run do
257
+ EM.next_tick do
258
+ write.puts "EM ran"
259
+ exit!
260
+ end
261
+ end
262
+ end
263
+ EM.stop
264
+ end
265
+ Process.waitall
266
+ assert_equal "forked\n", read.readline
267
+ assert_equal "EM ran\n", read.readline
268
+ ensure
269
+ read.close rescue nil
270
+ write.close rescue nil
271
+ end
272
+
273
+ def test_error_handler_idempotent # issue 185
274
+ errors = []
275
+ ticks = []
276
+ EM.error_handler do |e|
277
+ errors << e
278
+ end
279
+
280
+ EM.run do
281
+ EM.next_tick do
282
+ ticks << :first
283
+ raise
284
+ end
285
+ EM.next_tick do
286
+ ticks << :second
287
+ end
288
+ EM.add_timer(0.001) { EM.stop }
289
+ end
290
+
291
+ assert_equal 1, errors.size
292
+ assert_equal [:first, :second], ticks
293
+ end
294
+ end
@@ -0,0 +1,62 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestEMChannel < Test::Unit::TestCase
4
+ def test_channel_subscribe
5
+ s = 0
6
+ EM.run do
7
+ c = EM::Channel.new
8
+ c.subscribe { |v| s = v; EM.stop }
9
+ c << 1
10
+ end
11
+ assert_equal 1, s
12
+ end
13
+
14
+ def test_channel_unsubscribe
15
+ s = 0
16
+ EM.run do
17
+ c = EM::Channel.new
18
+ subscription = c.subscribe { |v| s = v }
19
+ c.unsubscribe(subscription)
20
+ c << 1
21
+ EM.next_tick { EM.stop }
22
+ end
23
+ assert_not_equal 1, s
24
+ end
25
+
26
+ def test_channel_pop
27
+ s = 0
28
+ EM.run do
29
+ c = EM::Channel.new
30
+ c.pop{ |v| s = v }
31
+ c.push(1,2,3)
32
+ c << 4
33
+ c << 5
34
+ EM.next_tick { EM.stop }
35
+ end
36
+ assert_equal 1, s
37
+ end
38
+
39
+ def test_channel_reactor_thread_push
40
+ out = []
41
+ c = EM::Channel.new
42
+ c.subscribe { |v| out << v }
43
+ Thread.new { c.push(1,2,3) }.join
44
+ assert out.empty?
45
+
46
+ EM.run { EM.next_tick { EM.stop } }
47
+
48
+ assert_equal [1,2,3], out
49
+ end
50
+
51
+ def test_channel_reactor_thread_callback
52
+ out = []
53
+ c = EM::Channel.new
54
+ Thread.new { c.subscribe { |v| out << v } }.join
55
+ c.push(1,2,3)
56
+ assert out.empty?
57
+
58
+ EM.run { EM.next_tick { EM.stop } }
59
+
60
+ assert_equal [1,2,3], out
61
+ end
62
+ end