tests: create fresh intances for all integration tests
[ruby-mogilefs-client.git] / test / test_mogilefs_integration_large_pipe.rb
blob16f6d58f901dcb338a4950f251153b65ea572a5e
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   def test_large_pipe_test
14     junk = File.open("/dev/urandom") { |fp| fp.read(1024) }
15     junk *= 32
16     nr = rand(666) + 1024
17     r, w = IO.pipe
18     sha1 = Digest::SHA1.new
19     th = Thread.new do
20       nr.times do
21         sha1.update(junk)
22         w.write(junk)
23       end
24       w.close
25     end
26     assert_equal(nr * junk.size, @client.store_file("a", nil, r))
27     r.close
28     th.join
29     @client.get_file_data("a") do |rd|
30       assert_equal(nr * junk.size, @client.store_file("b", nil, rd))
31     end
32     a = Thread.new { @client.get_file_data("a") { |rd| sha1read(rd) } }
33     b = Thread.new { @client.get_file_data("b") { |rd| sha1read(rd) } }
34     a = a.value
35     b = b.value
36     assert_equal a, b
37     assert_equal sha1, a
39     # We should be able to open FIFOs
40     tmp = tmpfile("fifo")
41     tmp_path = tmp.path
42     File.unlink(tmp_path)
43     x!("mkfifo", tmp_path)
44     pid = fork do
45       File.open(tmp_path, "wb") do |wr|
46         nr.times { wr.write(junk) }
47       end
48     end
49     assert_equal(nr * junk.size, @client.store_file("fifo", nil, tmp_path))
50     _, status = Process.waitpid2(pid)
51     assert status.success?, status.inspect
52     fifo_sha1 = @client.get_file_data("fifo") { |rd| sha1read(rd) }
53     assert_equal sha1, fifo_sha1
54   end
56   def sha1read(rd)
57     buf = ""
58     d = Digest::SHA1.new
59     while rd.read(16384, buf)
60       d << buf
61     end
62     d
63   end
64 end