1 # -*- encoding: binary -*-
10 # unused_port provides an unused port on +addr+ usable for TCP that is
11 # guaranteed to be unused across all unicorn builds on that system. It
12 # prevents race conditions by using a lock file other unicorn builds
13 # will see. This is required if you perform several builds in parallel
14 # with a continuous integration system or run tests in parallel via
15 # gmake. This is NOT guaranteed to be race-free if you run other
16 # processes that bind to random ports for testing (but the window
17 # for a race condition is very small).
18 def unused_port(addr = '127.0.0.1')
23 sock = TCPServer.new(addr, 0)
26 # since we'll end up closing the random port we just got, there's a race
27 # condition could allow the random port we just chose to reselect itself
28 # when running tests in parallel with gmake. Create a lock file while
29 # we have the port here to ensure that does not happen .
30 lock_path = "#{Dir::tmpdir}/unicorn_test.#{addr}:#{port}.lock"
31 File.open(lock_path, File::WRONLY|File::CREAT|File::EXCL, 0600).close
32 at_exit { File.unlink(lock_path) rescue nil }
41 class Test_IO_Splice < Test::Unit::TestCase
47 tmp = Tempfile.new('ruby_io_splice')
49 assert_nothing_raised {
54 nr = IO.splice(tmp.fileno, nil, wr.fileno, nil, size, 0)
56 assert_equal str, rd.sysread(size)
63 tmp = Tempfile.new('ruby_io_splice')
65 assert_nothing_raised {
70 nr = IO.splice(tmp, nil, wr, nil, size, 0)
72 assert_equal str, rd.sysread(size)
75 def test_splice_io_ish
79 tmp = Tempfile.new('ruby_io_splice')
85 assert_nothing_raised {
90 nr = IO.splice(io_ish, nil, wr, nil, size, 0)
92 assert_equal str, rd.sysread(size)
95 def test_splice_in_offset
100 tmp = Tempfile.new('ruby_io_splice')
102 assert_nothing_raised {
107 nr = IO.splice(tmp.fileno, off, wr.fileno, nil, len, 0)
109 assert_equal 'de', rd.sysread(len)
112 def test_splice_out_offset
115 tmp = Tempfile.new('ruby_io_splice')
117 assert_nothing_raised { wr.syswrite(str) }
118 nr = IO.splice(rd.fileno, nil, tmp.fileno, 3, str.size, 0)
120 assert_nothing_raised { tmp.sysseek(0) }
121 assert_equal "\0\0\0abcde", tmp.sysread(9)
124 def test_splice_nonblock
126 tmp = Tempfile.new('ruby_io_splice')
128 assert_raises(Errno::EAGAIN) {
129 IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
135 tmp = Tempfile.new('ruby_io_splice')
139 nr = IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
141 assert_raises(EOFError) {
142 IO.splice(rd.fileno, nil, tmp.fileno, 0, 5, IO::Splice::F_NONBLOCK)
146 def test_splice_nonblock_socket
148 server = TCPServer.new('127.0.0.1', port)
150 rs = TCPSocket.new('127.0.0.1', port)
152 assert_raises(Errno::EAGAIN) { IO.splice(rs, nil, wp, nil, 1024, 0) }
163 assert_nothing_raised { wra.syswrite(str) }
164 nr = IO.tee(rda.fileno, wrb.fileno, size, 0)
166 assert_equal str, rdb.sysread(5)
167 assert_equal str, rda.sysread(5)
174 assert_raises(EOFError) { IO.tee(rda.fileno, wrb.fileno, 4096, 0) }
177 def test_tee_nonblock
180 assert_raises(Errno::EAGAIN) {
181 IO.tee(rda.fileno, wrb.fileno, 4096, IO::Splice::F_NONBLOCK)
191 assert_nothing_raised { wra.syswrite(str) }
192 nr = IO.tee(rda, wrb, size, 0)
194 assert_equal str, rdb.sysread(5)
195 assert_equal str, rda.sysread(5)
198 def test_vmsplice_array
199 data = %w(hello world how are you today)
201 n = IO.vmsplice(w.fileno, data, 0)
202 assert_equal data.join('').size, n
203 assert_equal data.join(''), r.readpartial(16384)
206 def test_vmsplice_string
208 assert_equal 5, IO.vmsplice(w, 'hello', 0)
209 assert_equal 'hello', r.read(5)
212 def test_vmsplice_array_io
213 data = %w(hello world how are you today)
215 n = IO.vmsplice(w, data, 0)
216 assert_equal data.join('').size, n
217 assert_equal data.join(''), r.readpartial(16384)
220 def test_vmsplice_nonblock
221 data = %w(hello world how are you today)
223 w.syswrite('.' * IO::Splice::PIPE_CAPA)
224 assert_raises(Errno::EAGAIN) {
225 IO.vmsplice(w.fileno, data, IO::Splice::F_NONBLOCK)
229 def test_vmsplice_in_full
232 # bs * count should be > PIPE_BUF
233 [ [ 512, 512 ], [ 131073, 3 ], [ 4098, 64 ] ].each do |(bs,count)|
235 buf = File.open('/dev/urandom', 'rb') { |fp| fp.sysread(bs) }
237 vec = (1..count).map { buf }
243 tmp << rd.readpartial(8192)
247 ok = (vec.join(empty) == tmp.join(empty))
250 assert_nothing_raised { rd.close }
251 assert_equal(bs * count, IO.vmsplice(wr.fileno, vec, 0))
252 assert_nothing_raised { wr.close }
253 _, status = Process.waitpid2(pid)
254 assert status.success?
258 def test_vmsplice_nil
259 data = %w(hello world how are you today)
260 assert_raises(TypeError) { IO.vmsplice(nil, data, 0) }
264 assert IO::Splice::PIPE_BUF > 0
265 %w(move nonblock more gift).each { |x|
266 assert Integer === IO::Splice.const_get("F_#{x.upcase}")
268 assert IO::Splice::PIPE_CAPA >= IO::Splice::PIPE_BUF
271 def test_splice_copy_stream_file_to_file_small
272 a, b = Tempfile.new('a'), Tempfile.new('b')
273 a.syswrite 'hello world'
275 IO::Splice.copy_stream(a, b)
277 assert_equal 'hello world', b.read
280 def test_splice_copy_stream_file_to_file_big
281 buf = ('ab' * IO::Splice::PIPE_CAPA) + 'hi'
282 a, b = Tempfile.new('a'), Tempfile.new('b')
285 IO::Splice.copy_stream(a, b)
287 assert_equal buf, b.read
290 def test_splice_copy_stream_file_to_file_big_partial
291 nr = IO::Splice::PIPE_CAPA
292 buf = ('ab' * nr) + 'hi'
293 a, b = Tempfile.new('a'), Tempfile.new('b')
296 assert_equal nr, IO::Splice.copy_stream(a, b, nr)
298 assert_equal('ab' * (nr/2), b.read)
301 def test_splice_copy_stream_file_to_file_len
302 a, b = Tempfile.new('a'), Tempfile.new('b')
303 a.syswrite 'hello world'
305 IO::Splice.copy_stream(a, b, 5)
307 assert_equal 'hello', b.read
310 def test_splice_copy_stream_pipe_to_file_len
311 a = Tempfile.new('a')
313 w.syswrite 'hello world'
314 IO::Splice.copy_stream(r, a, 5)
316 assert_equal 'hello', a.read
319 def test_splice_copy_stream_paths
320 a = Tempfile.new('a')
321 b = Tempfile.new('a')
322 a.syswrite('hello world')
323 IO::Splice.copy_stream(a.path, b.path, 5)
324 assert_equal 'hello', b.read
327 def test_splice_copy_stream_src_offset
328 a = Tempfile.new('a')
329 b = Tempfile.new('a')
330 a.syswrite('hello world')
331 IO::Splice.copy_stream(a.path, b.path, 5, 6)
332 assert_equal 'world', b.read
335 def test_copy_stream_nonblock_src
337 server = TCPServer.new('127.0.0.1', port)
339 rs = TCPSocket.new('127.0.0.1', port)
342 assert_raises(Timeout::Error) do
343 timeout(0.05) { nr += IO::Splice.copy_stream(rs, wp, 5) }
350 def test_copy_stream_nonblock_dst
352 server = TCPServer.new('127.0.0.1', port)
354 rs = TCPSocket.new('127.0.0.1', port)
356 client = server.accept
357 buf = ' ' * IO::Splice::PIPE_CAPA
359 assert_raises(Timeout::Error) do
362 wp.write_nonblock(buf)
366 nr += IO::Splice.copy_stream(rp, rs, IO::Splice::PIPE_CAPA)
370 assert_equal nr, client.read(nr).size
375 def test_copy_stream_eof
377 w.syswrite 'hello world'
379 a = Tempfile.new('a')
380 assert_equal 11, IO::Splice.copy_stream(r, a)
382 assert_equal 'hello world', a.read
387 assert Integer, r.pipe_size
388 assert(r.pipe_size >= 512)
389 assert_nothing_raised { w.pipe_size = 8192 }
390 assert 8192, r.pipe_size
393 assert_raises(Errno::EBUSY) { r.pipe_size = 4096 }
395 pipe_max_size = File.read("/proc/sys/fs/pipe-max-size").to_i
396 assert_nothing_raised { r.pipe_size = pipe_max_size }
397 assert_raises(Errno::EPERM) { r.pipe_size = pipe_max_size * 2 }
398 end if IO.method_defined?(:pipe_size)