spec/mock_tcp_socket.rb in fastdfs-client-1.3.0 vs spec/mock_tcp_socket.rb in fastdfs-client-1.4.0

- old
+ new

@@ -8,20 +8,32 @@ @host = host @port = port @recv_offset = 0 @connect_state = true @cmd = nil + @content = [] + @header = [] end + def connection + @content = [] + @header = [] + end + def write(*args) pkg = args[0].unpack("C*") + if @header.length <= 0 + @header = pkg + else + @content.concat(pkg) + end @cmd ||= pkg[8] sleep(rand(0..4)) end def recv(len) - sleep(rand(0..3)) + sleep(rand(0..2)) data = case @cmd when 101 gate_tracker(len) when 11 upload_file(len) @@ -53,21 +65,22 @@ header = ProtoCommon.header_bytes(CMD::RESP_CODE, 0) header[7] = ProtoCommon::TRACKER_BODY_LEN group_name = Utils.array_merge([].fill(0, 0...16), TestConfig::GROUP_NAME.bytes) ip = Utils.array_merge([].fill(0, 0...15), TestConfig::STORAGE_IP.bytes) - port = Utils.number_to_buffer(TestConfig::STORAGE_PORT.to_i) + port = TestConfig::STORAGE_PORT.to_i.to_eight_buffer store_path = Array(TestConfig::STORE_PATH) (header+group_name+ip+port+store_path)[@recv_offset...@recv_offset+len].pack("C*") end def upload_file(len) header = ProtoCommon.header_bytes(CMD::RESP_CODE, 0) group_name = Utils.array_merge([].fill(0, 0...16), TestConfig::GROUP_NAME.bytes) - file_name = TestConfig::FILE_NAME.bytes - res = (group_name + file_name) + path = path_replace_extname + file_path_bytes = path.bytes + res = (group_name + file_path_bytes) header[7] = (header + res).length res = (header + res) res[@recv_offset...@recv_offset+len].pack("C*") end @@ -92,7 +105,15 @@ def download_file(len) header = ProtoCommon.header_bytes(CMD::RESP_CODE, 0) body = IO.read(TestConfig::FILE).bytes header[7] = body.length (header + body)[@recv_offset...@recv_offset+len].pack("C*") + end + + private + def path_replace_extname + path = TestConfig::FILE_PATH + extname = File.extname(path) + path.gsub!(extname, ".#{@header[19..-1].reject{|i| i.zero? }.pack('C*')}") + path end end \ No newline at end of file