1 # -*- encoding: binary -*-
10 class Test_IO_Splice < Test::Unit::TestCase
13 (defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby") || !defined?(RUBY_ENGINE)
20 tmp = Tempfile.new('ruby_io_splice')
22 assert_nothing_raised {
27 nr = IO.splice(tmp.fileno, nil, wr.fileno, nil, size, 0)
29 assert_equal str, rd.sysread(size)
36 tmp = Tempfile.new('ruby_io_splice')
38 assert_nothing_raised {
43 nr = IO.splice(tmp, nil, wr, nil, size, 0)
45 assert_equal str, rd.sysread(size)
48 def test_splice_io_noflags
52 tmp = Tempfile.new('ruby_io_splice')
54 assert_nothing_raised {
59 nr = IO.splice(tmp, nil, wr, nil, size)
61 assert_equal str, rd.sysread(size)
64 def test_trysplice_io_noflags
68 tmp = Tempfile.new('ruby_io_splice')
70 assert_nothing_raised {
75 nr = IO.trysplice(tmp, nil, wr, nil, size)
77 assert_equal str, rd.sysread(size)
80 def test_splice_io_ish
84 tmp = Tempfile.new('ruby_io_splice')
90 assert_nothing_raised {
95 nr = IO.splice(io_ish, nil, wr, nil, size, 0)
97 assert_equal str, rd.sysread(size)
100 def test_splice_in_offset
105 tmp = Tempfile.new('ruby_io_splice')
107 assert_nothing_raised {
112 nr = IO.splice(tmp.fileno, off, wr.fileno, nil, len, 0)
114 assert_equal 'de', rd.sysread(len)
117 def test_splice_out_offset
120 tmp = Tempfile.new('ruby_io_splice')
122 assert_nothing_raised { wr.syswrite(str) }
123 nr = IO.splice(rd.fileno, nil, tmp.fileno, 3, str.size, 0)
125 assert_nothing_raised { tmp.sysseek(0) }
126 assert_equal "\0\0\0abcde", tmp.sysread(9)
129 def test_splice_nonblock
131 tmp = Tempfile.new('ruby_io_splice')
133 assert_raises(Errno::EAGAIN) {
134 IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
138 def test_trysplice_nonblock
140 tmp = Tempfile.new('ruby_io_splice')
141 assert_equal :EAGAIN,
142 IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_NONBLOCK)
145 def test_trysplice_nonblock_noargs
147 tmp = Tempfile.new('ruby_io_splice')
148 assert_equal :EAGAIN, IO.trysplice(rd, nil, tmp, 0, 5)
149 assert_equal :EAGAIN, IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_MORE)
154 tmp = Tempfile.new('ruby_io_splice')
158 nr = IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
160 assert_raises(EOFError) {
161 IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
165 def test_trysplice_eof
167 tmp = Tempfile.new('ruby_io_splice')
171 nr = IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_NONBLOCK)
173 assert_nil IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_NONBLOCK)
176 def test_splice_nonblock_socket
177 server = TCPServer.new('127.0.0.1', 0)
178 port = server.addr[1]
180 rs = TCPSocket.new('127.0.0.1', port)
182 assert_raises(Errno::EAGAIN) { IO.splice(rs, nil, wp, nil, 1024, 0) }
193 assert_nothing_raised { wra.syswrite(str) }
194 nr = IO.tee(rda.fileno, wrb.fileno, size, 0)
196 assert_equal str, rdb.sysread(5)
197 assert_equal str, rda.sysread(5)
206 assert_nothing_raised { wra.syswrite(str) }
207 nr = IO.trytee(rda, wrb, size, 0)
209 assert_equal str, rdb.sysread(5)
210 assert_equal str, rda.sysread(5)
217 assert_raises(EOFError) { IO.tee(rda.fileno, wrb.fileno, 4096, 0) }
224 assert_nil IO.trytee(rda, wrb, 4096)
227 def test_tee_nonblock
230 assert_raises(Errno::EAGAIN) {
231 IO.tee(rda.fileno, wrb.fileno, 4096, IO::Splice::F_NONBLOCK)
235 def test_trytee_nonblock
238 assert_equal :EAGAIN, IO.trytee(rda, wrb, 4096)
247 assert_nothing_raised { wra.syswrite(str) }
248 nr = IO.tee(rda, wrb, size, 0)
250 assert_equal str, rdb.sysread(5)
251 assert_equal str, rda.sysread(5)
254 def test_vmsplice_array
255 data = %w(hello world how are you today)
257 n = IO.vmsplice(w.fileno, data, 0)
258 assert_equal data.join('').size, n
259 assert_equal data.join(''), r.readpartial(16384)
262 def test_vmsplice_noflags
263 data = %w(hello world how are you today)
265 n = IO.vmsplice(w, data)
266 assert_equal data.join('').size, n
267 assert_equal data.join(''), r.readpartial(16384)
270 def test_vmsplice_string
272 assert_equal 5, IO.vmsplice(w, 'hello', 0)
273 assert_equal 'hello', r.read(5)
276 def test_vmsplice_array_io
277 data = %w(hello world how are you today)
279 n = IO.vmsplice(w, data, 0)
280 assert_equal data.join('').size, n
281 assert_equal data.join(''), r.readpartial(16384)
284 def test_vmsplice_nonblock
285 data = %w(hello world how are you today)
287 w.syswrite('.' * IO::Splice::PIPE_CAPA)
288 assert_raises(Errno::EAGAIN) {
289 IO.vmsplice(w.fileno, data, IO::Splice::F_NONBLOCK)
293 def test_vmsplice_in_full
296 # bs * count should be > PIPE_BUF
297 [ [ 512, 512 ], [ 131073, 3 ], [ 4098, 64 ] ].each do |(bs,count)|
299 buf = File.open('/dev/urandom', 'rb') { |fp| fp.sysread(bs) }
301 vec = (1..count).map { buf }
307 tmp << rd.readpartial(8192)
311 ok = (vec.join(empty) == tmp.join(empty))
314 assert_nothing_raised { rd.close }
315 assert_equal(bs * count, IO.vmsplice(wr.fileno, vec, 0))
316 assert_nothing_raised { wr.close }
317 _, status = Process.waitpid2(pid)
318 assert status.success?
322 def test_vmsplice_nil
323 data = %w(hello world how are you today)
324 assert_raises(TypeError) { IO.vmsplice(nil, data, 0) }
328 assert IO::Splice::PIPE_BUF > 0
329 %w(move nonblock more gift).each { |x|
330 assert Integer === IO::Splice.const_get("F_#{x.upcase}")
332 assert IO::Splice::PIPE_CAPA >= IO::Splice::PIPE_BUF
337 assert_kind_of Integer, r.pipe_size
338 assert(r.pipe_size >= 512)
339 assert_nothing_raised { w.pipe_size = 8192 }
340 assert_equal 8192, r.pipe_size
343 assert_raises(Errno::EBUSY) { r.pipe_size = 4096 }
345 pipe_max_size = File.read("/proc/sys/fs/pipe-max-size").to_i
346 assert_nothing_raised { r.pipe_size = pipe_max_size }
347 assert_raises(Errno::EPERM) { r.pipe_size = pipe_max_size * 2 }
348 end if IO.method_defined?(:pipe_size)