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,48 @@
1
+ require 'em_test_helper'
2
+
3
+ if EM.kqueue?
4
+ class TestProcessWatch < Test::Unit::TestCase
5
+ module ParentProcessWatcher
6
+ def process_forked
7
+ $forked = true
8
+ end
9
+ end
10
+
11
+ module ChildProcessWatcher
12
+ def process_exited
13
+ $exited = true
14
+ end
15
+ def unbind
16
+ $unbind = true
17
+ EM.stop
18
+ end
19
+ end
20
+
21
+ def setup
22
+ EM.kqueue = true
23
+ end
24
+
25
+ def teardown
26
+ EM.kqueue = false
27
+ end
28
+
29
+ def test_events
30
+ EM.run{
31
+ # watch ourselves for a fork notification
32
+ EM.watch_process(Process.pid, ParentProcessWatcher)
33
+ $fork_pid = fork{ sleep }
34
+ child = EM.watch_process($fork_pid, ChildProcessWatcher)
35
+ $pid = child.pid
36
+
37
+ EM.add_timer(0.2){
38
+ Process.kill('TERM', $fork_pid)
39
+ }
40
+ }
41
+
42
+ assert_equal($pid, $fork_pid)
43
+ assert($forked)
44
+ assert($exited)
45
+ assert($unbind)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,133 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestProcesses < Test::Unit::TestCase
4
+
5
+ if !windows? && !jruby?
6
+
7
+ # EM::DeferrableChildProcess is a sugaring of a common use-case
8
+ # involving EM::popen.
9
+ # Call the #open method on EM::DeferrableChildProcess, passing
10
+ # a command-string. #open immediately returns an EM::Deferrable
11
+ # object. It also schedules the forking of a child process, which
12
+ # will execute the command passed to #open.
13
+ # When the forked child terminates, the Deferrable will be signalled
14
+ # and execute its callbacks, passing the data that the child process
15
+ # wrote to stdout.
16
+ #
17
+ def test_deferrable_child_process
18
+ ls = ""
19
+ EM.run {
20
+ d = EM::DeferrableChildProcess.open( "ls -ltr" )
21
+ d.callback {|data_from_child|
22
+ ls = data_from_child
23
+ EM.stop
24
+ }
25
+ }
26
+ assert( ls.length > 0)
27
+ end
28
+
29
+ def setup
30
+ $out = nil
31
+ $status = nil
32
+ end
33
+
34
+ def test_em_system
35
+ EM.run{
36
+ EM.system('ls'){ |out,status| $out, $status = out, status; EM.stop }
37
+ }
38
+
39
+ assert( $out.length > 0 )
40
+ assert_equal($status.exitstatus, 0)
41
+ assert_equal($status.class, Process::Status)
42
+ end
43
+
44
+ def test_em_system_pid
45
+ $pids = []
46
+
47
+ EM.run{
48
+ $pids << EM.system('echo hi', proc{ |out,status|$pids << status.pid; EM.stop })
49
+ }
50
+
51
+ assert_equal $pids[0], $pids[1]
52
+ end
53
+
54
+ def test_em_system_with_proc
55
+ EM.run{
56
+ EM.system('ls', proc{ |out,status| $out, $status = out, status; EM.stop })
57
+ }
58
+
59
+ assert( $out.length > 0 )
60
+ assert_equal($status.exitstatus, 0)
61
+ assert_equal($status.class, Process::Status)
62
+ end
63
+
64
+ def test_em_system_with_two_procs
65
+ EM.run{
66
+ EM.system('sh', proc{ |process|
67
+ process.send_data("echo hello\n")
68
+ process.send_data("exit\n")
69
+ }, proc{ |out,status|
70
+ $out = out
71
+ $status = status
72
+ EM.stop
73
+ })
74
+ }
75
+
76
+ assert_equal("hello\n", $out)
77
+ end
78
+
79
+ def test_em_system_cmd_arguments
80
+ EM.run{
81
+ EM.system('echo', '1', '2', 'version', proc{ |process|
82
+ }, proc{ |out,status|
83
+ $out = out
84
+ $status = status
85
+ EM.stop
86
+ })
87
+ }
88
+
89
+ assert_match(/1 2 version/i, $out)
90
+ end
91
+
92
+ def test_em_system_spaced_arguments
93
+ EM.run{
94
+ EM.system('ruby', '-e', 'puts "hello"', proc{ |out,status|
95
+ $out = out
96
+ EM.stop
97
+ })
98
+ }
99
+
100
+ assert_equal("hello\n", $out)
101
+ end
102
+
103
+ def test_em_popen_pause_resume
104
+ c_rx = 0
105
+
106
+ test_client = Module.new do
107
+ define_method :receive_data do |data|
108
+ c_rx += 1
109
+ pause
110
+ EM.add_timer(0.5) { EM.stop }
111
+ end
112
+ end
113
+
114
+ EM.run{
115
+ EM.popen('cat /dev/random', test_client)
116
+ }
117
+
118
+ # XXX FIXME this test is a bit broken. Since
119
+ # PipeDescriptor::Read reads up to 10 data units and does not
120
+ # check bPaused, depending on a race condition any value between
121
+ # 1 and 10 is possible here. If this is a bug, it needs to be
122
+ # fixed in pipe.c assert_equal 1, c_rx
123
+ assert((1..10) === c_rx, "#{c_rx} data batches made it in")
124
+ end
125
+ else
126
+ warn "EM.popen not implemented, skipping tests in #{__FILE__}"
127
+
128
+ # Because some rubies will complain if a TestCase class has no tests
129
+ def test_em_popen_unsupported
130
+ assert true
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,168 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestProxyConnection < Test::Unit::TestCase
4
+
5
+ if EM.respond_to?(:start_proxy)
6
+ module ProxyConnection
7
+ def initialize(client, request)
8
+ @client, @request = client, request
9
+ end
10
+
11
+ def post_init
12
+ EM::enable_proxy(self, @client)
13
+ end
14
+
15
+ def connection_completed
16
+ EM.next_tick {
17
+ send_data @request
18
+ }
19
+ end
20
+
21
+ def proxy_target_unbound
22
+ $unbound_early = true
23
+ EM.stop
24
+ end
25
+
26
+ def unbind
27
+ @client.close_connection_after_writing
28
+ end
29
+ end
30
+
31
+ module PartialProxyConnection
32
+ def initialize(client, request, length)
33
+ @client, @request, @length = client, request, length
34
+ end
35
+
36
+ def post_init
37
+ EM::enable_proxy(self, @client, 0, @length)
38
+ end
39
+
40
+ def receive_data(data)
41
+ $unproxied_data = data
42
+ @client.send_data(data)
43
+ end
44
+
45
+ def connection_completed
46
+ EM.next_tick {
47
+ send_data @request
48
+ }
49
+ end
50
+
51
+ def proxy_target_unbound
52
+ $unbound_early = true
53
+ EM.stop
54
+ end
55
+
56
+ def proxy_completed
57
+ $proxy_completed = true
58
+ end
59
+
60
+ def unbind
61
+ @client.close_connection_after_writing
62
+ end
63
+ end
64
+
65
+ module Client
66
+ def connection_completed
67
+ send_data "EM rocks!"
68
+ end
69
+
70
+ def receive_data(data)
71
+ $client_data = data
72
+ end
73
+
74
+ def unbind
75
+ EM.stop
76
+ end
77
+ end
78
+
79
+ module Client2
80
+ include Client
81
+ def unbind; end
82
+ end
83
+
84
+ module Server
85
+ def receive_data(data)
86
+ send_data "I know!" if data == "EM rocks!"
87
+ close_connection_after_writing
88
+ end
89
+ end
90
+
91
+ module ProxyServer
92
+ def initialize port
93
+ @port = port
94
+ end
95
+
96
+ def receive_data(data)
97
+ EM.connect("127.0.0.1", @port, ProxyConnection, self, data)
98
+ end
99
+ end
100
+
101
+ module PartialProxyServer
102
+ def initialize port
103
+ @port = port
104
+ end
105
+
106
+ def receive_data(data)
107
+ EM.connect("127.0.0.1", @port, PartialProxyConnection, self, data, 1)
108
+ end
109
+ end
110
+
111
+ module EarlyClosingProxy
112
+ def initialize port
113
+ @port = port
114
+ end
115
+
116
+ def receive_data(data)
117
+ EM.connect("127.0.0.1", @port, ProxyConnection, self, data)
118
+ close_connection
119
+ end
120
+ end
121
+
122
+ def setup
123
+ @port = next_port
124
+ @proxy_port = next_port
125
+ end
126
+
127
+ def test_proxy_connection
128
+ EM.run {
129
+ EM.start_server("127.0.0.1", @port, Server)
130
+ EM.start_server("127.0.0.1", @proxy_port, ProxyServer, @port)
131
+ EM.connect("127.0.0.1", @proxy_port, Client)
132
+ }
133
+
134
+ assert_equal("I know!", $client_data)
135
+ end
136
+
137
+ def test_partial_proxy_connection
138
+ EM.run {
139
+ EM.start_server("127.0.0.1", @port, Server)
140
+ EM.start_server("127.0.0.1", @proxy_port, PartialProxyServer, @port)
141
+ EM.connect("127.0.0.1", @proxy_port, Client)
142
+ }
143
+
144
+ assert_equal("I know!", $client_data)
145
+ assert_equal(" know!", $unproxied_data)
146
+ assert($proxy_completed)
147
+ end
148
+
149
+ def test_early_close
150
+ $client_data = nil
151
+ EM.run {
152
+ EM.start_server("127.0.0.1", @port, Server)
153
+ EM.start_server("127.0.0.1", @proxy_port, EarlyClosingProxy, @port)
154
+ EM.connect("127.0.0.1", @proxy_port, Client2)
155
+ }
156
+
157
+ assert($unbound_early)
158
+ end
159
+ else
160
+ warn "EM.start_proxy not implemented, skipping tests in #{__FILE__}"
161
+
162
+ # Because some rubies will complain if a TestCase class has no tests
163
+ def test_em_start_proxy_not_implemented
164
+ assert !EM.respond_to?(:start_proxy)
165
+ end
166
+ end
167
+
168
+ end
@@ -0,0 +1,88 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestPure < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @port = next_port
7
+ end
8
+
9
+ # These tests are intended to exercise problems that come up in the
10
+ # pure-Ruby implementation. However, we DON'T constrain them such that
11
+ # they only run in pure-Ruby. These tests need to work identically in
12
+ # any implementation.
13
+
14
+ #-------------------------------------
15
+
16
+ # The EM reactor needs to run down open connections and release other resources
17
+ # when it stops running. Make sure this happens even if user code throws a Ruby
18
+ # exception.
19
+ # If exception handling is incorrect, the second test will fail with a no-bind error
20
+ # because the TCP server opened in the first test will not have been closed.
21
+
22
+ def test_exception_handling_releases_resources
23
+ exception = Class.new(StandardError)
24
+
25
+ 2.times do
26
+ assert_raises(exception) do
27
+ EM.run do
28
+ EM.start_server "127.0.0.1", @port
29
+ raise exception
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ # Under some circumstances, the pure Ruby library would emit an Errno::ECONNREFUSED
36
+ # exception on certain kinds of TCP connect-errors.
37
+ # It's always been something of an open question whether EM should throw an exception
38
+ # in these cases but the defined answer has always been to catch it the unbind method.
39
+ # With a connect failure, the latter will always fire, but connection_completed will
40
+ # never fire. So even though the point is arguable, it's incorrect for the pure Ruby
41
+ # version to throw an exception.
42
+ module TestConnrefused
43
+ def unbind
44
+ EM.stop
45
+ end
46
+ def connection_completed
47
+ raise "should never get here"
48
+ end
49
+ end
50
+
51
+ def test_connrefused
52
+ assert_nothing_raised do
53
+ EM.run {
54
+ setup_timeout(2)
55
+ EM.connect "127.0.0.1", @port, TestConnrefused
56
+ }
57
+ end
58
+ end
59
+
60
+ # Make sure connection_completed gets called as expected with TCP clients. This is the
61
+ # opposite of test_connrefused.
62
+ # If the test fails, it will hang because EM.stop never gets called.
63
+ #
64
+ module TestConnaccepted
65
+ def connection_completed
66
+ EM.stop
67
+ end
68
+ end
69
+ def test_connaccepted
70
+ assert_nothing_raised do
71
+ EM.run {
72
+ EM.start_server "127.0.0.1", @port
73
+ EM.connect "127.0.0.1", @port, TestConnaccepted
74
+ setup_timeout(1)
75
+ }
76
+ end
77
+ end
78
+
79
+ def test_reactor_running
80
+ a = false
81
+ EM.run {
82
+ a = EM.reactor_running?
83
+ EM.next_tick {EM.stop}
84
+ }
85
+ assert a
86
+ end
87
+
88
+ end
@@ -0,0 +1,50 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestEMQueue < Test::Unit::TestCase
4
+ def test_queue_push
5
+ s = 0
6
+ EM.run do
7
+ q = EM::Queue.new
8
+ q.push(1)
9
+ EM.next_tick { s = q.size; EM.stop }
10
+ end
11
+ assert_equal 1, s
12
+ end
13
+
14
+ def test_queue_pop
15
+ x,y,z = nil
16
+ EM.run do
17
+ q = EM::Queue.new
18
+ q.push(1,2,3)
19
+ q.pop { |v| x = v }
20
+ q.pop { |v| y = v }
21
+ q.pop { |v| z = v; EM.stop }
22
+ end
23
+ assert_equal 1, x
24
+ assert_equal 2, y
25
+ assert_equal 3, z
26
+ end
27
+
28
+ def test_queue_reactor_thread
29
+ q = EM::Queue.new
30
+
31
+ Thread.new { q.push(1,2,3) }.join
32
+ assert q.empty?
33
+ EM.run { EM.next_tick { EM.stop } }
34
+ assert_equal 3, q.size
35
+
36
+ x = nil
37
+ Thread.new { q.pop { |v| x = v } }.join
38
+ assert_equal nil, x
39
+ EM.run { EM.next_tick { EM.stop } }
40
+ assert_equal 1, x
41
+ end
42
+
43
+ def test_num_waiting
44
+ q = EM::Queue.new
45
+ many = 3
46
+ many.times { q.pop {} }
47
+ EM.run { EM.next_tick { EM.stop } }
48
+ assert_equal many, q.num_waiting
49
+ end
50
+ end