1 # -*- encoding: binary -*-
2 STDIN.sync = STDOUT.sync = STDERR.sync = true
9 trap('CHLD') { Process.waitpid(-1, Process::WNOHANG) rescue nil }
17 attr_reader :lasterr, :lasterrstr
20 @responses = Hash.new { |h,k| h[k] = [] }
26 err_camel = err_snake.gsub(/(?:^|_)([a-z])/) { $1.upcase } << 'Error'
27 unless MogileFS::Backend.const_defined?(err_camel)
28 MogileFS::Backend.class_eval("class #{err_camel} < MogileFS::Error; end")
30 MogileFS::Backend.const_get(err_camel)
33 def method_missing(meth, *args)
35 if meth =~ /(.*)=$/ then
36 @responses[$1] << args.first
38 response = @responses[meth].shift
41 @lasterr = response.first
42 @lasterrstr = response.last
43 raise error(@lasterr), @lasterrstr
52 class MogileFS::Client
58 attr_reader :port, :pid
61 ObjectSpace.each_object(TempServer) { |t| t.destroy! }
64 at_exit { TempServer.destroy_all! }
66 def initialize(server_proc, port = nil)
71 @port ||= 1024 + rand(32768 - 1024)
72 @sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
73 @sock.bind(Socket.pack_sockaddr_in(@port.to_i, '127.0.0.1'))
75 rescue Errno::EADDRINUSE, Errno::EACCES => err
76 @sock.close rescue nil
78 retry if port.nil? && (retries -= 1) > 0
79 warn "retries failed: #{retries} port=#{port.inspect}"
82 @pid = fork { server_proc.call(@sock, @port) }
83 @sock.close rescue nil
87 @sock.close rescue nil
88 Process.kill('KILL', @pid) rescue nil
93 class TestMogileFS < Test::Unit::TestCase
95 undef_method :default_test if method_defined?(:default_test)
98 @client = @klass.new :hosts => ['kaa:6001'], :domain => 'test'
99 @backend = FakeBackend.new
100 @client.instance_variable_set '@backend', @backend
105 # for our mock results
107 alias_method :fetch_row, :shift
113 # devid, hostip, altip, http_port, http_get_port, dev status, host status
114 [ 1, '10.0.0.1', '192.168.0.1', 7500, 7600, 'readonly', 'alive' ],
115 [ 2, '10.0.0.2', '192.168.0.2', 7500, 7600, 'alive', 'alive' ],
116 [ 3, '10.0.0.3', nil, 7500, nil, 'readonly', 'alive' ],
117 [ 4, '10.0.0.4', nil, 7500, nil, 'alive', 'alive' ],
118 [ 5, '10.0.0.5', nil, 7500, nil, 'dead', 'alive' ],
119 [ 6, '10.0.0.6', nil, 7500, nil, 'alive', 'down' ],
132 str.to_s.gsub(/\\/, '\&\&').gsub(/'/, "''")
137 when MogileFS::Mysql::GET_DEVICES then TBL_DEVICES
138 when MogileFS::Mysql::GET_DOMAINS then TBL_DOMAINS