8 require 'test/zentest_assertions'
16 attr_reader :lasterr, :lasterrstr
19 @responses = Hash.new { |h,k| h[k] = [] }
25 err_camel = err_snake.gsub(/(?:^|_)([a-z])/) { $1.upcase } << 'Error'
26 unless MogileFS::Backend.const_defined?(err_camel)
27 MogileFS::Backend.class_eval("class #{err_camel} < MogileFS::Error; end")
29 MogileFS::Backend.const_get(err_camel)
32 def method_missing(meth, *args)
34 if meth =~ /(.*)=$/ then
35 @responses[$1] << args.first
37 response = @responses[meth].shift
40 @lasterr = response.first
41 @lasterrstr = response.last
42 raise error(@lasterr), @lasterrstr
57 def initialize(read = '', write = StringIO.new)
58 @read_s = read.class.method_defined?(:sysread) ? read : StringIO.new(read)
66 @write_s.sync = do_sync
67 @read_s.sync = do_sync
84 ['AF_INET', 6001, 'localhost', '127.0.0.1']
91 def sysread(bytes, buf = '')
92 @read_s.sysread bytes, buf
95 def recv_nonblock(bytes, flags = 0)
96 ret = @read_s.sysread(bytes)
97 # Ruby doesn't expose pread(2)
98 if (flags & Socket::MSG_PEEK) != 0
99 if @read_s.respond_to?(:sysseek)
100 @read_s.sysseek(-ret.size, IO::SEEK_CUR)
102 @read_s.seek(-ret.size, IO::SEEK_CUR)
107 alias_method :recv, :recv_nonblock
114 @write_s.syswrite data
119 class MogileFS::Client
120 attr_writer :readonly
127 attr_accessor :connections
128 attr_accessor :sockets
133 raise Errno::ECONNREFUSED if @sockets.empty?
134 @connections << [host, port]
144 class TestMogileFS < Test::Unit::TestCase
146 undef_method :default_test
149 @tempdir = File.join Dir.tmpdir, "test_mogilefs_#{$$}"
150 @root = File.join @tempdir, 'root'
151 FileUtils.mkdir_p @root
153 @client = @klass.new :hosts => ['kaa:6001'], :domain => 'test',
155 @backend = FakeBackend.new
156 @client.instance_variable_set '@backend', @backend
158 TCPSocket.sockets = []
159 TCPSocket.connections = []
163 FileUtils.rm_rf @tempdir