eventmachine-le 1.1.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. data/.gitignore +21 -0
  2. data/.yardopts +7 -0
  3. data/GNU +281 -0
  4. data/LICENSE +60 -0
  5. data/README.md +80 -0
  6. data/Rakefile +19 -0
  7. data/eventmachine-le.gemspec +42 -0
  8. data/ext/binder.cpp +124 -0
  9. data/ext/binder.h +46 -0
  10. data/ext/cmain.cpp +841 -0
  11. data/ext/ed.cpp +1995 -0
  12. data/ext/ed.h +424 -0
  13. data/ext/em.cpp +2377 -0
  14. data/ext/em.h +243 -0
  15. data/ext/eventmachine.h +126 -0
  16. data/ext/extconf.rb +166 -0
  17. data/ext/fastfilereader/extconf.rb +94 -0
  18. data/ext/fastfilereader/mapper.cpp +214 -0
  19. data/ext/fastfilereader/mapper.h +59 -0
  20. data/ext/fastfilereader/rubymain.cpp +127 -0
  21. data/ext/kb.cpp +79 -0
  22. data/ext/page.cpp +107 -0
  23. data/ext/page.h +51 -0
  24. data/ext/pipe.cpp +347 -0
  25. data/ext/project.h +155 -0
  26. data/ext/rubymain.cpp +1269 -0
  27. data/ext/ssl.cpp +468 -0
  28. data/ext/ssl.h +94 -0
  29. data/lib/em/buftok.rb +110 -0
  30. data/lib/em/callback.rb +58 -0
  31. data/lib/em/channel.rb +64 -0
  32. data/lib/em/completion.rb +304 -0
  33. data/lib/em/connection.rb +728 -0
  34. data/lib/em/deferrable.rb +210 -0
  35. data/lib/em/deferrable/pool.rb +2 -0
  36. data/lib/em/file_watch.rb +73 -0
  37. data/lib/em/future.rb +61 -0
  38. data/lib/em/iterator.rb +313 -0
  39. data/lib/em/messages.rb +66 -0
  40. data/lib/em/pool.rb +151 -0
  41. data/lib/em/process_watch.rb +45 -0
  42. data/lib/em/processes.rb +123 -0
  43. data/lib/em/protocols.rb +37 -0
  44. data/lib/em/protocols/header_and_content.rb +138 -0
  45. data/lib/em/protocols/httpclient.rb +279 -0
  46. data/lib/em/protocols/httpclient2.rb +600 -0
  47. data/lib/em/protocols/line_and_text.rb +125 -0
  48. data/lib/em/protocols/line_protocol.rb +29 -0
  49. data/lib/em/protocols/linetext2.rb +161 -0
  50. data/lib/em/protocols/memcache.rb +331 -0
  51. data/lib/em/protocols/object_protocol.rb +46 -0
  52. data/lib/em/protocols/postgres3.rb +246 -0
  53. data/lib/em/protocols/saslauth.rb +175 -0
  54. data/lib/em/protocols/smtpclient.rb +365 -0
  55. data/lib/em/protocols/smtpserver.rb +663 -0
  56. data/lib/em/protocols/socks4.rb +66 -0
  57. data/lib/em/protocols/stomp.rb +202 -0
  58. data/lib/em/protocols/tcptest.rb +54 -0
  59. data/lib/em/queue.rb +71 -0
  60. data/lib/em/resolver.rb +195 -0
  61. data/lib/em/spawnable.rb +84 -0
  62. data/lib/em/streamer.rb +118 -0
  63. data/lib/em/threaded_resource.rb +90 -0
  64. data/lib/em/tick_loop.rb +85 -0
  65. data/lib/em/timers.rb +106 -0
  66. data/lib/em/version.rb +3 -0
  67. data/lib/eventmachine-le.rb +10 -0
  68. data/lib/eventmachine.rb +1548 -0
  69. data/rakelib/cpp.rake_example +77 -0
  70. data/rakelib/package.rake +98 -0
  71. data/rakelib/test.rake +8 -0
  72. data/tests/client.crt +31 -0
  73. data/tests/client.key +51 -0
  74. data/tests/em_test_helper.rb +143 -0
  75. data/tests/test_attach.rb +148 -0
  76. data/tests/test_basic.rb +294 -0
  77. data/tests/test_channel.rb +62 -0
  78. data/tests/test_completion.rb +177 -0
  79. data/tests/test_connection_count.rb +33 -0
  80. data/tests/test_defer.rb +18 -0
  81. data/tests/test_deferrable.rb +35 -0
  82. data/tests/test_epoll.rb +134 -0
  83. data/tests/test_error_handler.rb +38 -0
  84. data/tests/test_exc.rb +28 -0
  85. data/tests/test_file_watch.rb +65 -0
  86. data/tests/test_futures.rb +170 -0
  87. data/tests/test_get_sock_opt.rb +37 -0
  88. data/tests/test_handler_check.rb +35 -0
  89. data/tests/test_hc.rb +155 -0
  90. data/tests/test_httpclient.rb +190 -0
  91. data/tests/test_httpclient2.rb +128 -0
  92. data/tests/test_inactivity_timeout.rb +54 -0
  93. data/tests/test_ipv4.rb +125 -0
  94. data/tests/test_ipv6.rb +131 -0
  95. data/tests/test_iterator.rb +110 -0
  96. data/tests/test_kb.rb +34 -0
  97. data/tests/test_line_protocol.rb +33 -0
  98. data/tests/test_ltp.rb +138 -0
  99. data/tests/test_ltp2.rb +288 -0
  100. data/tests/test_next_tick.rb +104 -0
  101. data/tests/test_object_protocol.rb +36 -0
  102. data/tests/test_pause.rb +78 -0
  103. data/tests/test_pending_connect_timeout.rb +52 -0
  104. data/tests/test_pool.rb +196 -0
  105. data/tests/test_process_watch.rb +48 -0
  106. data/tests/test_processes.rb +133 -0
  107. data/tests/test_proxy_connection.rb +168 -0
  108. data/tests/test_pure.rb +88 -0
  109. data/tests/test_queue.rb +50 -0
  110. data/tests/test_resolver.rb +55 -0
  111. data/tests/test_running.rb +14 -0
  112. data/tests/test_sasl.rb +47 -0
  113. data/tests/test_send_file.rb +217 -0
  114. data/tests/test_servers.rb +33 -0
  115. data/tests/test_set_sock_opt.rb +41 -0
  116. data/tests/test_shutdown_hooks.rb +23 -0
  117. data/tests/test_smtpclient.rb +55 -0
  118. data/tests/test_smtpserver.rb +120 -0
  119. data/tests/test_spawn.rb +293 -0
  120. data/tests/test_ssl_args.rb +78 -0
  121. data/tests/test_ssl_methods.rb +48 -0
  122. data/tests/test_ssl_verify.rb +82 -0
  123. data/tests/test_threaded_resource.rb +55 -0
  124. data/tests/test_tick_loop.rb +59 -0
  125. data/tests/test_timers.rb +180 -0
  126. data/tests/test_ud.rb +8 -0
  127. data/tests/test_udp46.rb +53 -0
  128. data/tests/test_unbind_reason.rb +48 -0
  129. metadata +390 -0
