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,75 @@
1
+ require_relative '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
+
63
+ def test_channel_num_subscribers
64
+ subs = 0
65
+ EM.run do
66
+ c = EM::Channel.new
67
+ c.subscribe { |v| }
68
+ c.subscribe { |v| }
69
+ EM.next_tick { EM.stop }
70
+ subs = c.num_subscribers
71
+ end
72
+
73
+ assert_equal subs, 2
74
+ end
75
+ end
@@ -0,0 +1,178 @@
1
+ require_relative 'em_test_helper'
2
+ require 'em/completion'
3
+
4
+ class TestCompletion < Test::Unit::TestCase
5
+ def completion
6
+ @completion ||= EM::Completion.new
7
+ end
8
+
9
+ def crank
10
+ # This is a slow solution, but this just executes the next tick queue
11
+ # once. It's the easiest way for now.
12
+ EM.run { EM.stop }
13
+ end
14
+
15
+ def results
16
+ @results ||= []
17
+ end
18
+
19
+ def test_state
20
+ assert_equal :unknown, completion.state
21
+ end
22
+
23
+ def test_succeed
24
+ completion.callback { |val| results << val }
25
+ completion.succeed :object
26
+ crank
27
+ assert_equal :succeeded, completion.state
28
+ assert_equal [:object], results
29
+ end
30
+
31
+ def test_fail
32
+ completion.errback { |val| results << val }
33
+ completion.fail :object
34
+ crank
35
+ assert_equal :failed, completion.state
36
+ assert_equal [:object], results
37
+ end
38
+
39
+ def test_callback
40
+ completion.callback { results << :callback }
41
+ completion.errback { results << :errback }
42
+ completion.succeed
43
+ crank
44
+ assert_equal [:callback], results
45
+ end
46
+
47
+ def test_errback
48
+ completion.callback { results << :callback }
49
+ completion.errback { results << :errback }
50
+ completion.fail
51
+ crank
52
+ assert_equal [:errback], results
53
+ end
54
+
55
+ def test_stateback
56
+ completion.stateback(:magic) { results << :stateback }
57
+ completion.change_state(:magic)
58
+ crank
59
+ assert_equal [:stateback], results
60
+ end
61
+
62
+ def test_does_not_enqueue_when_completed
63
+ completion.callback { results << :callback }
64
+ completion.succeed
65
+ completion.errback { results << :errback }
66
+ completion.fail
67
+ crank
68
+ assert_equal [:callback], results
69
+ end
70
+
71
+ def test_completed
72
+ assert_equal false, completion.completed?
73
+ completion.succeed
74
+ assert_equal true, completion.completed?
75
+ completion.fail
76
+ assert_equal true, completion.completed?
77
+ completion.change_state :magic
78
+ assert_equal false, completion.completed?
79
+ end
80
+
81
+ def test_recursive_callbacks
82
+ completion.callback do |val|
83
+ results << val
84
+ completion.succeed :two
85
+ end
86
+ completion.callback do |val|
87
+ results << val
88
+ completion.succeed :three
89
+ end
90
+ completion.callback do |val|
91
+ results << val
92
+ end
93
+ completion.succeed :one
94
+ crank
95
+ assert_equal [:one, :two, :three], results
96
+ end
97
+
98
+ def test_late_defined_callbacks
99
+ completion.callback { results << :one }
100
+ completion.succeed
101
+ crank
102
+ assert_equal [:one], results
103
+ completion.callback { results << :two }
104
+ crank
105
+ assert_equal [:one, :two], results
106
+ end
107
+
108
+ def test_cleared_completions
109
+ completion.callback { results << :callback }
110
+ completion.errback { results << :errback }
111
+
112
+ completion.succeed
113
+ crank
114
+ completion.fail
115
+ crank
116
+ completion.succeed
117
+ crank
118
+
119
+ assert_equal [:callback], results
120
+ end
121
+
122
+ def test_skip_completed_callbacks
123
+ completion.callback { results << :callback }
124
+ completion.succeed
125
+ crank
126
+
127
+ completion.errback { results << :errback }
128
+ completion.fail
129
+ crank
130
+
131
+ assert_equal [:callback], results
132
+ end
133
+
134
+ def test_completions
135
+ completion.completion { results << :completion }
136
+ completion.succeed
137
+ crank
138
+ assert_equal [:completion], results
139
+
140
+ completion.change_state(:unknown)
141
+ results.clear
142
+
143
+ completion.completion { results << :completion }
144
+ completion.fail
145
+ crank
146
+ assert_equal [:completion], results
147
+ end
148
+
149
+ def test_latent_completion
150
+ completion.completion { results << :completion }
151
+ completion.succeed
152
+ crank
153
+ completion.completion { results << :completion }
154
+ crank
155
+ assert_equal [:completion, :completion], results
156
+ end
157
+
158
+ def test_timeout
159
+ args = [1, 2, 3]
160
+ EM.run do
161
+ completion.timeout(0.0001, *args)
162
+ completion.errback { |*errargs| results << errargs }
163
+ completion.completion { EM.stop }
164
+ EM.add_timer(0.1) { flunk 'test timed out' }
165
+ end
166
+ assert_equal [[1,2,3]], results
167
+ end
168
+
169
+ def test_timeout_gets_cancelled
170
+ EM.run do
171
+ completion.timeout(0.0001, :timeout)
172
+ completion.errback { results << :errback }
173
+ completion.succeed
174
+ EM.add_timer(0.0002) { EM.stop }
175
+ end
176
+ assert_equal [], results
177
+ end
178
+ end
@@ -0,0 +1,83 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ class TestConnectionCount < Test::Unit::TestCase
4
+ def teardown
5
+ EM.epoll = false
6
+ EM.kqueue = false
7
+ end
8
+
9
+ def test_idle_connection_count
10
+ count = nil
11
+ EM.run {
12
+ count = EM.connection_count
13
+ EM.stop_event_loop
14
+ }
15
+ assert_equal(0, count)
16
+ end
17
+
18
+ # Run this again with epoll enabled (if available)
19
+ def test_idle_connection_count_epoll
20
+ EM.epoll if EM.epoll?
21
+
22
+ count = nil
23
+ EM.run {
24
+ count = EM.connection_count
25
+ EM.stop_event_loop
26
+ }
27
+ assert_equal(0, count)
28
+ end
29
+
30
+ # Run this again with kqueue enabled (if available)
31
+ def test_idle_connection_count_kqueue
32
+ EM.kqueue if EM.kqueue?
33
+
34
+ count = nil
35
+ EM.run {
36
+ count = EM.connection_count
37
+ EM.stop_event_loop
38
+ }
39
+ assert_equal(0, count)
40
+ end
41
+
42
+ module Client
43
+ def connection_completed
44
+ $client_conns += 1
45
+ EM.stop if $client_conns == 3
46
+ end
47
+ end
48
+
49
+ def test_with_some_connections
50
+ EM.run {
51
+ $client_conns = 0
52
+ $initial_conns = EM.connection_count
53
+ EM.start_server("127.0.0.1", 9999)
54
+ $server_conns = EM.connection_count
55
+ 3.times { EM.connect("127.0.0.1", 9999, Client) }
56
+ }
57
+
58
+ assert_equal(0, $initial_conns)
59
+ assert_equal(1, $server_conns)
60
+ assert_equal(4, $client_conns + $server_conns)
61
+ end
62
+
63
+ module DoubleCloseClient
64
+ def unbind
65
+ close_connection
66
+ $num_close_scheduled_1 = EM.num_close_scheduled
67
+ EM.next_tick do
68
+ $num_close_scheduled_2 = EM.num_close_scheduled
69
+ EM.stop
70
+ end
71
+ end
72
+ end
73
+
74
+ def test_num_close_scheduled
75
+ omit_if(jruby?)
76
+ EM.run {
77
+ assert_equal(0, EM.num_close_scheduled)
78
+ EM.connect("127.0.0.1", 9999, DoubleCloseClient) # nothing listening on 9999
79
+ }
80
+ assert_equal(1, $num_close_scheduled_1)
81
+ assert_equal(0, $num_close_scheduled_2)
82
+ end
83
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ class TestConnectionWrite < Test::Unit::TestCase
4
+
5
+ # This test takes advantage of the fact that EM::_RunSelectOnce iterates over the connections twice:
6
+ # - once to determine which ones to call Write() on
7
+ # - and once to call Write() on each of them.
8
+ #
9
+ # But state may change in the meantime before Write() is finally called.
10
+ # And that is what we try to exploit to get Write() to be called when bWatchOnly is true, and bNotifyWritable is false,
11
+ # to cause an assertion failure.
12
+
13
+ module SimpleClient
14
+ def notify_writable
15
+ $conn2.notify_writable = false # Being naughty in callback
16
+ # If this doesn't crash anything, the test passed!
17
+ end
18
+ end
19
+
20
+ def test_with_naughty_callback
21
+ EM.run do
22
+ r1, _ = IO.pipe
23
+ r2, _ = IO.pipe
24
+
25
+ # Adding EM.watches
26
+ $conn1 = EM.watch(r1, SimpleClient)
27
+ $conn2 = EM.watch(r2, SimpleClient)
28
+
29
+ $conn1.notify_writable = true
30
+ $conn2.notify_writable = true
31
+
32
+ EM.stop
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ class TestDefer < Test::Unit::TestCase
4
+
5
+ def test_defers
6
+ n = 0
7
+ n_times = 20
8
+ EM.run {
9
+ n_times.times {
10
+ work_proc = proc { n += 1 }
11
+ callback = proc { EM.stop if n == n_times }
12
+ EM.defer work_proc, callback
13
+ }
14
+ }
15
+ assert_equal( n, n_times )
16
+ end
17
+
18
+ def test_errbacks
19
+ iterations = 20
20
+ callback_parameter = rand(100)
21
+ callback_parameters = []
22
+ callback_op = proc { callback_parameter }
23
+ callback = proc { |result| callback_parameters << result }
24
+ errback_parameter = Exception.new
25
+ errback_parameters = []
26
+ errback_op = proc { raise errback_parameter }
27
+ errback = proc { |error| errback_parameters << error }
28
+ EventMachine.run do
29
+ (1..iterations).each { |index| EventMachine.defer(index.even? ? callback_op : errback_op, callback, errback) }
30
+ EventMachine.add_periodic_timer(0.1) { EventMachine.stop if EventMachine.defers_finished? }
31
+ end
32
+ assert_equal(callback_parameters.select { |parameter| parameter == callback_parameter }.length, iterations * 0.5)
33
+ assert_equal(errback_parameters.select{ |parameter| parameter == errback_parameter }.length, iterations * 0.5)
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ class TestDeferrable < Test::Unit::TestCase
4
+ class Later
5
+ include EM::Deferrable
6
+ end
7
+
8
+ def test_timeout_without_args
9
+ assert_nothing_raised do
10
+ EM.run {
11
+ df = Later.new
12
+ df.timeout(0)
13
+ df.errback { EM.stop }
14
+ EM.add_timer(0.01) { flunk "Deferrable was not timed out." }
15
+ }
16
+ end
17
+ end
18
+
19
+ def test_timeout_with_args
20
+ args = nil
21
+
22
+ EM.run {
23
+ df = Later.new
24
+ df.timeout(0, :timeout, :foo)
25
+ df.errback do |type, name|
26
+ args = [type, name]
27
+ EM.stop
28
+ end
29
+
30
+ EM.add_timer(0.01) { flunk "Deferrable was not timed out." }
31
+ }
32
+
33
+ assert_equal [:timeout, :foo], args
34
+ end
35
+ end
@@ -0,0 +1,141 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ class TestEpoll < Test::Unit::TestCase
4
+
5
+ module TestEchoServer
6
+ def receive_data data
7
+ send_data data
8
+ close_connection_after_writing
9
+ end
10
+ end
11
+
12
+ module TestEchoClient
13
+ def connection_completed
14
+ send_data "ABCDE"
15
+ $max += 1
16
+ end
17
+ def receive_data data
18
+ raise "bad response" unless data == "ABCDE"
19
+ end
20
+ def unbind
21
+ $n -= 1
22
+ EM.stop if $n == 0
23
+ end
24
+ end
25
+
26
+
27
+ # We can set the rlimit/nofile of a process but we can only set it
28
+ # higher if we're running as root.
29
+ # On most systems, the default value is 1024.
30
+ def test_rlimit
31
+ omit_if(windows? || jruby?)
32
+ unless EM.set_descriptor_table_size >= 1024
33
+ a = EM.set_descriptor_table_size
34
+ assert( a <= 1024 )
35
+ a = EM.set_descriptor_table_size( 1024 )
36
+ assert( a == 1024 )
37
+ end
38
+ end
39
+
40
+ # Run a high-volume version of this test by kicking the number of connections
41
+ # up past 512. (Each connection uses two sockets, a client and a server.)
42
+ # (Will require running the test as root)
43
+ # This test exercises TCP clients and servers.
44
+ #
45
+ # XXX this test causes all sort of weird issues on OSX (when run as part of the suite)
46
+ def _test_descriptors
47
+ EM.epoll
48
+ EM.set_descriptor_table_size 60000
49
+ EM.run {
50
+ EM.start_server "127.0.0.1", 9800, TestEchoServer
51
+ $n = 0
52
+ $max = 0
53
+ 100.times {
54
+ EM.connect("127.0.0.1", 9800, TestEchoClient) {$n += 1}
55
+ }
56
+ }
57
+ assert_equal(0, $n)
58
+ assert_equal(100, $max)
59
+ end
60
+
61
+ def setup
62
+ @port = next_port
63
+ end
64
+
65
+ module TestDatagramServer
66
+ def receive_data dgm
67
+ $in = dgm
68
+ send_data "abcdefghij"
69
+ end
70
+ end
71
+ module TestDatagramClient
72
+ def initialize port
73
+ @port = port
74
+ end
75
+
76
+ def post_init
77
+ send_datagram "1234567890", "127.0.0.1", @port
78
+ end
79
+
80
+ def receive_data dgm
81
+ $out = dgm
82
+ EM.stop
83
+ end
84
+ end
85
+
86
+ def test_datagrams
87
+ $in = $out = ""
88
+ EM.run {
89
+ EM.open_datagram_socket "127.0.0.1", @port, TestDatagramServer
90
+ EM.open_datagram_socket "127.0.0.1", 0, TestDatagramClient, @port
91
+ }
92
+ assert_equal( "1234567890", $in )
93
+ assert_equal( "abcdefghij", $out )
94
+ end
95
+
96
+ # XXX this test fails randomly...
97
+ def _test_unix_domain
98
+ fn = "/tmp/xxx.chain"
99
+ EM.epoll
100
+ EM.set_descriptor_table_size 60000
101
+ EM.run {
102
+ # The pure-Ruby version won't let us open the socket if the node already exists.
103
+ # Not sure, that actually may be correct and the compiled version is wrong.
104
+ # Pure Ruby also oddly won't let us make that many connections. This test used
105
+ # to run 100 times. Not sure where that lower connection-limit is coming from in
106
+ # pure Ruby.
107
+ # Let's not sweat the Unix-ness of the filename, since this test can't possibly
108
+ # work on Windows anyway.
109
+ #
110
+ File.unlink(fn) if File.exist?(fn)
111
+ EM.start_unix_domain_server fn, TestEchoServer
112
+ $n = 0
113
+ $max = 0
114
+ 50.times {
115
+ EM.connect_unix_domain(fn, TestEchoClient) {$n += 1}
116
+ }
117
+ EM::add_timer(1) { $stderr.puts("test_unix_domain timed out!"); EM::stop }
118
+ }
119
+ assert_equal(0, $n)
120
+ assert_equal(50, $max)
121
+ ensure
122
+ File.unlink(fn) if File.exist?(fn)
123
+ end
124
+
125
+ def test_attach_detach
126
+ EM.epoll
127
+ EM.run {
128
+ EM.add_timer(0.01) { EM.stop }
129
+
130
+ r, _ = IO.pipe
131
+
132
+ # This tests a regression where detach in the same tick as attach crashes EM
133
+ EM.watch(r) do |connection|
134
+ connection.detach
135
+ end
136
+ }
137
+
138
+ assert true
139
+ end
140
+ end
141
+