1 # -*- encoding: binary -*-
10 class Test_IO_Splice < Test::Unit::TestCase
16 tmp = Tempfile.new('ruby_io_splice')
18 assert_nothing_raised {
23 nr = IO.splice(tmp.fileno, nil, wr.fileno, nil, size, 0)
25 assert_equal str, rd.sysread(size)
32 tmp = Tempfile.new('ruby_io_splice')
34 assert_nothing_raised {
39 nr = IO.splice(tmp, nil, wr, nil, size, 0)
41 assert_equal str, rd.sysread(size)
44 def test_splice_io_noflags
48 tmp = Tempfile.new('ruby_io_splice')
50 assert_nothing_raised {
55 nr = IO.splice(tmp, nil, wr, nil, size)
57 assert_equal str, rd.sysread(size)
60 def test_trysplice_io_noflags
64 tmp = Tempfile.new('ruby_io_splice')
66 assert_nothing_raised {
71 nr = IO.trysplice(tmp, nil, wr, nil, size)
73 assert_equal str, rd.sysread(size)
76 def test_splice_io_ish
80 tmp = Tempfile.new('ruby_io_splice')
86 assert_nothing_raised {
91 nr = IO.splice(io_ish, nil, wr, nil, size, 0)
93 assert_equal str, rd.sysread(size)
96 def test_splice_in_offset
101 tmp = Tempfile.new('ruby_io_splice')
103 assert_nothing_raised {
108 nr = IO.splice(tmp.fileno, off, wr.fileno, nil, len, 0)
110 assert_equal 'de', rd.sysread(len)
113 def test_splice_out_offset
116 tmp = Tempfile.new('ruby_io_splice')
118 assert_nothing_raised { wr.syswrite(str) }
119 nr = IO.splice(rd.fileno, nil, tmp.fileno, 3, str.size, 0)
121 assert_nothing_raised { tmp.sysseek(0) }
122 assert_equal "\0\0\0abcde", tmp.sysread(9)
125 def test_splice_nonblock
127 tmp = Tempfile.new('ruby_io_splice')
129 assert_raises(Errno::EAGAIN) {
130 IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
134 def test_trysplice_nonblock
136 tmp = Tempfile.new('ruby_io_splice')
137 assert_equal :EAGAIN,
138 IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_NONBLOCK)
143 tmp = Tempfile.new('ruby_io_splice')
147 nr = IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
149 assert_raises(EOFError) {
150 IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
154 def test_trysplice_eof
156 tmp = Tempfile.new('ruby_io_splice')
160 nr = IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_NONBLOCK)
162 assert_nil IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_NONBLOCK)
165 def test_splice_nonblock_socket
166 server = TCPServer.new('127.0.0.1', 0)
167 port = server.addr[1]
169 rs = TCPSocket.new('127.0.0.1', port)
171 assert_raises(Errno::EAGAIN) { IO.splice(rs, nil, wp, nil, 1024, 0) }
182 assert_nothing_raised { wra.syswrite(str) }
183 nr = IO.tee(rda.fileno, wrb.fileno, size, 0)
185 assert_equal str, rdb.sysread(5)
186 assert_equal str, rda.sysread(5)
193 assert_raises(EOFError) { IO.tee(rda.fileno, wrb.fileno, 4096, 0) }
196 def test_tee_nonblock
199 assert_raises(Errno::EAGAIN) {
200 IO.tee(rda.fileno, wrb.fileno, 4096, IO::Splice::F_NONBLOCK)
210 assert_nothing_raised { wra.syswrite(str) }
211 nr = IO.tee(rda, wrb, size, 0)
213 assert_equal str, rdb.sysread(5)
214 assert_equal str, rda.sysread(5)
217 def test_vmsplice_array
218 data = %w(hello world how are you today)
220 n = IO.vmsplice(w.fileno, data, 0)
221 assert_equal data.join('').size, n
222 assert_equal data.join(''), r.readpartial(16384)
225 def test_vmsplice_string
227 assert_equal 5, IO.vmsplice(w, 'hello', 0)
228 assert_equal 'hello', r.read(5)
231 def test_vmsplice_array_io
232 data = %w(hello world how are you today)
234 n = IO.vmsplice(w, data, 0)
235 assert_equal data.join('').size, n
236 assert_equal data.join(''), r.readpartial(16384)
239 def test_vmsplice_nonblock
240 data = %w(hello world how are you today)
242 w.syswrite('.' * IO::Splice::PIPE_CAPA)
243 assert_raises(Errno::EAGAIN) {
244 IO.vmsplice(w.fileno, data, IO::Splice::F_NONBLOCK)
248 def test_vmsplice_in_full
251 # bs * count should be > PIPE_BUF
252 [ [ 512, 512 ], [ 131073, 3 ], [ 4098, 64 ] ].each do |(bs,count)|
254 buf = File.open('/dev/urandom', 'rb') { |fp| fp.sysread(bs) }
256 vec = (1..count).map { buf }
262 tmp << rd.readpartial(8192)
266 ok = (vec.join(empty) == tmp.join(empty))
269 assert_nothing_raised { rd.close }
270 assert_equal(bs * count, IO.vmsplice(wr.fileno, vec, 0))
271 assert_nothing_raised { wr.close }
272 _, status = Process.waitpid2(pid)
273 assert status.success?
277 def test_vmsplice_nil
278 data = %w(hello world how are you today)
279 assert_raises(TypeError) { IO.vmsplice(nil, data, 0) }
283 assert IO::Splice::PIPE_BUF > 0
284 %w(move nonblock more gift).each { |x|
285 assert Integer === IO::Splice.const_get("F_#{x.upcase}")
287 assert IO::Splice::PIPE_CAPA >= IO::Splice::PIPE_BUF
290 def test_splice_copy_stream_file_to_file_small
291 a, b = Tempfile.new('a'), Tempfile.new('b')
292 a.syswrite 'hello world'
294 IO::Splice.copy_stream(a, b)
296 assert_equal 'hello world', b.read
299 def test_splice_copy_stream_file_to_file_big
300 buf = ('ab' * IO::Splice::PIPE_CAPA) + 'hi'
301 a, b = Tempfile.new('a'), Tempfile.new('b')
304 IO::Splice.copy_stream(a, b)
306 assert_equal buf, b.read
309 def test_splice_copy_stream_file_to_file_big_partial
310 nr = IO::Splice::PIPE_CAPA
311 buf = ('ab' * nr) + 'hi'
312 a, b = Tempfile.new('a'), Tempfile.new('b')
315 assert_equal nr, IO::Splice.copy_stream(a, b, nr)
317 assert_equal('ab' * (nr/2), b.read)
320 def test_splice_copy_stream_file_to_file_len
321 a, b = Tempfile.new('a'), Tempfile.new('b')
322 a.syswrite 'hello world'
324 IO::Splice.copy_stream(a, b, 5)
326 assert_equal 'hello', b.read
329 def test_splice_copy_stream_pipe_to_file_len
330 a = Tempfile.new('a')
332 w.syswrite 'hello world'
333 IO::Splice.copy_stream(r, a, 5)
335 assert_equal 'hello', a.read
338 def test_splice_copy_stream_paths
339 a = Tempfile.new('a')
340 b = Tempfile.new('a')
341 a.syswrite('hello world')
342 IO::Splice.copy_stream(a.path, b.path, 5)
343 assert_equal 'hello', b.read
346 def test_splice_copy_stream_src_offset
347 a = Tempfile.new('a')
348 b = Tempfile.new('a')
349 a.syswrite('hello world')
350 IO::Splice.copy_stream(a.path, b.path, 5, 6)
351 assert_equal 'world', b.read
354 def test_copy_stream_nonblock_src
355 server = TCPServer.new('127.0.0.1', 0)
356 port = server.addr[1]
358 rs = TCPSocket.new('127.0.0.1', port)
361 assert_raises(Timeout::Error) do
362 timeout(0.05) { nr += IO::Splice.copy_stream(rs, wp, 5) }
369 def test_copy_stream_nonblock_dst
370 server = TCPServer.new('127.0.0.1', 0)
371 port = server.addr[1]
373 rs = TCPSocket.new('127.0.0.1', port)
375 client = server.accept
376 buf = ' ' * IO::Splice::PIPE_CAPA
378 assert_raises(Timeout::Error) do
381 wp.write_nonblock(buf)
385 nr += IO::Splice.copy_stream(rp, rs, IO::Splice::PIPE_CAPA)
389 assert_equal nr, client.read(nr).size
394 def test_copy_stream_eof
396 w.syswrite 'hello world'
398 a = Tempfile.new('a')
399 assert_equal 11, IO::Splice.copy_stream(r, a)
401 assert_equal 'hello world', a.read
406 assert_kind_of Integer, r.pipe_size
407 assert(r.pipe_size >= 512)
408 assert_nothing_raised { w.pipe_size = 8192 }
409 assert_equal 8192, r.pipe_size
412 assert_raises(Errno::EBUSY) { r.pipe_size = 4096 }
414 pipe_max_size = File.read("/proc/sys/fs/pipe-max-size").to_i
415 assert_nothing_raised { r.pipe_size = pipe_max_size }
416 assert_raises(Errno::EPERM) { r.pipe_size = pipe_max_size * 2 }
417 end if IO.method_defined?(:pipe_size)