1 # -*- encoding: binary -*-
6 class Test_IO_Splice < Test::Unit::TestCase
12 tmp = Tempfile.new(nil)
14 assert_nothing_raised {
19 nr = IO.splice(tmp.fileno, nil, wr.fileno, nil, size, 0)
21 assert_equal str, rd.sysread(size)
24 def test_splice_in_offset
29 tmp = Tempfile.new(nil)
31 assert_nothing_raised {
36 nr = IO.splice(tmp.fileno, off, wr.fileno, nil, len, 0)
38 assert_equal 'de', rd.sysread(len)
41 def test_splice_out_offset
44 tmp = Tempfile.new(nil)
46 assert_nothing_raised { wr.syswrite(str) }
47 nr = IO.splice(rd.fileno, nil, tmp.fileno, 3, str.size, 0)
49 assert_nothing_raised { tmp.sysseek(0) }
50 assert_equal "\0\0\0abcde", tmp.sysread(9)
53 def test_splice_nonblock
55 tmp = Tempfile.new(nil)
57 assert_raises(Errno::EAGAIN) {
58 IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
64 tmp = Tempfile.new(nil)
68 nr = IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
70 assert_raises(EOFError) {
71 IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
81 assert_nothing_raised { wra.syswrite(str) }
82 nr = IO.tee(rda.fileno, wrb.fileno, size, 0)
84 assert_equal str, rdb.sysread(5)
85 assert_equal str, rda.sysread(5)
92 assert_raises(EOFError) { IO.tee(rda.fileno, wrb.fileno, 4096, 0) }
98 assert_raises(Errno::EAGAIN) {
99 IO.tee(rda.fileno, wrb.fileno, 4096, IO::Splice::F_NONBLOCK)
103 def test_vmsplice_array
104 data = %w(hello world how are you today)
106 n = IO.vmsplice(w.fileno, data, 0)
107 assert_equal data.join('').size, n
108 assert_equal data.join(''), r.readpartial(16384)
111 def test_vmsplice_nonblock
112 data = %w(hello world how are you today)
114 w.syswrite('.' * IO::Splice::PIPE_CAPA)
115 assert_raises(Errno::EAGAIN) {
116 IO.vmsplice(w.fileno, data, IO::Splice::F_NONBLOCK)
120 def test_vmsplice_in_full
123 # bs * count should be > PIPE_BUF
124 [ [ 512, 512 ], [ 131073, 3 ], [ 4098, 64 ] ].each do |(bs,count)|
126 buf = File.open('/dev/urandom', 'rb') { |fp| fp.sysread(bs) }
128 vec = (1..count).map { buf }
134 tmp << rd.readpartial(8192, buf)
138 ok = (vec.join(empty) == tmp.join(empty))
141 assert_nothing_raised { rd.close }
142 assert_equal(bs * count, IO.vmsplice(wr.fileno, vec, 0))
143 assert_nothing_raised { wr.close }
144 _, status = Process.waitpid2(pid)
145 assert status.success?
150 assert IO::Splice::PIPE_BUF > 0
151 %w(move nonblock more gift).each { |x|
152 assert Integer === IO::Splice.const_get("F_#{x.upcase}")
154 assert IO::Splice::PIPE_CAPA >= IO::Splice::PIPE_BUF