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)
141 def test_trysplice_nonblock_noargs
143 tmp = Tempfile.new('ruby_io_splice')
144 assert_equal :EAGAIN, IO.trysplice(rd, nil, tmp, 0, 5)
145 assert_equal :EAGAIN, IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_MORE)
150 tmp = Tempfile.new('ruby_io_splice')
154 nr = IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
156 assert_raises(EOFError) {
157 IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
161 def test_trysplice_eof
163 tmp = Tempfile.new('ruby_io_splice')
167 nr = IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_NONBLOCK)
169 assert_nil IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_NONBLOCK)
172 def test_splice_nonblock_socket
173 server = TCPServer.new('127.0.0.1', 0)
174 port = server.addr[1]
176 rs = TCPSocket.new('127.0.0.1', port)
178 assert_raises(Errno::EAGAIN) { IO.splice(rs, nil, wp, nil, 1024, 0) }
189 assert_nothing_raised { wra.syswrite(str) }
190 nr = IO.tee(rda.fileno, wrb.fileno, size, 0)
192 assert_equal str, rdb.sysread(5)
193 assert_equal str, rda.sysread(5)
200 assert_raises(EOFError) { IO.tee(rda.fileno, wrb.fileno, 4096, 0) }
203 def test_tee_nonblock
206 assert_raises(Errno::EAGAIN) {
207 IO.tee(rda.fileno, wrb.fileno, 4096, IO::Splice::F_NONBLOCK)
217 assert_nothing_raised { wra.syswrite(str) }
218 nr = IO.tee(rda, wrb, size, 0)
220 assert_equal str, rdb.sysread(5)
221 assert_equal str, rda.sysread(5)
224 def test_vmsplice_array
225 data = %w(hello world how are you today)
227 n = IO.vmsplice(w.fileno, data, 0)
228 assert_equal data.join('').size, n
229 assert_equal data.join(''), r.readpartial(16384)
232 def test_vmsplice_string
234 assert_equal 5, IO.vmsplice(w, 'hello', 0)
235 assert_equal 'hello', r.read(5)
238 def test_vmsplice_array_io
239 data = %w(hello world how are you today)
241 n = IO.vmsplice(w, data, 0)
242 assert_equal data.join('').size, n
243 assert_equal data.join(''), r.readpartial(16384)
246 def test_vmsplice_nonblock
247 data = %w(hello world how are you today)
249 w.syswrite('.' * IO::Splice::PIPE_CAPA)
250 assert_raises(Errno::EAGAIN) {
251 IO.vmsplice(w.fileno, data, IO::Splice::F_NONBLOCK)
255 def test_vmsplice_in_full
258 # bs * count should be > PIPE_BUF
259 [ [ 512, 512 ], [ 131073, 3 ], [ 4098, 64 ] ].each do |(bs,count)|
261 buf = File.open('/dev/urandom', 'rb') { |fp| fp.sysread(bs) }
263 vec = (1..count).map { buf }
269 tmp << rd.readpartial(8192)
273 ok = (vec.join(empty) == tmp.join(empty))
276 assert_nothing_raised { rd.close }
277 assert_equal(bs * count, IO.vmsplice(wr.fileno, vec, 0))
278 assert_nothing_raised { wr.close }
279 _, status = Process.waitpid2(pid)
280 assert status.success?
284 def test_vmsplice_nil
285 data = %w(hello world how are you today)
286 assert_raises(TypeError) { IO.vmsplice(nil, data, 0) }
290 assert IO::Splice::PIPE_BUF > 0
291 %w(move nonblock more gift).each { |x|
292 assert Integer === IO::Splice.const_get("F_#{x.upcase}")
294 assert IO::Splice::PIPE_CAPA >= IO::Splice::PIPE_BUF
297 def test_splice_copy_stream_file_to_file_small
298 a, b = Tempfile.new('a'), Tempfile.new('b')
299 a.syswrite 'hello world'
301 IO::Splice.copy_stream(a, b)
303 assert_equal 'hello world', b.read
306 def test_splice_copy_stream_file_to_file_big
307 buf = ('ab' * IO::Splice::PIPE_CAPA) + 'hi'
308 a, b = Tempfile.new('a'), Tempfile.new('b')
311 IO::Splice.copy_stream(a, b)
313 assert_equal buf, b.read
316 def test_splice_copy_stream_file_to_file_big_partial
317 nr = IO::Splice::PIPE_CAPA
318 buf = ('ab' * nr) + 'hi'
319 a, b = Tempfile.new('a'), Tempfile.new('b')
322 assert_equal nr, IO::Splice.copy_stream(a, b, nr)
324 assert_equal('ab' * (nr/2), b.read)
327 def test_splice_copy_stream_file_to_file_len
328 a, b = Tempfile.new('a'), Tempfile.new('b')
329 a.syswrite 'hello world'
331 IO::Splice.copy_stream(a, b, 5)
333 assert_equal 'hello', b.read
336 def test_splice_copy_stream_pipe_to_file_len
337 a = Tempfile.new('a')
339 w.syswrite 'hello world'
340 IO::Splice.copy_stream(r, a, 5)
342 assert_equal 'hello', a.read
345 def test_splice_copy_stream_paths
346 a = Tempfile.new('a')
347 b = Tempfile.new('a')
348 a.syswrite('hello world')
349 IO::Splice.copy_stream(a.path, b.path, 5)
350 assert_equal 'hello', b.read
353 def test_splice_copy_stream_src_offset
354 a = Tempfile.new('a')
355 b = Tempfile.new('a')
356 a.syswrite('hello world')
357 IO::Splice.copy_stream(a.path, b.path, 5, 6)
358 assert_equal 'world', b.read
361 def test_copy_stream_nonblock_src
362 server = TCPServer.new('127.0.0.1', 0)
363 port = server.addr[1]
365 rs = TCPSocket.new('127.0.0.1', port)
368 assert_raises(Timeout::Error) do
369 timeout(0.05) { nr += IO::Splice.copy_stream(rs, wp, 5) }
376 def test_copy_stream_nonblock_dst
377 server = TCPServer.new('127.0.0.1', 0)
378 port = server.addr[1]
380 rs = TCPSocket.new('127.0.0.1', port)
382 client = server.accept
383 buf = ' ' * IO::Splice::PIPE_CAPA
385 assert_raises(Timeout::Error) do
388 wp.write_nonblock(buf)
392 nr += IO::Splice.copy_stream(rp, rs, IO::Splice::PIPE_CAPA)
396 assert_equal nr, client.read(nr).size
401 def test_copy_stream_eof
403 w.syswrite 'hello world'
405 a = Tempfile.new('a')
406 assert_equal 11, IO::Splice.copy_stream(r, a)
408 assert_equal 'hello world', a.read
413 assert_kind_of Integer, r.pipe_size
414 assert(r.pipe_size >= 512)
415 assert_nothing_raised { w.pipe_size = 8192 }
416 assert_equal 8192, r.pipe_size
419 assert_raises(Errno::EBUSY) { r.pipe_size = 4096 }
421 pipe_max_size = File.read("/proc/sys/fs/pipe-max-size").to_i
422 assert_nothing_raised { r.pipe_size = pipe_max_size }
423 assert_raises(Errno::EPERM) { r.pipe_size = pipe_max_size * 2 }
424 end if IO.method_defined?(:pipe_size)