/** * 测试私有函数 */ public function testRequest() { $subject = new MetaAgentAdapter(); $metod = TestUtilities::get_private_method($subject, '_request'); $this->assertTrue(false != $metod); // mock connection 行为 // 定义connection行为 $this->stub_conn->expects($this->any())->method('is_connected')->will($this->returnValue(true)); $this->stub_conn->expects($this->any())->method('create_connection')->will($this->onConsecutiveCalls(false, true, true, true)); $this->stub_conn->expects($this->any())->method('send')->will($this->onConsecutiveCalls(false, true, true)); $this->stub_conn->expects($this->any())->method('close'); $ack = new MetaAgentErrorAckFrame(); $ack->error_code = 369; $ack->error_code = 'bingo'; $ack->store(); $ack_data = $ack->buffer(); $this->stub_conn->expects($this->any())->method('receive')->will($this->onConsecutiveCalls($ack_data)); $this->assertTrue(TestUtilities::set_private_var($subject, '_connection', $this->stub_conn)); $frame = new FakeFrame(); // 测试1 create connection失败 $this->assertNull($metod->invoke($subject, $frame)); // 测试2 buffer size为0 $this->assertNull($metod->invoke($subject, $frame)); // 使用UninitMetaFrame测试 $frame = new UninitMetaFrame(); $frame->meta_name = 'meta'; // 测试3 send失败 $this->assertNull($metod->invoke($subject, $frame)); // 测试4 send成功,但收到server error包 $this->assertNull($metod->invoke($subject, $frame)); $this->assertTrue(TestUtilities::set_private_var($subject, '_inited', false)); }
/** * 检查ack状态,如果返回的是标准错误包,说明命令处理失败了,读取错误信息 * @param binary string $res_body: 响应消息流 * @return false如果ack不是status ok, 否则返回true */ private function _ack_status_ok($res_body) { // load command type $type = BigpipeFrame::get_command_type($res_body); if (MetaAgentFrameType::UNKNOWN_TYPE == $type) { BigpipeLog::warning('[no cmd_type in ack]'); return false; } if ($type == MetaAgentFrameType::ACK_ERROR_PACK) { // 有错误,返回的是错误提示包 $ack = new MetaAgentErrorAckFrame(); if (!$ack->load($res_body)) { $this->last_error_message = $ack->last_error_message(); } else { $this->last_error_message = $ack->error_msg; } BigpipeLog::warning('[%s:%u][%s][ack error][cmd_type:%d][err:%s]', __FILE__, __LINE__, __FUNCTION__, $ack->command_type, $this->last_error_message); return false; // 不是正常ack } // 处理标准错误包 return true; }