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