add mogstored_rack example
[ruby-mogilefs-client.git] / test / test_mogstored_rack.rb
blob491f372995fd96da809a2bd4f999851bb68e78b9
1 # -*- encoding: binary -*-
2 require "./test/fresh"
4 class TestMogstoredRack < Test::Unit::TestCase
5   include TestFreshSetup
6   def setup
7     setup_mogilefs
8   end
10   def test_md5_check
11     add_host_device_domain
12     client = MogileFS::MogileFS.new :hosts => @hosts, :domain => @domain
13     node = "#@test_host:#@mogstored_http_port"
14     pid = fork do
15       # not modifying this hash in the same process
16       MogileFS::HTTPFile::MD5_TRAILER_NODES[node] = true
17       client.store_content("md5_me", nil, "HELLO WORLD")
18     end
19     _, status = Process.waitpid2(pid)
20     assert status.success?, status.inspect
21     assert_equal "HELLO WORLD", client.get_file_data("md5_me")
22   end
24   def setup_mogstored
25     @docroot = Dir.mktmpdir(["mogfresh", "docroot"])
26     @mogstored_mgmt = TCPServer.new(@test_host, 0)
27     @mogstored_http = TCPServer.new(@test_host, 0)
28     @mogstored_mgmt_port = @mogstored_mgmt.addr[1]
29     @mogstored_http_port = @mogstored_http.addr[1]
30     @mogstored_conf = Tempfile.new(["mogstored", "conf"])
31     @mogstored_pid = Tempfile.new(["mogstored", "pid"])
32     @mogstored_conf.write <<EOF
33 pidfile = #{@mogstored_pid.path}
34 maxconns = 1000
35 mgmtlisten = #@test_host:#{@mogstored_mgmt_port}
36 server = none
37 docroot = #@docroot
38 EOF
39     @mogstored_conf.flush
40     @mogstored_mgmt.close
42     unicorn_setup
44     x!("mogstored", "--daemon", "--config=#{@mogstored_conf.path}")
45     wait_for_port @mogstored_mgmt_port
46   end
48   # I would use Rainbows! + *Threads + Ruby 1.9.3 in production
49   def unicorn_setup
50     examples_dir = Dir.pwd + "/examples"
51     assert File.directory?(examples_dir)
52     @ru = Tempfile.new(%w(mogstored_rack .ru))
53     @ru.write <<EOF
54 require "mogstored_rack"
55 use Rack::Head
56 run MogstoredRack.new("#@docroot")
57 EOF
58     @ru.flush
60     @unicorn_pid = Tempfile.new(%w(unicorn .pid))
61     @unicorn_conf = Tempfile.new(%w(unicorn.conf .rb))
62     @unicorn_stderr = Tempfile.new(%w(unicorn .stderr))
63     @unicorn_stdout = Tempfile.new(%w(unicorn .stdout))
64     @unicorn_conf.write <<EOF
65 listen "#@test_host:#{@mogstored_http_port}"
66 pid "#{@unicorn_pid.path}"
67 stderr_path "#{@unicorn_stderr.path}"
68 stdout_path "#{@unicorn_stdout.path}"
69 rewindable_input false
70 EOF
71     @unicorn_conf.flush
73     @mogstored_http.close
74     x!("unicorn", "-I", examples_dir, "-E", "deployment",
75        "--daemon", "--config", @unicorn_conf.path, @ru.path)
76     wait_for_port @mogstored_http_port
77     40.times do
78       break if File.size(@unicorn_pid.path) > 0
79       sleep 0.1
80     end
81   end
83   def teardown
84     pid = File.read(@unicorn_pid.path).to_i
85     Process.kill(:QUIT, pid) if pid > 0
86     teardown_mogilefs
87     puts(@unicorn_stderr.read) if $DEBUG
88   end
89 end if `which unicorn`.chomp.size > 0