get_paths defaults to noverify: true
[ruby-mogilefs-client.git] / test / socket_test.rb
blob75e852b075e22b0bbcb3f06a85c0d62cf234fd3a
1 require "socket"
2 require "test/unit"
3 module SocketTest
5   def setup
6     @host = ENV["TEST_HOST"] || '127.0.0.1'
7     @srv = TCPServer.new(@host, 0)
8     @port = @srv.addr[1]
9   end
11   def test_start
12     sock = MogileFS::Socket.start(@host, @port)
13     assert_instance_of MogileFS::Socket, sock, sock.inspect
14     assert_nothing_raised do
15       begin
16         sock.write_nonblock("a")
17       rescue Errno::EAGAIN
18       end
19     end
20     thr = Thread.new { @srv.accept }
21     accepted = thr.value
22     assert_instance_of TCPSocket, accepted, accepted.inspect
23     assert_nil sock.close
24   end
26   def test_new
27     sock = MogileFS::Socket.tcp(@host, @port)
28     assert_instance_of MogileFS::Socket, sock, sock.inspect
29     assert_nothing_raised do
30       sock.write_nonblock("a")
31     end
32     thr = Thread.new { @srv.accept }
33     accepted = thr.value
34     assert_instance_of TCPSocket, accepted, accepted.inspect
35     assert_equal "a", accepted.read(1)
36     assert_nil sock.close
37   end
39   def test_timed_peek
40     sock = MogileFS::Socket.tcp(@host, @port)
41     accepted = @srv.accept
42     buf = ""
43     assert_raises(MogileFS::UnreadableSocketError) do
44       sock.timed_peek(2, buf, 0.01)
45     end
46     accepted.write "HI"
47     assert_equal "HI", sock.timed_peek(2, buf, 0.1)
48     assert_equal "HI", buf
49     assert_equal "HI", sock.timed_peek(2, buf)
50     assert_equal "HI", sock.timed_read(2, buf)
51     accepted.close
52     assert_nil sock.timed_peek(2, buf)
53   end
55   def test_timed_read
56     sock = MogileFS::Socket.tcp(@host, @port)
57     accepted = @srv.accept
58     buf = ""
59     assert_raises(MogileFS::UnreadableSocketError) do
60       sock.timed_read(2, buf, 0.01)
61     end
62     accepted.write "HI"
63     assert_equal "HI", sock.timed_read(2, buf, 0.1)
64     assert_equal "HI", buf
65     assert_raises(MogileFS::UnreadableSocketError) do
66       sock.timed_read(2, buf, 0.01)
67     end
68     accepted.close
69     assert_nil sock.timed_read(2, buf)
70   end
72   def test_timed_write
73     sock = MogileFS::Socket.tcp(@host, @port)
74     accepted = @srv.accept
75     buf = "A" * 100000
76     written = 0
77     assert_raises(MogileFS::RequestTruncatedError) do
78       loop do
79         sock.timed_write(buf, 0.01)
80         written += buf.size
81       end
82     end
83     tmp = accepted.read(written)
84     assert_equal written, tmp.size
85   end
87   def timed_gets
88     sock = MogileFS::Socket.tcp(@host, @port)
89     accepted = @srv.accept
90     assert_raises(MogileFS::UnreadableSocketError) { sock.timed_gets(0.01) }
91     accepted.write "HI"
92     assert_raises(MogileFS::UnreadableSocketError) { sock.timed_gets(0.01) }
93     accepted.write "\n"
94     assert_equal "HI\n", sock.timed_gets
95     accepted.close
96     assert_nil sock.timed_gets
97   end
98 end