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,55 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestResolver < Test::Unit::TestCase
4
+ def test_a
5
+ EM.run {
6
+ d = EM::DNS::Resolver.resolve "google.com"
7
+ d.errback { |msg, err| assert false, "msg: #{msg.inspect}, err: #{err.inspect}" }
8
+ d.callback { |r|
9
+ assert r
10
+ EM.stop
11
+ }
12
+ }
13
+ end
14
+
15
+ def test_bad_host
16
+ EM.run {
17
+ d = EM::DNS::Resolver.resolve "asdfasasdf"
18
+ d.callback { assert false }
19
+ d.errback { assert true; EM.stop }
20
+ }
21
+ end
22
+
23
+ def test_garbage
24
+ assert_raises( ArgumentError ) {
25
+ EM.run {
26
+ EM::DNS::Resolver.resolve 123
27
+ }
28
+ }
29
+ end
30
+
31
+ def test_a_pair
32
+ EM.run {
33
+ d = EM::DNS::Resolver.resolve "google.com"
34
+ d.errback { |msg, err| assert false, "msg: #{msg.inspect}, err: #{err.inspect}" }
35
+ d.callback { |r|
36
+ assert_equal(Array, r.class)
37
+ assert r.size > 1
38
+ EM.stop
39
+ }
40
+ }
41
+ end
42
+
43
+ def test_localhost
44
+ EM.run {
45
+ d = EM::DNS::Resolver.resolve "localhost"
46
+ d.errback { assert false }
47
+ d.callback { |r|
48
+ assert_equal("127.0.0.1", r.first)
49
+ assert_equal(Array, r.class)
50
+
51
+ EM.stop
52
+ }
53
+ }
54
+ end
55
+ end
@@ -0,0 +1,14 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestRunning < Test::Unit::TestCase
4
+ def test_running
5
+ assert_equal( false, EM::reactor_running? )
6
+ r = false
7
+ EM.run {
8
+ r = EM::reactor_running?
9
+ EM.stop
10
+ }
11
+ assert_equal( true, r )
12
+ end
13
+ end
14
+
@@ -0,0 +1,47 @@
1
+ require 'em_test_helper'
2
+
3
+
4
+ class TestSASL < Test::Unit::TestCase
5
+
6
+ # SASL authentication is usually done with UNIX-domain sockets, but
7
+ # we'll use TCP so this test will work on Windows. As far as the
8
+ # protocol handlers are concerned, there's no difference.
9
+
10
+ TestUser,TestPsw = "someone", "password"
11
+
12
+ class SaslServer < EM::Connection
13
+ include EM::Protocols::SASLauth
14
+ def validate usr, psw, sys, realm
15
+ usr == TestUser and psw == TestPsw
16
+ end
17
+ end
18
+
19
+ class SaslClient < EM::Connection
20
+ include EM::Protocols::SASLauthclient
21
+ end
22
+
23
+ def setup
24
+ @port = next_port
25
+ end
26
+
27
+ def test_sasl
28
+ resp = nil
29
+ EM.run {
30
+ EM.start_server( "127.0.0.1", @port, SaslServer )
31
+
32
+ c = EM.connect( "127.0.0.1", @port, SaslClient )
33
+ d = c.validate?( TestUser, TestPsw )
34
+ d.timeout 1
35
+ d.callback {
36
+ resp = true
37
+ EM.stop
38
+ }
39
+ d.errback {
40
+ resp = false
41
+ EM.stop
42
+ }
43
+ }
44
+ assert_equal( true, resp )
45
+ end
46
+
47
+ end
@@ -0,0 +1,217 @@
1
+ require 'em_test_helper'
2
+ require 'tempfile'
3
+
4
+ class TestSendFile < Test::Unit::TestCase
5
+
6
+ if EM.respond_to?(:send_file_data)
7
+ module TestModule
8
+ def initialize filename
9
+ @filename = filename
10
+ end
11
+
12
+ def post_init
13
+ send_file_data @filename
14
+ close_connection_after_writing
15
+ end
16
+ end
17
+
18
+ module TestClient
19
+ def data_to(&blk)
20
+ @data_to = blk
21
+ end
22
+
23
+ def receive_data(data)
24
+ @data_to.call(data) if @data_to
25
+ end
26
+
27
+ def unbind
28
+ EM.stop
29
+ end
30
+ end
31
+
32
+ def setup
33
+ @file = Tempfile.new("em_test_file")
34
+ @filename = @file.path
35
+ @port = next_port
36
+ end
37
+
38
+ def test_send_file
39
+ File.open( @filename, "w" ) {|f|
40
+ f << ("A" * 5000)
41
+ }
42
+
43
+ data = ''
44
+
45
+ EM.run {
46
+ EM.start_server "127.0.0.1", @port, TestModule, @filename
47
+ setup_timeout
48
+
49
+ EM.connect "127.0.0.1", @port, TestClient do |c|
50
+ c.data_to { |d| data << d }
51
+ end
52
+ }
53
+
54
+ assert_equal( "A" * 5000, data )
55
+ end
56
+
57
+ # EM::Connection#send_file_data has a strict upper limit on the filesize it will work with.
58
+ def test_send_large_file
59
+ File.open( @filename, "w" ) {|f|
60
+ f << ("A" * 1000000)
61
+ }
62
+
63
+ data = ''
64
+
65
+ assert_raises(RuntimeError) {
66
+ EM.run {
67
+ EM.start_server "127.0.0.1", @port, TestModule, @filename
68
+ setup_timeout
69
+ EM.connect "127.0.0.1", @port, TestClient do |c|
70
+ c.data_to { |d| data << d }
71
+ end
72
+ }
73
+ }
74
+ end
75
+
76
+ module StreamTestModule
77
+ def initialize filename
78
+ @filename = filename
79
+ end
80
+
81
+ def post_init
82
+ EM::Deferrable.future( stream_file_data(@filename)) {
83
+ close_connection_after_writing
84
+ }
85
+ end
86
+ end
87
+
88
+ module ChunkStreamTestModule
89
+ def initialize filename
90
+ @filename = filename
91
+ end
92
+
93
+ def post_init
94
+ EM::Deferrable.future( stream_file_data(@filename, :http_chunks=>true)) {
95
+ close_connection_after_writing
96
+ }
97
+ end
98
+ end
99
+
100
+ def test_stream_file_data
101
+ File.open( @filename, "w" ) {|f|
102
+ f << ("A" * 1000)
103
+ }
104
+
105
+ data = ''
106
+
107
+ EM.run {
108
+ EM.start_server "127.0.0.1", @port, StreamTestModule, @filename
109
+ setup_timeout
110
+ EM.connect "127.0.0.1", @port, TestClient do |c|
111
+ c.data_to { |d| data << d }
112
+ end
113
+ }
114
+
115
+ assert_equal( "A" * 1000, data )
116
+ end
117
+
118
+ def test_stream_chunked_file_data
119
+ File.open( @filename, "w" ) {|f|
120
+ f << ("A" * 1000)
121
+ }
122
+
123
+ data = ''
124
+
125
+ EM.run {
126
+ EM.start_server "127.0.0.1", @port, ChunkStreamTestModule, @filename
127
+ setup_timeout
128
+ EM.connect "127.0.0.1", @port, TestClient do |c|
129
+ c.data_to { |d| data << d }
130
+ end
131
+ }
132
+
133
+ assert_equal( "3e8\r\n#{"A" * 1000}\r\n0\r\n\r\n", data )
134
+ end
135
+
136
+ module BadFileTestModule
137
+ def initialize filename
138
+ @filename = filename
139
+ end
140
+
141
+ def post_init
142
+ de = stream_file_data( @filename+".wrong" )
143
+ de.errback {|msg|
144
+ send_data msg
145
+ close_connection_after_writing
146
+ }
147
+ end
148
+ end
149
+ def test_stream_bad_file
150
+ data = ''
151
+ EM.run {
152
+ EM.start_server "127.0.0.1", @port, BadFileTestModule, @filename
153
+ setup_timeout(5)
154
+ EM.connect "127.0.0.1", @port, TestClient do |c|
155
+ c.data_to { |d| data << d }
156
+ end
157
+ }
158
+
159
+ assert_equal( "file not found", data )
160
+ end
161
+ else
162
+ warn "EM.send_file_data not implemented, skipping tests in #{__FILE__}"
163
+
164
+ # Because some rubies will complain if a TestCase class has no tests
165
+ def test_em_send_file_data_not_implemented
166
+ assert !EM.respond_to?(:send_file_data)
167
+ end
168
+ end
169
+
170
+ begin
171
+ require 'fastfilereaderext'
172
+
173
+ def test_stream_large_file_data
174
+ File.open( @filename, "w" ) {|f|
175
+ f << ("A" * 10000)
176
+ }
177
+
178
+ data = ''
179
+
180
+ EM.run {
181
+ EM.start_server "127.0.0.1", @port, StreamTestModule, @filename
182
+ setup_timeout
183
+ EM.connect "127.0.0.1", @port, TestClient do |c|
184
+ c.data_to { |d| data << d }
185
+ end
186
+ }
187
+
188
+ assert_equal( "A" * 10000, data )
189
+ end
190
+
191
+ def test_stream_large_chunked_file_data
192
+ File.open( @filename, "w" ) {|f|
193
+ f << ("A" * 100000)
194
+ }
195
+
196
+ data = ''
197
+
198
+ EM.run {
199
+ EM.start_server "127.0.0.1", @port, ChunkStreamTestModule, @filename
200
+ setup_timeout
201
+ EM.connect "127.0.0.1", @port, TestClient do |c|
202
+ c.data_to { |d| data << d }
203
+ end
204
+ }
205
+
206
+ expected = [
207
+ "4000\r\n#{"A" * 16384}\r\n" * 6,
208
+ "6a0\r\n#{"A" * 0x6a0}\r\n",
209
+ "0\r\n\r\n"
210
+ ].join
211
+ assert_equal( expected, data )
212
+ end
213
+ rescue LoadError
214
+ warn "require 'fastfilereaderext' failed, skipping tests in #{__FILE__}"
215
+ end
216
+
217
+ end
@@ -0,0 +1,33 @@
1
+ require 'em_test_helper'
2
+ require 'socket'
3
+
4
+ class TestServers < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @port = next_port
8
+ end
9
+
10
+ def server_alive?
11
+ port_in_use?(@port)
12
+ end
13
+
14
+ def run_test_stop_server
15
+ EM.run {
16
+ sig = EM.start_server("127.0.0.1", @port)
17
+ assert server_alive?, "Server didn't start"
18
+ EM.stop_server sig
19
+ # Give the server some time to shutdown.
20
+ EM.add_timer(0.1) {
21
+ assert !server_alive?, "Server didn't stop"
22
+ EM.stop
23
+ }
24
+ }
25
+ end
26
+
27
+ def test_stop_server
28
+ assert !server_alive?, "Port already in use"
29
+ 2.times { run_test_stop_server }
30
+ assert !server_alive?, "Servers didn't stop"
31
+ end
32
+
33
+ end
@@ -0,0 +1,41 @@
1
+ require 'em_test_helper'
2
+ require 'socket'
3
+
4
+ class TestSetSockOpt < Test::Unit::TestCase
5
+
6
+ if EM.respond_to? :set_sock_opt
7
+ def setup
8
+ assert(!EM.reactor_running?)
9
+ end
10
+
11
+ def teardown
12
+ assert(!EM.reactor_running?)
13
+ end
14
+
15
+ #-------------------------------------
16
+
17
+ def test_set_sock_opt
18
+ test = self
19
+ EM.run do
20
+ EM.connect 'google.com', 80, Module.new {
21
+ define_method :post_init do
22
+ wanted = 10240
23
+ val = set_sock_opt Socket::SOL_SOCKET, Socket::SO_RCVBUF, wanted
24
+ test.assert_equal 0, val
25
+ val = get_sock_opt Socket::SOL_SOCKET, Socket::SO_RCVBUF
26
+ obtained = val.unpack("L")[0]
27
+ test.assert obtained == wanted || obtained == wanted * 2 # Linux at work
28
+ EM.stop
29
+ end
30
+ }
31
+ end
32
+ end
33
+ else
34
+ warn "EM.set_sock_opt not implemented, skipping tests in #{__FILE__}"
35
+
36
+ # Because some rubies will complain if a TestCase class has no tests
37
+ def test_em_set_sock_opt_unsupported
38
+ assert true
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,23 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestShutdownHooks < Test::Unit::TestCase
4
+ def test_shutdown_hooks
5
+ r = false
6
+ EM.run {
7
+ EM.add_shutdown_hook { r = true }
8
+ EM.stop
9
+ }
10
+ assert_equal( true, r )
11
+ end
12
+
13
+ def test_hook_order
14
+ r = []
15
+ EM.run {
16
+ EM.add_shutdown_hook { r << 2 }
17
+ EM.add_shutdown_hook { r << 1 }
18
+ EM.stop
19
+ }
20
+ assert_equal( [1, 2], r )
21
+ end
22
+ end
23
+
@@ -0,0 +1,55 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestSmtpClient < Test::Unit::TestCase
4
+
5
+ Localhost = "127.0.0.1"
6
+ Localport = 9801
7
+
8
+ def setup
9
+ end
10
+
11
+ def teardown
12
+ end
13
+
14
+ def test_a
15
+ # No real tests until we have a server implementation to test against.
16
+ # This is what the call looks like, though:
17
+ err = nil
18
+ EM.run {
19
+ d = EM::Protocols::SmtpClient.send :domain=>"example.com",
20
+ :host=>Localhost,
21
+ :port=>Localport, # optional, defaults 25
22
+ :starttls=>true,
23
+ :from=>"sender@example.com",
24
+ :to=> ["to_1@example.com", "to_2@example.com"],
25
+ :header=> {"Subject" => "This is a subject line"},
26
+ :body=> "This is the body of the email",
27
+ :verbose=>true
28
+ d.errback {|e|
29
+ err = e
30
+ EM.stop
31
+ }
32
+ }
33
+ assert(err)
34
+ end
35
+
36
+ def test_content
37
+ err = nil
38
+ EM.run {
39
+ d = EM::Protocols::SmtpClient.send :domain=>"example.com",
40
+ :host=>Localhost,
41
+ :port=>Localport, # optional, defaults 25
42
+ :starttls=>true,
43
+ :from=>"sender@example.com",
44
+ :to=> ["to_1@example.com", "to_2@example.com"],
45
+ :content => ["Subject: xxx\r\n\r\ndata\r\n.\r\n"],
46
+ :verbose=>true
47
+ d.errback {|e|
48
+ err = e
49
+ EM.stop
50
+ }
51
+ }
52
+ assert(err)
53
+ end
54
+
55
+ end