trysplice implies SPLICE_F_NONBLOCK for flags
[ruby_io_splice.git] / test / test_io_splice.rb
blobf5bce6a10fb995f32c164b18496639c543e10530
1 # -*- encoding: binary -*-
2 require 'test/unit'
3 require 'tempfile'
4 require 'socket'
5 require 'io/nonblock'
6 require 'timeout'
7 $-w = true
8 require 'io/splice'
10 class Test_IO_Splice < Test::Unit::TestCase
12   def test_splice
13     str = 'abcde'
14     size = 5
15     rd, wr = IO.pipe
16     tmp = Tempfile.new('ruby_io_splice')
18     assert_nothing_raised {
19       tmp.syswrite(str)
20       tmp.sysseek(0)
21     }
23     nr = IO.splice(tmp.fileno, nil, wr.fileno, nil, size, 0)
24     assert_equal size, nr
25     assert_equal str, rd.sysread(size)
26   end
28   def test_splice_io
29     str = 'abcde'
30     size = 5
31     rd, wr = IO.pipe
32     tmp = Tempfile.new('ruby_io_splice')
34     assert_nothing_raised {
35       tmp.syswrite(str)
36       tmp.sysseek(0)
37     }
39     nr = IO.splice(tmp, nil, wr, nil, size, 0)
40     assert_equal size, nr
41     assert_equal str, rd.sysread(size)
42   end
44   def test_splice_io_noflags
45     str = 'abcde'
46     size = 5
47     rd, wr = IO.pipe
48     tmp = Tempfile.new('ruby_io_splice')
50     assert_nothing_raised {
51       tmp.syswrite(str)
52       tmp.sysseek(0)
53     }
55     nr = IO.splice(tmp, nil, wr, nil, size)
56     assert_equal size, nr
57     assert_equal str, rd.sysread(size)
58   end
60   def test_trysplice_io_noflags
61     str = 'abcde'
62     size = 5
63     rd, wr = IO.pipe
64     tmp = Tempfile.new('ruby_io_splice')
66     assert_nothing_raised {
67       tmp.syswrite(str)
68       tmp.sysseek(0)
69     }
71     nr = IO.trysplice(tmp, nil, wr, nil, size)
72     assert_equal size, nr
73     assert_equal str, rd.sysread(size)
74   end
76   def test_splice_io_ish
77     str = 'abcde'
78     size = 5
79     rd, wr = IO.pipe
80     tmp = Tempfile.new('ruby_io_splice')
81     io_ish = [ tmp ]
82     def io_ish.to_io
83       first.to_io
84     end
86     assert_nothing_raised {
87       tmp.syswrite(str)
88       tmp.sysseek(0)
89     }
91     nr = IO.splice(io_ish, nil, wr, nil, size, 0)
92     assert_equal size, nr
93     assert_equal str, rd.sysread(size)
94   end
96   def test_splice_in_offset
97     str = 'abcde'
98     off = 3
99     len = 2
100     rd, wr = IO.pipe
101     tmp = Tempfile.new('ruby_io_splice')
103     assert_nothing_raised {
104       tmp.syswrite(str)
105       tmp.sysseek(0)
106     }
108     nr = IO.splice(tmp.fileno, off, wr.fileno, nil, len, 0)
109     assert_equal len, nr
110     assert_equal 'de', rd.sysread(len)
111   end
113   def test_splice_out_offset
114     str = 'abcde'
115     rd, wr = IO.pipe
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)
120     assert_equal 5, nr
121     assert_nothing_raised { tmp.sysseek(0) }
122     assert_equal "\0\0\0abcde", tmp.sysread(9)
123   end
125   def test_splice_nonblock
126     rd, wr = IO.pipe
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)
131     }
132   end
134   def test_trysplice_nonblock
135     rd, wr = IO.pipe
136     tmp = Tempfile.new('ruby_io_splice')
137     assert_equal :EAGAIN,
138            IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_NONBLOCK)
139   end
141   def test_trysplice_nonblock_noargs
142     rd, wr = IO.pipe
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)
146   end
148   def test_splice_eof
149     rd, wr = IO.pipe
150     tmp = Tempfile.new('ruby_io_splice')
151     wr.syswrite 'abc'
152     wr.close
154     nr = IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
155     assert_equal 3, nr
156     assert_raises(EOFError) {
157       IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
158     }
159   end
161   def test_trysplice_eof
162     rd, wr = IO.pipe
163     tmp = Tempfile.new('ruby_io_splice')
164     wr.syswrite 'abc'
165     wr.close
167     nr = IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_NONBLOCK)
168     assert_equal 3, nr
169     assert_nil IO.trysplice(rd, nil, tmp, 0, 5, IO::Splice::F_NONBLOCK)
170   end
172   def test_splice_nonblock_socket
173     server = TCPServer.new('127.0.0.1', 0)
174     port = server.addr[1]
175     rp, wp = IO.pipe
176     rs = TCPSocket.new('127.0.0.1', port)
177     rs.nonblock = true
178     assert_raises(Errno::EAGAIN) { IO.splice(rs, nil, wp, nil, 1024, 0) }
179     rs.close
180     server.close
181   end
183   def test_tee
184     str = 'abcde'
185     size = 5
186     rda, wra = IO.pipe
187     rdb, wrb = IO.pipe
189     assert_nothing_raised { wra.syswrite(str) }
190     nr = IO.tee(rda.fileno, wrb.fileno, size, 0)
191     assert_equal 5, nr
192     assert_equal str, rdb.sysread(5)
193     assert_equal str, rda.sysread(5)
194   end
196   def test_tee_eof
197     rda, wra = IO.pipe
198     rdb, wrb = IO.pipe
199     wra.close
200     assert_raises(EOFError) { IO.tee(rda.fileno, wrb.fileno, 4096, 0) }
201   end
203   def test_tee_nonblock
204     rda, wra = IO.pipe
205     rdb, wrb = IO.pipe
206     assert_raises(Errno::EAGAIN) {
207       IO.tee(rda.fileno, wrb.fileno, 4096, IO::Splice::F_NONBLOCK)
208     }
209   end
211   def test_tee_io
212     str = 'abcde'
213     size = 5
214     rda, wra = IO.pipe
215     rdb, wrb = IO.pipe
217     assert_nothing_raised { wra.syswrite(str) }
218     nr = IO.tee(rda, wrb, size, 0)
219     assert_equal 5, nr
220     assert_equal str, rdb.sysread(5)
221     assert_equal str, rda.sysread(5)
222   end
224   def test_vmsplice_array
225     data = %w(hello world how are you today)
226     r, w = IO.pipe
227     n = IO.vmsplice(w.fileno, data, 0)
228     assert_equal data.join('').size, n
229     assert_equal data.join(''), r.readpartial(16384)
230   end
232   def test_vmsplice_string
233     r, w = IO.pipe
234     assert_equal 5, IO.vmsplice(w, 'hello', 0)
235     assert_equal 'hello', r.read(5)
236   end
238   def test_vmsplice_array_io
239     data = %w(hello world how are you today)
240     r, w = IO.pipe
241     n = IO.vmsplice(w, data, 0)
242     assert_equal data.join('').size, n
243     assert_equal data.join(''), r.readpartial(16384)
244   end
246   def test_vmsplice_nonblock
247     data = %w(hello world how are you today)
248     r, w = IO.pipe
249     w.syswrite('.' * IO::Splice::PIPE_CAPA)
250     assert_raises(Errno::EAGAIN) {
251       IO.vmsplice(w.fileno, data, IO::Splice::F_NONBLOCK)
252     }
253   end
255   def test_vmsplice_in_full
256     empty = ""
258     # bs * count should be > PIPE_BUF
259     [ [ 512, 512 ], [ 131073, 3 ], [ 4098, 64 ] ].each do |(bs,count)|
260       rd, wr = IO.pipe
261       buf = File.open('/dev/urandom', 'rb') { |fp| fp.sysread(bs) }
263       vec = (1..count).map { buf }
264       pid = fork do
265         wr.close
266         tmp = []
267         begin
268           sleep 0.005
269           tmp << rd.readpartial(8192)
270         rescue EOFError
271           break
272         end while true
273         ok = (vec.join(empty) == tmp.join(empty))
274         exit! ok
275       end
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?
281     end
282   end
284   def test_vmsplice_nil
285     data = %w(hello world how are you today)
286     assert_raises(TypeError) { IO.vmsplice(nil, data, 0) }
287   end
289   def test_constants
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}")
293     }
294     assert IO::Splice::PIPE_CAPA >= IO::Splice::PIPE_BUF
295   end
297   def test_splice_copy_stream_file_to_file_small
298     a, b = Tempfile.new('a'), Tempfile.new('b')
299     a.syswrite 'hello world'
300     a.sysseek(0)
301     IO::Splice.copy_stream(a, b)
302     b.rewind
303     assert_equal 'hello world', b.read
304   end
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')
309     a.syswrite buf
310     a.sysseek(0)
311     IO::Splice.copy_stream(a, b)
312     b.rewind
313     assert_equal buf, b.read
314   end
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')
320     a.syswrite buf
321     a.sysseek(0)
322     assert_equal nr, IO::Splice.copy_stream(a, b, nr)
323     b.rewind
324     assert_equal('ab' * (nr/2), b.read)
325   end
327   def test_splice_copy_stream_file_to_file_len
328     a, b = Tempfile.new('a'), Tempfile.new('b')
329     a.syswrite 'hello world'
330     a.sysseek(0)
331     IO::Splice.copy_stream(a, b, 5)
332     b.rewind
333     assert_equal 'hello', b.read
334   end
336   def test_splice_copy_stream_pipe_to_file_len
337     a = Tempfile.new('a')
338     r, w = IO.pipe
339     w.syswrite 'hello world'
340     IO::Splice.copy_stream(r, a, 5)
341     a.rewind
342     assert_equal 'hello', a.read
343   end
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
351   end
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
359   end
361   def test_copy_stream_nonblock_src
362     server = TCPServer.new('127.0.0.1', 0)
363     port = server.addr[1]
364     rp, wp = IO.pipe
365     rs = TCPSocket.new('127.0.0.1', port)
366     rs.nonblock = true
367     nr = 0
368     assert_raises(Timeout::Error) do
369       timeout(0.05) { nr += IO::Splice.copy_stream(rs, wp, 5) }
370     end
371     assert_equal 0, nr
372     rs.close
373     server.close
374   end
376   def test_copy_stream_nonblock_dst
377     server = TCPServer.new('127.0.0.1', 0)
378     port = server.addr[1]
379     rp, wp = IO.pipe
380     rs = TCPSocket.new('127.0.0.1', port)
381     rs.nonblock = true
382     client = server.accept
383     buf = ' ' * IO::Splice::PIPE_CAPA
384     nr = 0
385     assert_raises(Timeout::Error) do
386       loop do
387         begin
388           wp.write_nonblock(buf)
389         rescue Errno::EAGAIN
390         end
391         timeout(0.05) do
392           nr += IO::Splice.copy_stream(rp, rs, IO::Splice::PIPE_CAPA)
393         end
394       end
395     end
396     assert_equal nr, client.read(nr).size
397     rs.close
398     server.close
399   end
401   def test_copy_stream_eof
402     r, w = IO.pipe
403     w.syswrite 'hello world'
404     w.close
405     a = Tempfile.new('a')
406     assert_equal 11, IO::Splice.copy_stream(r, a)
407     a.rewind
408     assert_equal 'hello world', a.read
409   end
411   def test_pipe_size
412     r, w = IO.pipe
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
418     w.write('*' * 4097)
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)