set close-on-exec in pipe size modification check
[ruby_io_splice.git] / test / test_tcp_splice.rb
blob686333c1c5414ffbc4ceb9e76dca63de1c5d50d3
1 require 'socket'
2 require 'io/wait'
3 require 'io/splice'
4 require 'io/nonblock'
5 require "test/unit"
7 class TestTCPCopyStream < Test::Unit::TestCase
8   def setup
9     host = ENV["TEST_HOST"] || "127.0.0.1"
10     @srv = TCPServer.new(host, 0)
11     @port = @srv.addr[1]
12     @client = TCPSocket.new(host, @port)
13     @client.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
14     @accept = @srv.accept
15     @accept.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
16     @client.sync = @accept.sync = true
17     @r, @w = IO.pipe
18   end
20   def teardown
21     @srv.close
22     [ @client, @accept, @r, @w ].each { |io| io.close unless io.closed? }
23   end
25   def test_client_to_server_eof
26     nr = 2000
27     buf = '0123456789abcdef' * 1024
28     expect = buf.size * nr
29     thr = Thread.new do
30       nr.times { @client.write(buf) }
31       @client.close
32     end
33     sleep 1 # wait for rcvbuf to fill up
34     bytes = IO::Splice.copy_stream(@accept, "/dev/null")
35     assert_equal expect, bytes
36   end
38   def test_client_to_server_expect
39     nr = 2000
40     buf = '0123456789abcdef' * 1024
41     expect = buf.size * nr
42     thr = Thread.new do
43       nr.times { @client.write(buf) }
44     end
45     sleep 1 # wait for rcvbuf to fill up
46     bytes = IO::Splice.copy_stream(@accept, "/dev/null", expect)
47     assert_equal expect, bytes
48   end
50   def test_mega_splice
51     nr = 2000
52     buf = '0123456789abcdef' * 1024
53     expect = buf.size * nr
54     thr = Thread.new do
55       nr.times { @client.write(buf) }
56       @client.close
57     end
58     size_t_max = if (1 << 30).kind_of?(Bignum)
59       0xffffffff
60     else
61       0xffffffffffffffff
62     end
63     bytes = IO::Splice.copy_stream(@accept, "/dev/null", size_t_max)
64     assert_equal expect, bytes
65   end
66 end