diff options
Diffstat (limited to 'test/test_tcp_splice.rb')
-rw-r--r-- | test/test_tcp_splice.rb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/test/test_tcp_splice.rb b/test/test_tcp_splice.rb new file mode 100644 index 0000000..2ddbfcb --- /dev/null +++ b/test/test_tcp_splice.rb @@ -0,0 +1,49 @@ +require 'socket' +require 'io/wait' +require 'io/splice' +require 'io/nonblock' +require "test/unit" + +class TestTCPCopyStream < Test::Unit::TestCase + def setup + host = ENV["TEST_HOST"] || "127.0.0.1" + @srv = TCPServer.new(host, 0) + @port = @srv.addr[1] + @client = TCPSocket.new(host, @port) + @client.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1 + @accept = @srv.accept + @accept.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1 + @client.sync = @accept.sync = true + @r, @w = IO.pipe + end + + def teardown + @srv.close + [ @client, @accept, @r, @w ].each { |io| io.close unless io.closed? } + end + + def test_client_to_server_eof + nr = 2000 + buf = '0123456789abcdef' * 1024 + expect = buf.size * nr + thr = Thread.new do + nr.times { @client.write(buf) } + @client.close + end + sleep 1 # wait for rcvbuf to fill up + bytes = IO::Splice.copy_stream(@accept, "/dev/null") + assert_equal expect, bytes + end + + def test_client_to_server_expect + nr = 2000 + buf = '0123456789abcdef' * 1024 + expect = buf.size * nr + thr = Thread.new do + nr.times { @client.write(buf) } + end + sleep 1 # wait for rcvbuf to fill up + bytes = IO::Splice.copy_stream(@accept, "/dev/null", expect) + assert_equal expect, bytes + end +end |