connect_timeout: match :timeout if unset
[ruby-mogilefs-client.git] / test / test_mogilefs_integration_large_pipe.rb
blobf6b7dc925eb5c91e0a7d991bd9073e3acec58491
1 # -*- encoding: binary -*-
2 require './test/fresh'
3 require "digest/sha1"
5 class TestMogileFSLargePipe < Test::Unit::TestCase
6   include TestFreshSetup
7   def setup
8     setup_mogilefs
9     add_host_device_domain
10     @client = MogileFS::MogileFS.new(:hosts => @trackers, :domain => @domain)
11   end
13   alias teardown teardown_mogilefs
15   def test_large_pipe_test
16     junk = File.open("/dev/urandom") { |fp| fp.read(1024) }
17     junk *= 32
18     nr = rand(666) + 1024
19     r, w = IO.pipe
20     sha1 = Digest::SHA1.new
21     th = Thread.new do
22       nr.times do
23         sha1.update(junk)
24         w.write(junk)
25       end
26       w.close
27     end
28     assert_equal(nr * junk.size, @client.store_file("a", nil, r))
29     r.close
30     th.join
31     @client.get_file_data("a") do |rd|
32       assert_equal(nr * junk.size, @client.store_file("b", nil, rd))
33     end
34     a = Thread.new { @client.get_file_data("a") { |rd| sha1read(rd) } }
35     b = Thread.new { @client.get_file_data("b") { |rd| sha1read(rd) } }
36     a = a.value
37     b = b.value
38     assert_equal a, b
39     assert_equal sha1, a
41     # We should be able to open FIFOs
42     tmp = tmpfile("fifo")
43     tmp_path = tmp.path
44     File.unlink(tmp_path)
45     x!("mkfifo", tmp_path)
46     pid = fork do
47       File.open(tmp_path, "wb") do |wr|
48         nr.times { wr.write(junk) }
49       end
50     end
51     assert_equal(nr * junk.size, @client.store_file("fifo", nil, tmp_path))
52     _, status = Process.waitpid2(pid)
53     assert status.success?, status.inspect
54     fifo_sha1 = @client.get_file_data("fifo") { |rd| sha1read(rd) }
55     assert_equal sha1, fifo_sha1
56   end
58   def sha1read(rd)
59     buf = ""
60     d = Digest::SHA1.new
61     while rd.read(16384, buf)
62       d << buf
63     end
64     d
65   end
66 end