1 STDIN.sync = STDOUT.sync = STDERR.sync = true
14 attr_reader :lasterr, :lasterrstr
17 @responses = Hash.new { |h,k| h[k] = [] }
23 err_camel = err_snake.gsub(/(?:^|_)([a-z])/) { $1.upcase } << 'Error'
24 unless MogileFS::Backend.const_defined?(err_camel)
25 MogileFS::Backend.class_eval("class #{err_camel} < MogileFS::Error; end")
27 MogileFS::Backend.const_get(err_camel)
30 def method_missing(meth, *args)
32 if meth =~ /(.*)=$/ then
33 @responses[$1] << args.first
35 response = @responses[meth].shift
38 @lasterr = response.first
39 @lasterrstr = response.last
40 raise error(@lasterr), @lasterrstr
49 class MogileFS::Client
58 ObjectSpace.each_object(TempServer) { |t| t.destroy! }
61 def initialize(server_proc)
62 @thr = @port = @sock = nil
65 @port = 1024 + rand(32768 - 1024)
66 @sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
67 @sock.bind(Socket.pack_sockaddr_in(@port, '127.0.0.1'))
69 rescue Errno::EADDRINUSE, Errno::EACCES
70 @sock.close rescue nil
71 retry if (retries += 1) < 10
73 @thr = Thread.new(@sock, @port) { |s,p| server_proc.call(s, p) }
77 @sock.close rescue nil
78 Thread.kill(@thr) rescue nil
83 class TestMogileFS < Test::Unit::TestCase
85 undef_method :default_test
88 @client = @klass.new :hosts => ['kaa:6001'], :domain => 'test'
89 @backend = FakeBackend.new
90 @client.instance_variable_set '@backend', @backend
95 # for our mock results
97 alias_method :fetch_row, :shift
103 # devid, hostip, altip, http_port, http_get_port
104 [ 1, '10.0.0.1', '192.168.0.1', 7500, 7600 ],
105 [ 2, '10.0.0.2', '192.168.0.2', 7500, 7600 ],
106 [ 3, '10.0.0.3', nil, 7500, nil ],
107 [ 4, '10.0.0.4', nil, 7500, nil ],
120 str.to_s.gsub(/\\/, '\&\&').gsub(/'/, "''")
125 when MogileFS::Mysql::GET_DEVICES then TBL_DEVICES
126 when MogileFS::Mysql::GET_DOMAINS then TBL_DOMAINS