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
239 File.open("src", "w") {|f| f << content }
241 with_socketpair {|s1, s2|
242 ret = IO::Splice.copy_stream("src", s1)
243 assert_equal(content.bytesize, ret)
245 assert_equal(content, s2.read)
248 bigcontent = "abc" * 123456
249 File.open("bigsrc", "w") {|f| f << bigcontent }
251 with_socketpair {|s1, s2|
252 t = Thread.new { s2.read }
253 ret = IO::Splice.copy_stream("bigsrc", s1)
254 assert_equal(bigcontent.bytesize, ret)
257 assert_equal(bigcontent, result)
260 with_socketpair {|s1, s2|
261 t = Thread.new { s2.read }
262 ret = IO::Splice.copy_stream("bigsrc", s1, 10000)
263 assert_equal(10000, ret)
266 assert_equal(bigcontent[0,10000], result)
269 File.open("bigsrc") {|f|
270 assert_equal(0, f.pos)
271 with_socketpair {|s1, s2|
272 t = Thread.new { s2.read }
273 ret = IO::Splice.copy_stream(f, s1, nil, 100)
274 assert_equal(bigcontent.bytesize-100, ret)
275 assert_equal(0, f.pos)
278 assert_equal(bigcontent[100..-1], result)
282 File.open("bigsrc") {|f|
283 assert_equal(bigcontent[0,100], f.read(100))
284 assert_equal(100, f.pos)
285 with_socketpair {|s1, s2|
286 t = Thread.new { s2.read }
287 ret = IO::Splice.copy_stream(f, s1)
288 assert_equal(bigcontent.bytesize-100, ret)
289 assert_equal(bigcontent.length, f.pos)
292 assert_equal(bigcontent[100..-1], result)
296 megacontent = "abc" * 1234567
297 File.open("megasrc", "w") {|f| f << megacontent }
300 with_socketpair {|s1, s2|
304 skip "nonblocking IO for pipe is not implemented"
306 t = Thread.new { s2.read }
307 ret = IO::Splice.copy_stream("megasrc", s1)
308 assert_equal(megacontent.bytesize, ret)
311 assert_equal(megacontent, result)
313 with_socketpair {|s1, s2|
317 skip "nonblocking IO for pipe is not implemented"
324 Process.kill(:USR1, Process.ppid)
329 assert_equal megacontent.bytesize,
330 IO::Splice.copy_stream("megasrc", s1)
332 assert_equal(1, @usr1_rcvd)
334 _, status = Process.waitpid2(pid)
335 assert status.success?, status.inspect