copy_stream: always use SPLICE_F_NONBLOCK for partial copy
[ruby_io_splice.git] / test / test_tcp_splice.rb
blob2ddbfcb208ca85a8d48e12258772609d0ddaf97e
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
49 end