1 # -*- encoding: binary -*-
2 STDIN.sync = STDOUT.sync = STDERR.sync = true
3 Thread.abort_on_exception = true
10 trap('CHLD') { Process.waitpid(-1, Process::WNOHANG) rescue nil }
18 attr_reader :lasterr, :lasterrstr
21 @responses = Hash.new { |h,k| h[k] = [] }
27 err_camel = err_snake.gsub(/(?:^|_)([a-z])/) { $1.upcase } << 'Error'
28 unless MogileFS::Backend.const_defined?(err_camel)
29 MogileFS::Backend.class_eval("class #{err_camel} < MogileFS::Error; end")
31 MogileFS::Backend.const_get(err_camel)
34 def method_missing(meth, *args)
36 if meth =~ /(.*)=$/ then
37 @responses[$1] << args.first
39 response = @responses[meth].shift
42 @lasterr = response.first
43 @lasterrstr = response.last
44 raise error(@lasterr), @lasterrstr
53 class MogileFS::Client
59 attr_reader :port, :pid
61 def initialize(server_proc, port = nil)
67 @port ||= 1024 + rand(32768 - 1024)
68 @sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
69 @sock.bind(Socket.pack_sockaddr_in(@port.to_i, '127.0.0.1'))
71 rescue Errno::EADDRINUSE, Errno::EACCES => err
72 @sock.close rescue nil
74 retry if port.nil? && (retries -= 1) > 0
75 warn "retries failed: #{retries} port=#{port.inspect}"
78 @pid = fork { server_proc.call(@sock, @port) }
79 @sock.close rescue nil
83 return if @destroy_pid != $$
84 @sock.close rescue nil
85 Process.kill('KILL', @pid) rescue nil
90 class TestMogileFS < Test::Unit::TestCase
92 undef_method :default_test if method_defined?(:default_test)
95 @client = @klass.new :hosts => ['kaa:6001'], :domain => 'test'
96 @backend = FakeBackend.new
97 @client.instance_variable_set '@backend', @backend
102 # for our mock results
104 alias_method :fetch_row, :shift
110 # devid, hostip, altip, http_port, http_get_port, dev status, host status
111 [ 1, '10.0.0.1', '192.168.0.1', 7500, 7600, 'readonly', 'alive' ],
112 [ 2, '10.0.0.2', '192.168.0.2', 7500, 7600, 'alive', 'alive' ],
113 [ 3, '10.0.0.3', nil, 7500, nil, 'readonly', 'alive' ],
114 [ 4, '10.0.0.4', nil, 7500, nil, 'alive', 'alive' ],
115 [ 5, '10.0.0.5', nil, 7500, nil, 'dead', 'alive' ],
116 [ 6, '10.0.0.6', nil, 7500, nil, 'alive', 'down' ],
129 str.to_s.gsub(/\\/, '\&\&').gsub(/'/, "''")
134 when MogileFS::Mysql::GET_DEVICES then TBL_DEVICES
135 when MogileFS::Mysql::GET_DOMAINS then TBL_DOMAINS