@@ -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
@@ -0,0 +1,177 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestCompletion < Test::Unit::TestCase
4
+ def completion
5
+ @completion ||= EM::Completion.new
6
+ end
7
+
8
+ def crank
9
+ # This is a slow solution, but this just executes the next tick queue
10
+ # once. It's the easiest way for now.
11
+ EM.run { EM.stop }
12
+ end
13
+
14
+ def results
15
+ @results ||= []
16
+ end
17
+
18
+ def test_state
19
+ assert_equal :unknown, completion.state
20
+ end
21
+
22
+ def test_succeed
23
+ completion.callback { |val| results << val }
24
+ completion.succeed :object
25
+ crank
26
+ assert_equal :succeeded, completion.state
27
+ assert_equal [:object], results
28
+ end
29
+
30
+ def test_fail
31
+ completion.errback { |val| results << val }
32
+ completion.fail :object
33
+ crank
34
+ assert_equal :failed, completion.state
35
+ assert_equal [:object], results
36
+ end
37
+
38
+ def test_callback
39
+ completion.callback { results << :callback }
40
+ completion.errback { results << :errback }
41
+ completion.succeed
42
+ crank
43
+ assert_equal [:callback], results
44
+ end
45
+
46
+ def test_errback
47
+ completion.callback { results << :callback }
48
+ completion.errback { results << :errback }
49
+ completion.fail
50
+ crank
51
+ assert_equal [:errback], results
52
+ end
53
+
54
+ def test_stateback
55
+ completion.stateback(:magic) { results << :stateback }
56
+ completion.change_state(:magic)
57
+ crank
58
+ assert_equal [:stateback], results
59
+ end
60
+
61
+ def test_does_not_enqueue_when_completed
62
+ completion.callback { results << :callback }
63
+ completion.succeed
64
+ completion.errback { results << :errback }
65
+ completion.fail
66
+ crank
67
+ assert_equal [:callback], results
68
+ end
69
+
70
+ def test_completed
71
+ assert_equal false, completion.completed?
72
+ completion.succeed
73
+ assert_equal true, completion.completed?
74
+ completion.fail
75
+ assert_equal true, completion.completed?
76
+ completion.change_state :magic
77
+ assert_equal false, completion.completed?
78
+ end
79
+
80
+ def test_recursive_callbacks
81
+ completion.callback do |val|
82
+ results << val
83
+ completion.succeed :two
84
+ end
85
+ completion.callback do |val|
86
+ results << val
87
+ completion.succeed :three
88
+ end
89
+ completion.callback do |val|
90
+ results << val
91
+ end
92
+ completion.succeed :one
93
+ crank
94
+ assert_equal [:one, :two, :three], results
95
+ end
96
+
97
+ def test_late_defined_callbacks
98
+ completion.callback { results << :one }
99
+ completion.succeed
100
+ crank
101
+ assert_equal [:one], results
102
+ completion.callback { results << :two }
103
+ crank
104
+ assert_equal [:one, :two], results
105
+ end
106
+
107
+ def test_cleared_completions
108
+ completion.callback { results << :callback }
109
+ completion.errback { results << :errback }
110
+
111
+ completion.succeed
112
+ crank
113
+ completion.fail
114
+ crank
115
+ completion.succeed
116
+ crank
117
+
118
+ assert_equal [:callback], results
119
+ end
120
+
121
+ def test_skip_completed_callbacks
122
+ completion.callback { results << :callback }
123
+ completion.succeed
124
+ crank
125
+
126
+ completion.errback { results << :errback }
127
+ completion.fail
128
+ crank
129
+
130
+ assert_equal [:callback], results
131
+ end
132
+
133
+ def test_completions
134
+ completion.completion { results << :completion }
135
+ completion.succeed
136
+ crank
137
+ assert_equal [:completion], results
138
+
139
+ completion.change_state(:unknown)
140
+ results.clear
141
+
142
+ completion.completion { results << :completion }
143
+ completion.fail
144
+ crank
145
+ assert_equal [:completion], results
146
+ end
147
+
148
+ def test_latent_completion
149
+ completion.completion { results << :completion }
150
+ completion.succeed
151
+ crank
152
+ completion.completion { results << :completion }
153
+ crank
154
+ assert_equal [:completion, :completion], results
155
+ end
156
+
157
+ def test_timeout
158
+ args = [1, 2, 3]
159
+ EM.run do
160
+ completion.timeout(0.0001, *args)
161
+ completion.errback { |*errargs| results << errargs }
162
+ completion.completion { EM.stop }
163
+ EM.add_timer(0.1) { flunk 'test timed out' }
164
+ end
165
+ assert_equal [[1,2,3]], results
166
+ end
167
+
168
+ def test_timeout_gets_cancelled
169
+ EM.run do
170
+ completion.timeout(0.0001, :timeout)
171
+ completion.errback { results << :errback }
172
+ completion.succeed
173
+ EM.add_timer(0.0002) { EM.stop }
174
+ end
175
+ assert_equal [], results
176
+ end
177
+ end