test: enable Ruby warnings
[ruby_io_splice.git] / test / test_io_splice.rb
blob142586a26be8c36b9f97e012c7d93de05182b826
1 # -*- encoding: binary -*-
2 require 'test/unit'
3 require 'tempfile'
4 $-w = true
5 require 'io/splice'
7 class Test_IO_Splice < Test::Unit::TestCase
9   def test_splice
10     str = 'abcde'
11     size = 5
12     rd, wr = IO.pipe
13     tmp = Tempfile.new('ruby_io_splice')
15     assert_nothing_raised {
16       tmp.syswrite(str)
17       tmp.sysseek(0)
18     }
20     nr = IO.splice(tmp.fileno, nil, wr.fileno, nil, size, 0)
21     assert_equal size, nr
22     assert_equal str, rd.sysread(size)
23   end
25   def test_splice_in_offset
26     str = 'abcde'
27     off = 3
28     len = 2
29     rd, wr = IO.pipe
30     tmp = Tempfile.new('ruby_io_splice')
32     assert_nothing_raised {
33       tmp.syswrite(str)
34       tmp.sysseek(0)
35     }
37     nr = IO.splice(tmp.fileno, off, wr.fileno, nil, len, 0)
38     assert_equal len, nr
39     assert_equal 'de', rd.sysread(len)
40   end
42   def test_splice_out_offset
43     str = 'abcde'
44     rd, wr = IO.pipe
45     tmp = Tempfile.new('ruby_io_splice')
47     assert_nothing_raised { wr.syswrite(str) }
48     nr = IO.splice(rd.fileno, nil, tmp.fileno, 3, str.size, 0)
49     assert_equal 5, nr
50     assert_nothing_raised { tmp.sysseek(0) }
51     assert_equal "\0\0\0abcde", tmp.sysread(9)
52   end
54   def test_splice_nonblock
55     rd, wr = IO.pipe
56     tmp = Tempfile.new('ruby_io_splice')
58     assert_raises(Errno::EAGAIN) {
59       IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
60     }
61   end
63   def test_splice_eof
64     rd, wr = IO.pipe
65     tmp = Tempfile.new('ruby_io_splice')
66     wr.syswrite 'abc'
67     wr.close
69     nr = IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
70     assert_equal 3, nr
71     assert_raises(EOFError) {
72       IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
73     }
74   end
76   def test_tee
77     str = 'abcde'
78     size = 5
79     rda, wra = IO.pipe
80     rdb, wrb = IO.pipe
82     assert_nothing_raised { wra.syswrite(str) }
83     nr = IO.tee(rda.fileno, wrb.fileno, size, 0)
84     assert_equal 5, nr
85     assert_equal str, rdb.sysread(5)
86     assert_equal str, rda.sysread(5)
87   end
89   def test_tee_eof
90     rda, wra = IO.pipe
91     rdb, wrb = IO.pipe
92     wra.close
93     assert_raises(EOFError) { IO.tee(rda.fileno, wrb.fileno, 4096, 0) }
94   end
96   def test_tee_nonblock
97     rda, wra = IO.pipe
98     rdb, wrb = IO.pipe
99     assert_raises(Errno::EAGAIN) {
100       IO.tee(rda.fileno, wrb.fileno, 4096, IO::Splice::F_NONBLOCK)
101     }
102   end
104   def test_vmsplice_array
105     data = %w(hello world how are you today)
106     r, w = IO.pipe
107     n = IO.vmsplice(w.fileno, data, 0)
108     assert_equal data.join('').size, n
109     assert_equal data.join(''), r.readpartial(16384)
110   end
112   def test_vmsplice_nonblock
113     data = %w(hello world how are you today)
114     r, w = IO.pipe
115     w.syswrite('.' * IO::Splice::PIPE_CAPA)
116     assert_raises(Errno::EAGAIN) {
117       IO.vmsplice(w.fileno, data, IO::Splice::F_NONBLOCK)
118     }
119   end
121   def test_vmsplice_in_full
122     empty = ""
124     # bs * count should be > PIPE_BUF
125     [ [ 512, 512 ], [ 131073, 3 ], [ 4098, 64 ] ].each do |(bs,count)|
126       rd, wr = IO.pipe
127       buf = File.open('/dev/urandom', 'rb') { |fp| fp.sysread(bs) }
129       vec = (1..count).map { buf }
130       pid = fork do
131         wr.close
132         tmp = []
133         begin
134           sleep 0.005
135           tmp << rd.readpartial(8192, buf)
136         rescue EOFError
137           break
138         end while true
139         ok = (vec.join(empty) == tmp.join(empty))
140         exit! ok
141       end
142       assert_nothing_raised { rd.close }
143       assert_equal(bs * count, IO.vmsplice(wr.fileno, vec, 0))
144       assert_nothing_raised { wr.close }
145       _, status = Process.waitpid2(pid)
146       assert status.success?
147     end
148   end
150   def test_constants
151     assert IO::Splice::PIPE_BUF > 0
152     %w(move nonblock more gift).each { |x|
153       assert Integer === IO::Splice.const_get("F_#{x.upcase}")
154     }
155     assert IO::Splice::PIPE_CAPA >= IO::Splice::PIPE_BUF
156   end