1 STDIN.sync = STDOUT.sync = STDERR.sync = true
9 require 'test/zentest_assertions'
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
61 ObjectSpace.each_object(TempServer) { |t| t.destroy! }
64 def initialize(server_proc)
65 @thr = @port = @sock = nil
68 @port = 1024 + rand(32768 - 1024)
69 @sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
70 @sock.bind(Socket.pack_sockaddr_in(@port, '127.0.0.1'))
72 rescue Errno::EADDRINUSE, Errno::EACCES
73 @sock.close rescue nil
74 retry if (retries += 1) < 10
76 @thr = Thread.new(@sock, @port) { |s,p| server_proc.call(s, p) }
80 @sock.close rescue nil
81 Thread.kill(@thr) rescue nil
86 class TestMogileFS < Test::Unit::TestCase
88 undef_method :default_test
91 @client = @klass.new :hosts => ['kaa:6001'], :domain => 'test'
92 @backend = FakeBackend.new
93 @client.instance_variable_set '@backend', @backend
98 # for our mock results
100 alias_method :fetch_row, :shift
106 # devid, hostip, altip, http_port, http_get_port
107 [ 1, '10.0.0.1', '192.168.0.1', 7500, 7600 ],
108 [ 2, '10.0.0.2', '192.168.0.2', 7500, 7600 ],
109 [ 3, '10.0.0.3', nil, 7500, nil ],
110 [ 4, '10.0.0.4', nil, 7500, nil ],
123 str.to_s.gsub(/\\/, '\&\&').gsub(/'/, "''")
128 when MogileFS::Mysql::GET_DEVICES then TBL_DEVICES
129 when MogileFS::Mysql::GET_DOMAINS then TBL_DOMAINS