mogilefs-client.git  about / heads / tags
MogileFS client library for Ruby
blob f6b7dc925eb5c91e0a7d991bd9073e3acec58491 1698 bytes (raw)
$ git show HEAD:test/test_mogilefs_integration_large_pipe.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 
# -*- encoding: binary -*-
require './test/fresh'
require "digest/sha1"

class TestMogileFSLargePipe < Test::Unit::TestCase
  include TestFreshSetup
  def setup
    setup_mogilefs
    add_host_device_domain
    @client = MogileFS::MogileFS.new(:hosts => @trackers, :domain => @domain)
  end

  alias teardown teardown_mogilefs

  def test_large_pipe_test
    junk = File.open("/dev/urandom") { |fp| fp.read(1024) }
    junk *= 32
    nr = rand(666) + 1024
    r, w = IO.pipe
    sha1 = Digest::SHA1.new
    th = Thread.new do
      nr.times do
        sha1.update(junk)
        w.write(junk)
      end
      w.close
    end
    assert_equal(nr * junk.size, @client.store_file("a", nil, r))
    r.close
    th.join
    @client.get_file_data("a") do |rd|
      assert_equal(nr * junk.size, @client.store_file("b", nil, rd))
    end
    a = Thread.new { @client.get_file_data("a") { |rd| sha1read(rd) } }
    b = Thread.new { @client.get_file_data("b") { |rd| sha1read(rd) } }
    a = a.value
    b = b.value
    assert_equal a, b
    assert_equal sha1, a

    # We should be able to open FIFOs
    tmp = tmpfile("fifo")
    tmp_path = tmp.path
    File.unlink(tmp_path)
    x!("mkfifo", tmp_path)
    pid = fork do
      File.open(tmp_path, "wb") do |wr|
        nr.times { wr.write(junk) }
      end
    end
    assert_equal(nr * junk.size, @client.store_file("fifo", nil, tmp_path))
    _, status = Process.waitpid2(pid)
    assert status.success?, status.inspect
    fifo_sha1 = @client.get_file_data("fifo") { |rd| sha1read(rd) }
    assert_equal sha1, fifo_sha1
  end

  def sha1read(rd)
    buf = ""
    d = Digest::SHA1.new
    while rd.read(16384, buf)
      d << buf
    end
    d
  end
end

git clone https://yhbt.net/mogilefs-client.git