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
| | require "socket"
require "test/unit"
module SocketTest
def setup
@host = ENV["TEST_HOST"] || '127.0.0.1'
@srv = TCPServer.new(@host, 0)
@port = @srv.addr[1]
end
def test_start
sock = MogileFS::Socket.start(@host, @port)
assert_instance_of MogileFS::Socket, sock, sock.inspect
assert_nothing_raised do
begin
sock.write_nonblock("a")
rescue Errno::EAGAIN
end
end
thr = Thread.new { @srv.accept }
accepted = thr.value
assert_instance_of TCPSocket, accepted, accepted.inspect
assert_nil sock.close
end
def test_new
sock = MogileFS::Socket.tcp(@host, @port)
assert_instance_of MogileFS::Socket, sock, sock.inspect
assert_nothing_raised do
sock.write_nonblock("a")
end
thr = Thread.new { @srv.accept }
accepted = thr.value
assert_instance_of TCPSocket, accepted, accepted.inspect
assert_equal "a", accepted.read(1)
assert_nil sock.close
end
def test_timed_peek
sock = MogileFS::Socket.tcp(@host, @port)
accepted = @srv.accept
buf = ""
assert_raises(MogileFS::UnreadableSocketError) do
sock.timed_peek(2, buf, 0.01)
end
accepted.write "HI"
assert_equal "HI", sock.timed_peek(2, buf, 0.1)
assert_equal "HI", buf
assert_equal "HI", sock.timed_peek(2, buf)
assert_equal "HI", sock.timed_read(2, buf)
accepted.close
assert_nil sock.timed_peek(2, buf)
end
def test_timed_read
sock = MogileFS::Socket.tcp(@host, @port)
accepted = @srv.accept
buf = ""
assert_raises(MogileFS::UnreadableSocketError) do
sock.timed_read(2, buf, 0.01)
end
accepted.write "HI"
assert_equal "HI", sock.timed_read(2, buf, 0.1)
assert_equal "HI", buf
assert_raises(MogileFS::UnreadableSocketError) do
sock.timed_read(2, buf, 0.01)
end
accepted.close
assert_nil sock.timed_read(2, buf)
end
def test_timed_write
sock = MogileFS::Socket.tcp(@host, @port)
accepted = @srv.accept
buf = "A" * 100000
written = 0
assert_raises(MogileFS::RequestTruncatedError) do
loop do
assert_equal buf.bytesize, sock.timed_write(buf, 0.01)
written += buf.size
end
end
tmp = accepted.read(written)
assert_equal written, tmp.size
end
def timed_gets
sock = MogileFS::Socket.tcp(@host, @port)
accepted = @srv.accept
assert_raises(MogileFS::UnreadableSocketError) { sock.timed_gets(0.01) }
accepted.write "HI"
assert_raises(MogileFS::UnreadableSocketError) { sock.timed_gets(0.01) }
accepted.write "\n"
assert_equal "HI\n", sock.timed_gets
accepted.close
assert_nil sock.timed_gets
end
def test_read_in_full
sock = MogileFS::Socket.tcp(@host, @port)
accepted = @srv.accept
thr = Thread.new do
accepted.write "HELLO"
sleep 0.1
accepted.write " "
sleep 0.1
accepted.write "WORLD!!!"
end
buf = sock.read("HELLO WORLD!!!".bytesize)
assert_equal "HELLO WORLD!!!", buf
end
end
|