wj_eventmachine 1.3.0.dev.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +179 -0
- data/GNU +281 -0
- data/LICENSE +60 -0
- data/README.md +110 -0
- data/docs/DocumentationGuidesIndex.md +27 -0
- data/docs/GettingStarted.md +520 -0
- data/docs/old/ChangeLog +211 -0
- data/docs/old/DEFERRABLES +246 -0
- data/docs/old/EPOLL +141 -0
- data/docs/old/INSTALL +13 -0
- data/docs/old/KEYBOARD +42 -0
- data/docs/old/LEGAL +25 -0
- data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
- data/docs/old/PURE_RUBY +75 -0
- data/docs/old/RELEASE_NOTES +94 -0
- data/docs/old/SMTP +4 -0
- data/docs/old/SPAWNED_PROCESSES +148 -0
- data/docs/old/TODO +8 -0
- data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
- data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
- data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
- data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
- data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
- data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
- data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
- data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
- data/examples/old/ex_channel.rb +43 -0
- data/examples/old/ex_queue.rb +2 -0
- data/examples/old/ex_tick_loop_array.rb +15 -0
- data/examples/old/ex_tick_loop_counter.rb +32 -0
- data/examples/old/helper.rb +2 -0
- data/ext/binder.cpp +124 -0
- data/ext/binder.h +52 -0
- data/ext/cmain.cpp +1046 -0
- data/ext/ed.cpp +2238 -0
- data/ext/ed.h +460 -0
- data/ext/em.cpp +2378 -0
- data/ext/em.h +266 -0
- data/ext/eventmachine.h +152 -0
- data/ext/extconf.rb +285 -0
- data/ext/fastfilereader/extconf.rb +120 -0
- data/ext/fastfilereader/mapper.cpp +214 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +126 -0
- data/ext/kb.cpp +79 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +354 -0
- data/ext/project.h +174 -0
- data/ext/rubymain.cpp +1610 -0
- data/ext/ssl.cpp +627 -0
- data/ext/ssl.h +103 -0
- data/ext/wait_for_single_fd.h +36 -0
- data/java/.classpath +8 -0
- data/java/.project +17 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +625 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EmReactorInterface.java +70 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +72 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +201 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +415 -0
- data/java/src/com/rubyeventmachine/NullEmReactor.java +157 -0
- data/java/src/com/rubyeventmachine/NullEventableChannel.java +81 -0
- data/lib/em/buftok.rb +59 -0
- data/lib/em/callback.rb +58 -0
- data/lib/em/channel.rb +69 -0
- data/lib/em/completion.rb +307 -0
- data/lib/em/connection.rb +776 -0
- data/lib/em/deferrable.rb +210 -0
- data/lib/em/deferrable/pool.rb +2 -0
- data/lib/em/file_watch.rb +73 -0
- data/lib/em/future.rb +61 -0
- data/lib/em/io_streamer.rb +68 -0
- data/lib/em/iterator.rb +252 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/pool.rb +151 -0
- data/lib/em/process_watch.rb +45 -0
- data/lib/em/processes.rb +123 -0
- data/lib/em/protocols.rb +37 -0
- data/lib/em/protocols/header_and_content.rb +138 -0
- data/lib/em/protocols/httpclient.rb +303 -0
- data/lib/em/protocols/httpclient2.rb +602 -0
- data/lib/em/protocols/line_and_text.rb +125 -0
- data/lib/em/protocols/line_protocol.rb +33 -0
- data/lib/em/protocols/linetext2.rb +179 -0
- data/lib/em/protocols/memcache.rb +331 -0
- data/lib/em/protocols/object_protocol.rb +46 -0
- data/lib/em/protocols/postgres3.rb +246 -0
- data/lib/em/protocols/saslauth.rb +175 -0
- data/lib/em/protocols/smtpclient.rb +394 -0
- data/lib/em/protocols/smtpserver.rb +666 -0
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/protocols/stomp.rb +205 -0
- data/lib/em/protocols/tcptest.rb +54 -0
- data/lib/em/pure_ruby.rb +1299 -0
- data/lib/em/queue.rb +80 -0
- data/lib/em/resolver.rb +232 -0
- data/lib/em/spawnable.rb +84 -0
- data/lib/em/streamer.rb +118 -0
- data/lib/em/threaded_resource.rb +90 -0
- data/lib/em/tick_loop.rb +85 -0
- data/lib/em/timers.rb +61 -0
- data/lib/em/version.rb +3 -0
- data/lib/eventmachine.rb +1602 -0
- data/lib/jeventmachine.rb +318 -0
- data/rakelib/package.rake +120 -0
- data/rakelib/test.rake +6 -0
- data/rakelib/test_pure.rake +11 -0
- data/tests/client.crt +31 -0
- data/tests/client.key +51 -0
- data/tests/dhparam.pem +13 -0
- data/tests/em_ssl_handlers.rb +153 -0
- data/tests/em_test_helper.rb +198 -0
- data/tests/jruby/test_jeventmachine.rb +38 -0
- data/tests/test_attach.rb +199 -0
- data/tests/test_basic.rb +321 -0
- data/tests/test_channel.rb +75 -0
- data/tests/test_completion.rb +178 -0
- data/tests/test_connection_count.rb +83 -0
- data/tests/test_connection_write.rb +35 -0
- data/tests/test_defer.rb +35 -0
- data/tests/test_deferrable.rb +35 -0
- data/tests/test_epoll.rb +141 -0
- data/tests/test_error_handler.rb +38 -0
- data/tests/test_exc.rb +37 -0
- data/tests/test_file_watch.rb +86 -0
- data/tests/test_fork.rb +75 -0
- data/tests/test_futures.rb +170 -0
- data/tests/test_handler_check.rb +35 -0
- data/tests/test_hc.rb +155 -0
- data/tests/test_httpclient.rb +238 -0
- data/tests/test_httpclient2.rb +132 -0
- data/tests/test_idle_connection.rb +31 -0
- data/tests/test_inactivity_timeout.rb +102 -0
- data/tests/test_io_streamer.rb +47 -0
- data/tests/test_ipv4.rb +96 -0
- data/tests/test_ipv6.rb +107 -0
- data/tests/test_iterator.rb +122 -0
- data/tests/test_kb.rb +28 -0
- data/tests/test_keepalive.rb +113 -0
- data/tests/test_line_protocol.rb +33 -0
- data/tests/test_ltp.rb +155 -0
- data/tests/test_ltp2.rb +332 -0
- data/tests/test_many_fds.rb +21 -0
- data/tests/test_next_tick.rb +104 -0
- data/tests/test_object_protocol.rb +36 -0
- data/tests/test_pause.rb +109 -0
- data/tests/test_pending_connect_timeout.rb +52 -0
- data/tests/test_pool.rb +196 -0
- data/tests/test_process_watch.rb +50 -0
- data/tests/test_processes.rb +128 -0
- data/tests/test_proxy_connection.rb +180 -0
- data/tests/test_pure.rb +156 -0
- data/tests/test_queue.rb +64 -0
- data/tests/test_resolver.rb +129 -0
- data/tests/test_running.rb +14 -0
- data/tests/test_sasl.rb +46 -0
- data/tests/test_send_file.rb +217 -0
- data/tests/test_servers.rb +32 -0
- data/tests/test_shutdown_hooks.rb +23 -0
- data/tests/test_smtpclient.rb +75 -0
- data/tests/test_smtpserver.rb +90 -0
- data/tests/test_sock_opt.rb +53 -0
- data/tests/test_spawn.rb +290 -0
- data/tests/test_ssl_args.rb +41 -0
- data/tests/test_ssl_dhparam.rb +57 -0
- data/tests/test_ssl_ecdh_curve.rb +57 -0
- data/tests/test_ssl_extensions.rb +24 -0
- data/tests/test_ssl_methods.rb +31 -0
- data/tests/test_ssl_protocols.rb +190 -0
- data/tests/test_ssl_verify.rb +52 -0
- data/tests/test_stomp.rb +38 -0
- data/tests/test_system.rb +46 -0
- data/tests/test_threaded_resource.rb +68 -0
- data/tests/test_tick_loop.rb +58 -0
- data/tests/test_timers.rb +150 -0
- data/tests/test_ud.rb +8 -0
- data/tests/test_unbind_reason.rb +40 -0
- metadata +384 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
require_relative '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
|
+
omit_if(rbx?)
|
31
|
+
omit_if(jruby?)
|
32
|
+
EM.run{
|
33
|
+
# watch ourselves for a fork notification
|
34
|
+
EM.watch_process(Process.pid, ParentProcessWatcher)
|
35
|
+
$fork_pid = fork{ sleep }
|
36
|
+
child = EM.watch_process($fork_pid, ChildProcessWatcher)
|
37
|
+
$pid = child.pid
|
38
|
+
|
39
|
+
EM.add_timer(0.2){
|
40
|
+
Process.kill('TERM', $fork_pid)
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
assert_equal($pid, $fork_pid)
|
45
|
+
assert($forked)
|
46
|
+
assert($exited)
|
47
|
+
assert($unbind)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require_relative '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(0, $status.exitstatus)
|
41
|
+
assert_kind_of(Process::Status, $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(0, $status.exitstatus)
|
61
|
+
assert_kind_of(Process::Status, $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 do
|
115
|
+
EM.popen('echo 1', test_client)
|
116
|
+
end
|
117
|
+
|
118
|
+
assert_equal 1, c_rx
|
119
|
+
end
|
120
|
+
else
|
121
|
+
warn "EM.popen not implemented, skipping tests in #{__FILE__}"
|
122
|
+
|
123
|
+
# Because some rubies will complain if a TestCase class has no tests
|
124
|
+
def test_em_popen_unsupported
|
125
|
+
assert true
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
require_relative '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
|
+
$proxied_bytes = self.get_proxied_bytes
|
28
|
+
@client.close_connection_after_writing
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module PartialProxyConnection
|
33
|
+
def initialize(client, request, length)
|
34
|
+
@client, @request, @length = client, request, length
|
35
|
+
end
|
36
|
+
|
37
|
+
def post_init
|
38
|
+
EM::enable_proxy(self, @client, 0, @length)
|
39
|
+
end
|
40
|
+
|
41
|
+
def receive_data(data)
|
42
|
+
$unproxied_data = data
|
43
|
+
@client.send_data(data)
|
44
|
+
end
|
45
|
+
|
46
|
+
def connection_completed
|
47
|
+
EM.next_tick {
|
48
|
+
send_data @request
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
def proxy_target_unbound
|
53
|
+
$unbound_early = true
|
54
|
+
EM.stop
|
55
|
+
end
|
56
|
+
|
57
|
+
def proxy_completed
|
58
|
+
$proxy_completed = true
|
59
|
+
end
|
60
|
+
|
61
|
+
def unbind
|
62
|
+
@client.close_connection_after_writing
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
module Client
|
67
|
+
def connection_completed
|
68
|
+
send_data "EM rocks!"
|
69
|
+
end
|
70
|
+
|
71
|
+
def receive_data(data)
|
72
|
+
$client_data = data
|
73
|
+
end
|
74
|
+
|
75
|
+
def unbind
|
76
|
+
EM.stop
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
module Client2
|
81
|
+
include Client
|
82
|
+
def unbind; end
|
83
|
+
end
|
84
|
+
|
85
|
+
module Server
|
86
|
+
def receive_data(data)
|
87
|
+
send_data "I know!" if data == "EM rocks!"
|
88
|
+
close_connection_after_writing
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
module ProxyServer
|
93
|
+
def initialize port
|
94
|
+
@port = port
|
95
|
+
end
|
96
|
+
|
97
|
+
def receive_data(data)
|
98
|
+
@proxy = EM.connect("127.0.0.1", @port, ProxyConnection, self, data)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
module PartialProxyServer
|
103
|
+
def initialize port
|
104
|
+
@port = port
|
105
|
+
end
|
106
|
+
|
107
|
+
def receive_data(data)
|
108
|
+
EM.connect("127.0.0.1", @port, PartialProxyConnection, self, data, 1)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
module EarlyClosingProxy
|
113
|
+
def initialize port
|
114
|
+
@port = port
|
115
|
+
end
|
116
|
+
|
117
|
+
def receive_data(data)
|
118
|
+
EM.connect("127.0.0.1", @port, ProxyConnection, self, data)
|
119
|
+
close_connection
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def setup
|
124
|
+
@port = next_port
|
125
|
+
@proxy_port = next_port
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_proxy_connection
|
129
|
+
EM.run {
|
130
|
+
EM.start_server("127.0.0.1", @port, Server)
|
131
|
+
EM.start_server("127.0.0.1", @proxy_port, ProxyServer, @port)
|
132
|
+
EM.connect("127.0.0.1", @proxy_port, Client)
|
133
|
+
}
|
134
|
+
|
135
|
+
assert_equal("I know!", $client_data)
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_proxied_bytes
|
139
|
+
EM.run {
|
140
|
+
EM.start_server("127.0.0.1", @port, Server)
|
141
|
+
EM.start_server("127.0.0.1", @proxy_port, ProxyServer, @port)
|
142
|
+
EM.connect("127.0.0.1", @proxy_port, Client)
|
143
|
+
}
|
144
|
+
|
145
|
+
assert_equal("I know!", $client_data)
|
146
|
+
assert_equal("I know!".bytesize, $proxied_bytes)
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_partial_proxy_connection
|
150
|
+
EM.run {
|
151
|
+
EM.start_server("127.0.0.1", @port, Server)
|
152
|
+
EM.start_server("127.0.0.1", @proxy_port, PartialProxyServer, @port)
|
153
|
+
EM.connect("127.0.0.1", @proxy_port, Client)
|
154
|
+
}
|
155
|
+
|
156
|
+
assert_equal("I know!", $client_data)
|
157
|
+
assert_equal(" know!", $unproxied_data)
|
158
|
+
assert($proxy_completed)
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_early_close
|
162
|
+
$client_data = nil
|
163
|
+
EM.run {
|
164
|
+
EM.start_server("127.0.0.1", @port, Server)
|
165
|
+
EM.start_server("127.0.0.1", @proxy_port, EarlyClosingProxy, @port)
|
166
|
+
EM.connect("127.0.0.1", @proxy_port, Client2)
|
167
|
+
}
|
168
|
+
|
169
|
+
assert($unbound_early)
|
170
|
+
end
|
171
|
+
else
|
172
|
+
warn "EM.start_proxy not implemented, skipping tests in #{__FILE__}"
|
173
|
+
|
174
|
+
# Because some rubies will complain if a TestCase class has no tests
|
175
|
+
def test_em_start_proxy_not_implemented
|
176
|
+
assert !EM.respond_to?(:start_proxy)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
data/tests/test_pure.rb
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
require_relative '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
|
+
module TLSServer
|
89
|
+
def post_init
|
90
|
+
start_tls
|
91
|
+
end
|
92
|
+
|
93
|
+
def ssl_handshake_completed
|
94
|
+
$server_handshake_completed = true
|
95
|
+
end
|
96
|
+
|
97
|
+
def receive_data(data)
|
98
|
+
$server_received_data = data
|
99
|
+
send_data(data)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
module TLSClient
|
104
|
+
def post_init
|
105
|
+
start_tls
|
106
|
+
end
|
107
|
+
|
108
|
+
def ssl_handshake_completed
|
109
|
+
$client_handshake_completed = true
|
110
|
+
end
|
111
|
+
|
112
|
+
def connection_completed
|
113
|
+
send_data('Hello World!')
|
114
|
+
end
|
115
|
+
|
116
|
+
def receive_data(data)
|
117
|
+
$client_received_data = data
|
118
|
+
close_connection
|
119
|
+
end
|
120
|
+
|
121
|
+
def unbind
|
122
|
+
EM.stop_event_loop
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_start_tls
|
127
|
+
omit("No SSL") unless EM.ssl?
|
128
|
+
$client_handshake_completed, $server_handshake_completed = false, false
|
129
|
+
$client_received_data, $server_received_data = nil, nil
|
130
|
+
EM.run do
|
131
|
+
EM.start_server("127.0.0.1", 16789, TLSServer)
|
132
|
+
EM.connect("127.0.0.1", 16789, TLSClient)
|
133
|
+
end
|
134
|
+
|
135
|
+
assert($client_handshake_completed)
|
136
|
+
assert($server_handshake_completed)
|
137
|
+
assert($client_received_data == "Hello World!")
|
138
|
+
assert($server_received_data == "Hello World!")
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_periodic_timer
|
142
|
+
x = 0
|
143
|
+
start, finish = nil
|
144
|
+
|
145
|
+
EM.run {
|
146
|
+
start = Time.now.to_f
|
147
|
+
EM::PeriodicTimer.new(0.2) do
|
148
|
+
x += 1
|
149
|
+
finish = Time.now.to_f
|
150
|
+
EM.stop if x == 4
|
151
|
+
end
|
152
|
+
}
|
153
|
+
assert_in_delta 0.8, (finish - start), (darwin? ? 0.6 : 0.2)
|
154
|
+
assert_equal 4, x
|
155
|
+
end
|
156
|
+
end
|