10 class TestIOCopyStreamCompat < Test::Unit::TestCase
12 IO.method_defined?("nonblock=")
34 flunk("timeout") unless wt.join(10) && rt.join(10)
36 w.close unless !w || w.closed?
37 r.close unless !r || r.closed?
38 (wt.kill; wt.join) if wt
39 (rt.kill; rt.join) if rt
49 r.close unless r.closed?
50 w.close unless w.closed?
54 def with_read_pipe(content)
73 trap(:USR1) { @usr1_rcvd += 1 }
76 trap(:USR1, "DEFAULT")
82 File.open("src", "w") {|f| f << content }
83 ret = IO::Splice.copy_stream("src", "dst")
84 assert_equal(content.bytesize, ret)
85 assert_equal(content, File.read("dst"))
87 # overwrite by smaller file.
89 File.open("src", "w") {|f| f << content }
90 ret = IO::Splice.copy_stream("src", "dst")
91 assert_equal(content.bytesize, ret)
92 assert_equal(content, File.read("dst"))
94 ret = IO::Splice.copy_stream("src", "dst", 2)
96 assert_equal(content[0,2], File.read("dst"))
98 ret = IO::Splice.copy_stream("src", "dst", 0)
100 assert_equal("", File.read("dst"))
102 ret = IO::Splice.copy_stream("src", "dst", nil, 1)
103 assert_equal(content.bytesize-1, ret)
104 assert_equal(content[1..-1], File.read("dst"))
106 assert_raise(Errno::ENOENT) {
107 IO::Splice.copy_stream("nodir/foo", "dst")
110 assert_raise(Errno::ENOENT) {
111 IO::Splice.copy_stream("src", "nodir/bar")
115 ret = IO::Splice.copy_stream("src", w)
116 assert_equal(content.bytesize, ret)
119 assert_equal(content, r.read)
124 assert_raise(IOError) { IO::Splice.copy_stream("src", w) }
128 with_read_pipe(pipe_content) {|r|
129 ret = IO::Splice.copy_stream(r, "dst")
130 assert_equal(pipe_content.bytesize, ret)
131 assert_equal(pipe_content, File.read("dst"))
135 ret = IO::Splice.copy_stream("src", w, 1, 1)
139 assert_equal(content[1,1], r.read)
142 bigcontent = "abc" * 123456
143 File.open("bigsrc", "w") {|f| f << bigcontent }
144 ret = IO::Splice.copy_stream("bigsrc", "bigdst")
145 assert_equal(bigcontent.bytesize, ret)
146 assert_equal(bigcontent, File.read("bigdst"))
148 File.unlink("bigdst")
149 ret = IO::Splice.copy_stream("bigsrc", "bigdst", nil, 100)
150 assert_equal(bigcontent.bytesize-100, ret)
151 assert_equal(bigcontent[100..-1], File.read("bigdst"))
153 File.unlink("bigdst")
154 ret = IO::Splice.copy_stream("bigsrc", "bigdst", 30000, 100)
155 assert_equal(30000, ret)
156 assert_equal(bigcontent[100, 30000], File.read("bigdst"))
158 File.open("bigsrc") {|f|
160 assert_equal(0, f.pos)
161 ret = IO::Splice.copy_stream(f, "bigdst", nil, 10)
162 assert_equal(bigcontent.bytesize-10, ret)
163 assert_equal(bigcontent[10..-1], File.read("bigdst"))
164 assert_equal(0, f.pos)
165 ret = IO::Splice.copy_stream(f, "bigdst", 40, 30)
166 assert_equal(40, ret)
167 assert_equal(bigcontent[30, 40], File.read("bigdst"))
168 assert_equal(0, f.pos)
169 rescue NotImplementedError
170 #skip "pread(2) is not implemtented."
176 assert_raise(IOError) { IO::Splice.copy_stream("src", w) }
179 megacontent = "abc" * 1234567
180 File.open("megasrc", "w") {|f| f << megacontent }
189 skip "nonblocking IO for pipe is not implemented"
191 t1 = Thread.new { w1 << megacontent; w1.close }
192 t2 = Thread.new { r2.read }
193 ret = IO::Splice.copy_stream(r1, w2)
194 assert_equal(megacontent.bytesize, ret)
197 assert_equal(megacontent, t2.value)
204 t1 = Thread.new { w1 << megacontent; w1.close }
205 t2 = Thread.new { r2.read }
206 ret = IO::Splice.copy_stream(r1, w2)
207 assert_equal(megacontent.bytesize, ret)
210 assert_equal(megacontent, t2.value)
215 t = Thread.new { r.read }
216 ret = IO::Splice.copy_stream("megasrc", w)
217 assert_equal(megacontent.bytesize, ret)
219 assert_equal(megacontent, t.value)
225 s1, s2 = UNIXSocket.pair
229 s1.close unless s1.closed?
230 s2.close unless s2.closed?
234 def test_copy_stream_socket
238 File.open("src", "w") {|f| f << content }
240 with_socketpair {|s1, s2|
241 ret = IO::Splice.copy_stream("src", s1)
242 assert_equal(content.bytesize, ret)
244 assert_equal(content, s2.read)
247 bigcontent = "abc" * 123456
248 File.open("bigsrc", "w") {|f| f << bigcontent }
250 with_socketpair {|s1, s2|
251 t = Thread.new { s2.read }
252 ret = IO::Splice.copy_stream("bigsrc", s1)
253 assert_equal(bigcontent.bytesize, ret)
256 assert_equal(bigcontent, result)
259 with_socketpair {|s1, s2|
260 t = Thread.new { s2.read }
261 ret = IO::Splice.copy_stream("bigsrc", s1, 10000)
262 assert_equal(10000, ret)
265 assert_equal(bigcontent[0,10000], result)
268 File.open("bigsrc") {|f|
269 assert_equal(0, f.pos)
270 with_socketpair {|s1, s2|
271 t = Thread.new { s2.read }
272 ret = IO::Splice.copy_stream(f, s1, nil, 100)
273 assert_equal(bigcontent.bytesize-100, ret)
274 assert_equal(0, f.pos)
277 assert_equal(bigcontent[100..-1], result)
281 File.open("bigsrc") {|f|
282 assert_equal(bigcontent[0,100], f.sysread(100))
283 assert_equal(100, f.pos)
284 with_socketpair {|s1, s2|
285 t = Thread.new { s2.read }
286 ret = IO::Splice.copy_stream(f, s1)
287 assert_equal(bigcontent.bytesize-100, ret)
288 assert_equal(bigcontent.length, f.sysseek(0, IO::SEEK_CUR))
291 assert_equal(bigcontent[100..-1], result)
295 megacontent = "abc" * 1234567
296 File.open("megasrc", "w") {|f| f << megacontent }
299 with_socketpair {|s1, s2|
303 skip "nonblocking IO for pipe is not implemented"
305 t = Thread.new { s2.read }
306 ret = IO::Splice.copy_stream("megasrc", s1)
307 assert_equal(megacontent.bytesize, ret)
310 assert_equal(megacontent, result)
312 with_socketpair {|s1, s2|
316 skip "nonblocking IO for pipe is not implemented"
323 Process.kill(:USR1, Process.ppid)
328 assert_equal megacontent.bytesize,
329 IO::Splice.copy_stream("megasrc", s1)
331 assert_equal(1, @usr1_rcvd)
333 _, status = Process.waitpid2(pid)
334 assert status.success?, status.inspect