public function testSend() { $subject = new BigpipePublishTask($this->pipe_name, $this->pipelet_id, $this->session_id, $this->conf, $this->sub_meta); $subject->unittest = true; $pkg = new BigpipeMessagePackage(); // 测试1: send on unstarted object $this->assertFalse($subject->send($pkg)); // set object to be started $this->assertTrue(TestUtilities::set_private_var($subject, '_is_started', true)); // 测试2 生成message package失败 $this->assertFalse($subject->send($pkg)); // set last send ok $this->assertTrue(TestUtilities::set_private_var($subject, '_last_send_ok', true)); $this->assertTrue($pkg->push('Testing Publisher Task')); // 定义meta行为 $broker = $this->_gen_pub_broker(); $this->assertTrue(false !== $broker); $this->stub_meta->expects($this->any())->method('get_pub_broker')->will($this->returnValue($broker)); $this->assertTrue(TestUtilities::set_private_var($subject, '_meta_adapter', $this->stub_meta)); // 设置stomp行为 $this->stub_stomp->expects($this->any())->method('send')->will($this->onConsecutiveCalls(false, false, false, true, true, true, true)); $this->stub_stomp->expects($this->any())->method('set_destination')->will($this->returnValue(true)); $this->stub_stomp->expects($this->any())->method('connect')->will($this->returnValue(true)); // mock ack result $res_arr = $this->_gen_ack_response($subject); $this->stub_stomp->expects($this->any())->method('receive')->will($this->onConsecutiveCalls(null, $res_arr['bad_session'], $res_arr['bad_receipt'], $res_arr['error_body'], $res_arr['good'])); $this->assertTrue(TestUtilities::set_private_var($subject, '_stomp_adapter', $this->stub_stomp)); // 测试3 send失败,failover失败并强制退出 $this->assertFalse($subject->send($pkg)); // 测试4 ack失败状态(ack三个失败分支) $this->assertFalse($subject->send($pkg)); // 测试5 error body 和 send成功 $this->assertFalse(false === $subject->send($pkg)); }
/** * @brief: 向primary broker 发送一条消息 * @param BigpipeMessagePackage $msg * @return: BigpipePubResult on success or false on failure */ public function send($msg_package) { if (false === $this->_inited) { BigpipeLog::fatal("[%s:%u][%s][publisher is not inited]", __FILE__, __LINE__, __FUNCTION__); return false; } $pipelet_id = $this->_partitioner->get_pipelet_id($msg_package, $this->_num_piplet); if ($pipelet_id >= $this->_num_piplet || $pipelet_id < 0) { BigpipeLog::fatal("[%s:%u][%s][invalid pipelet][pipelet_id:%u][max_pipelet_id:%u]", __FILE__, __LINE__, __FUNCTION__, $pipelet_id, $this->_num_piplet - 1); return false; } $pub = null; if (!isset($this->_pub_list[$pipelet_id])) { $pub = new BigpipePublishTask($this->_pipe_name, $pipelet_id, $this->_session, $this->_conf, $this->_meta_adapter); // 在pub_list中, 保存pub的引用 $this->_pub_list[$pipelet_id] =& $pub; } else { $pub =& $this->_pub_list[$pipelet_id]; } $pub_result = $pub->start(); if (!$pub_result) { BigpipeLog::warning("[%s:%u][%s][fail to start publish task][pipe_name:%u][pipelet_id:%u]", __FILE__, __LINE__, __FUNCTION__, $this->_pipe_name, $pipelet_id); } else { $pub_result = $pub->send($msg_package); } if (false === $pub_result) { $pub->stop(); // 发送出现错误,停止task(等待下次重启) } unset($pub); // 主动释放$pub与pub task的联系,否则可能会影响到_pub_list中被引用的元素。 return $pub_result; }