1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
require 'em_test_helper'
require 'socket'
class TestAttach < Test::Unit::TestCase
class EchoServer < EM::Connection
def receive_data data
send_data data
end
end
class EchoClient < EM::Connection
def initialize socket
self.notify_readable = true
@socket = socket
@socket.write("abc\n")
end
def notify_readable
$read = @socket.readline
$fd = detach
end
def unbind
EM.next_tick do
@socket.write("def\n")
EM.add_timer(0.1) { EM.stop }
end
end
end
def setup
@port = next_port
$read, $r, $w, $fd = nil
end
def teardown
[$r, $w].each do |io|
io.close rescue nil
end
end
def test_attach
socket = nil
EM.run {
EM.start_server "127.0.0.1", @port, EchoServer
socket = TCPSocket.new "127.0.0.1", @port
EM.watch socket, EchoClient, socket
}
assert_equal $read, "abc\n"
unless jruby? # jruby filenos are not real
assert_equal $fd, socket.fileno
end
assert_equal false, socket.closed?
assert_equal socket.readline, "def\n"
end
module PipeWatch
def notify_readable
$read = $r.readline
EM.stop
end
end
def test_attach_pipe
EM.run{
$r, $w = IO.pipe
EM.watch $r, PipeWatch do |c|
c.notify_readable = true
end
$w.write("ghi\n")
}
assert_equal $read, "ghi\n"
end
def test_set_readable
before, after = nil
EM.run{
$r, $w = IO.pipe
c = EM.watch $r, PipeWatch do |con|
con.notify_readable = false
end
EM.next_tick{
before = c.notify_readable?
c.notify_readable = true
after = c.notify_readable?
}
$w.write("jkl\n")
}
assert !before
assert after
assert_equal $read, "jkl\n"
end
def test_read_write_pipe
result = nil
pipe_reader = Module.new do
define_method :receive_data do |data|
result = data
EM.stop
end
end
r,w = IO.pipe
EM.run {
EM.attach r, pipe_reader
writer = EM.attach(w)
writer.send_data 'ghi'
# XXX: Process will hang in Windows without this line
writer.close_connection_after_writing
}
assert_equal "ghi", result
ensure
[r,w].each {|io| io.close rescue nil }
end
end
|