public function testUpdate()
 {
     $subject = new QueueServerMeta();
     // ²âÊÔ1 update uninitedµÄ¶ÔÏó
     $this->assertFalse($subject->update());
     $name = 'queue';
     $token = 'token';
     $this->assertTrue($subject->init($name, $token, $this->meta_params));
     // ¶¨ÒåzkÐÐΪ
     $this->stub_zk->expects($this->any())->method('connect')->will($this->onConsecutiveCalls(false, true));
     $this->stub_zk->expects($this->any())->method('exists')->will($this->onConsecutiveCalls(false, true, true, true, true));
     $this->stub_zk->expects($this->any())->method('get')->will($this->onConsecutiveCalls('error ip', '127.0.0.0:9527'));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_zk', $this->stub_zk));
     // ²âÊÔ2 connectʧ°Ü
     $this->assertFalse($subject->update());
     // ²âÊÔ3 update queue server info
     // ²âÊÔ3.1 reg_path²»´æÔÚ
     $this->assertFalse($subject->update());
     // ²âÊÔ3.2 reg_path¸ñʽ´íÎó
     $this->assertFalse($subject->update());
     // ²âÊÔ3.3 ³É¹¦
     $this->assertTrue($subject->update());
     // ²âÊÔ3.4 Íê³ÉÆäËû·ÖÖ§
     $this->assertEquals($name, $subject->queue_name());
     $this->assertEquals($token, $subject->token());
     $address = array('socket_address' => '127.0.0.0', 'socket_port' => 9527);
     $this->assertEquals($address, $subject->queue_address());
 }
        if ($msg_count != $max_msg_count + 1) {
            echo "[expected:{$max_msg_count}][actual:{$msg_count}]\n";
            continue;
            // 打包失败,退出
        }
        // 当消息添加完后,使用publisher的send接口,发布数据
        // send成功,返回的pub_result中包含了pipelet_id和pipelet_msg_id
        // pipelet_msg_id唯一标识了本个数据包在pipelet中的位置
        // send失败,用户可以选择继续send,这时send接口内部会更新状态,尝试重新发布
        $pub_result = $pub->send($msg_package);
        if (false === $pub_result) {
            echo "[fail to publish message package][count:{$count}]\n";
            break;
            // 出错便停止
        } else {
            // write result to file
            $session = TestUtilities::get_private_var($pub, '_session');
            $ret_str = sprintf("%d,%u,%u,%u,%s\n", $pub_result->error_no, $pub_result->pipelet_id, $pub_result->pipelet_msg_id, $pub_result->session_msg_id, $session);
            fwrite($pub_file, $ret_str);
            $succeed++;
        }
        echo "[Publisher][count:{$count}][success:{$succeed}]====\n";
        // 当发布结束后,使用uninit强制清空发布状态
        $pub->uninit();
    } else {
        echo "[Failure][init publisher]\n";
    }
    $count++;
}
// end of while
BigpipeLog::close();
 public function testIsReadable()
 {
     $subject = new BigpipeConnection($this->conf);
     // ²âÊÔ1 ²ÎÊý´íÎó
     $timeo = -4;
     $this->assertEquals(BigpipeErrorCode::INVALID_PARAM, $subject->is_readable($timeo));
     // ÉèÖÃsocketÐÐΪ
     $this->stub_sock->expects($this->any())->method('is_readable')->will($this->onConsecutiveCalls(c_socket::ERROR, c_socket::TIMEOUT, c_socket::OK));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_socket', $this->stub_sock));
     $timeo = 40;
     // ²âÊÔ2 socket error
     $this->assertEquals(BigpipeErrorCode::ERROR_CONNECTION, $subject->is_readable($timeo));
     // ²âÊÔ3 ³¬Ê±´íÎó
     $this->assertEquals(BigpipeErrorCode::TIMEOUT, $subject->is_readable($timeo));
     // ²âÊÔ4 READABLE
     $this->assertEquals(BigpipeErrorCode::READABLE, $subject->is_readable($timeo));
 }
 public function testAck()
 {
     $subject = new BigpipeQueueClient();
     // 测试1 uninit下调用接口
     $this->assertFalse($subject->ack(null));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_inited', true));
     // 测试2 参数检查失败
     $ack_msg = new BigpipeQueueMessage();
     $this->assertFalse($subject->ack($ack_msg));
     $ack_msg->pipe_name = 'pipe';
     // 定义connection的行为
     $this->stub_conn->expects($this->once())->method('send')->will($this->returnValue(true));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_connection', $this->stub_conn));
     // 定义meta的行为
     // todo 无法控制只抛出一次异常,异常接收分支会被跳过
     $e = new ErrorException('php unit test');
     $this->stub_meta->expects($this->any())->method('queue_name')->will($this->returnValue($this->que_name));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_meta', $this->stub_meta));
     $this->assertTrue($subject->ack($ack_msg));
 }
 /**
  * 生成ack响应供测试程序使用
  * @return ack响应集供test case选择
  */
 private function _gen_ack_response($subject)
 {
     $ack = new BStompAckFrame();
     $ack->status = BStompIdAckType::TOPIC_ID;
     $ack->ack_type = BStompFrameType::ACK;
     $ack->session_message_id = TestUtilities::get_private_var($subject, '_session_msg_id') + 1;
     $this->assertFalse(false === $ack->session_message_id);
     $ack->topic_message_id = 369;
     $ack->global_message_id = 7659;
     $ack->delay_time = 0;
     $ack->destination = 'unknown';
     $ack->receipt_id = 'fake-receipt-id';
     $ack->store();
     $good_ack = $ack->buffer();
     $orig_smid = $ack->session_message_id;
     $ack->session_message_id = $orig_smid + 10;
     $ack->store();
     $bad_session = $ack->buffer();
     $ack->session_message_id = $orig_smid;
     $ack->receipt_id = BigpipeUtilities::get_uid();
     $ack->store();
     $bad_receipt = $ack->buffer();
     $res_arr = array('good' => $good_ack, 'bad_session' => $bad_session, 'bad_receipt' => $bad_receipt, 'error_body' => 'error');
     return $res_arr;
 }
 /**
  * ²âÊÔ Ë½Óк¯Êý_init_meta
  */
 public function testOthers()
 {
     $subject = new BigpipeQueueAdministrationTools();
     $method = TestUtilities::get_private_method($subject, '_init_meta');
     $this->assertTrue(false != $method);
     // ²âÊÔinit metaʧ°ÜÇé¿ö
     $this->assertFalse($method->invoke($subject, null));
     $method = TestUtilities::get_private_method($subject, '_check_assign_array');
     $this->assertTrue(false != $method);
     // ²âÊÔcheckʧ°ÜÇé¿ö
     $testkey = 'test';
     $catched_count = 0;
     try {
         $dest = array();
         $src = array();
         $this->assertFalse($method->invoke($subject, $dest, $testkey, $src, $testkey));
     } catch (Exception $e) {
         $catched_count++;
     }
     $this->assertEquals(1, $catched_count);
     // ²âÊÔnormalize_queue_paramsʧ°Ü
     $method = TestUtilities::get_private_method($subject, '_normalize_queue_params');
     $this->assertTrue(false != $method);
     $params = $this->queue_conf;
     unset($params['window_size']);
     // ÖÆÔì´íÎó
     $this->assertFalse($method->invoke($subject, $params));
 }
 public function testDeleteEntry()
 {
     $subject = new BigpipeMetaManager();
     // 测试1:未init时调用接口
     $path = '/path';
     $this->assertFalse($subject->delete_entry($path));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_inited', true));
     // 定义zk行为
     $this->stub_zk->expects($this->any())->method('exists')->will($this->onConsecutiveCalls(false, true));
     $this->stub_zk->expects($this->once())->method('remove_path')->will($this->returnValue(true));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_zk_connection', $this->stub_zk));
     // 测试2:set的etnry不存在
     $this->assertFalse($subject->delete_entry($path));
     // 测试5:成功set
     $this->assertTrue($subject->delete_entry($path));
 }
 public function testGetChildren()
 {
     $subject = new ZooKeeperConnection();
     $path = '/path/sub';
     // 测试1:未init时调用接口
     $this->assertFalse($subject->get_children($path));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_inited', true));
     // 定义zk行为
     $this->stub_zk->expects($this->once())->method('getChildren')->will($this->returnValue(true));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_zk', $this->stub_zk));
     // 测试2:create失败
     $this->assertTrue($subject->get_children($path));
 }
 public function testUninitMeta()
 {
     $subject = new MetaAgentAdapter();
     $metod = TestUtilities::get_private_method($subject, '_uninit_meta');
     $this->assertTrue(false != $metod);
     // 测试1 进入close流程
     // 测试1.1 测试_uninit_meta方法
     // 定义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->returnValue(true));
     $this->stub_conn->expects($this->any())->method('send')->will($this->returnValue(true));
     $this->stub_conn->expects($this->any())->method('close');
     $ack_pkgs = $this->_gen_uninit_meta_ack();
     $this->stub_conn->expects($this->any())->method('receive')->will($this->onConsecutiveCalls(null, $ack_pkgs['bad'], $ack_pkgs['good']));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_connection', $this->stub_conn));
     // 测试1.1.1 meta_name为空
     $subject->meta_name = null;
     $metod->invoke($subject, $subject->meta_name);
     // 测试2.1.2 request返回为空
     $subject->meta_name = 'meta';
     $metod->invoke($subject, $subject->meta_name);
     // 测试2.1.3 res_body无法解包
     $subject->meta_name = 'meta';
     $metod->invoke($subject, $subject->meta_name);
     // 测试2.1.4 res_body正常
     $subject->meta_name = 'meta';
     $metod->invoke($subject, $subject->meta_name);
 }
 /**
  * ²âÊÔ_update_meta
  */
 public function testUpdateMeta()
 {
     $subject = new BigpipeSubscriber();
     $method = TestUtilities::get_private_method($subject, '_update_meta');
     $this->assertTrue(false !== $method);
     // ÉèÖÃmeta_adapterµÄÐÐΪ
     $sub_info = $this->_gen_sub_broker_group();
     $grp_fail = $this->_gen_sub_broker_group();
     $grp_fail['broker_group']->status = BigpipeBrokerGroupStatus::FAIL;
     $no_cand = $this->_gen_sub_broker_group();
     $no_cand['broker_group']->brokers[1]->role = BigpipeBrokerRole::PRIMARY;
     $this->stub_meta->expects($this->any())->method('get_sub_broker_group')->will($this->onConsecutiveCalls($grp_fail, $no_cand, $sub_info, $sub_info, $sub_info));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_meta_adapter', $this->stub_meta));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_pipelet_msg_id', SubscribeStartPoint::START_FROM_FIRST_POINT));
     // ²âÊÔ1 È¡µ½µÄgroupÊÇfail״̬
     $this->assertFalse($method->invoke($subject));
     // ²âÊÔ2 ÎÞºòÑ¡Õß
     $this->assertFalse($method->invoke($subject));
     // ²âÊÔ3 preferÌõ¼þ²»¶Ô
     $this->assertTrue(TestUtilities::set_private_var($subject, '_pref_conn', 9));
     $this->assertFalse($method->invoke($subject));
     // ²âÊÔ4 ³É¹¦²¢Ñ¡È¡primary broker
     $this->assertTrue(TestUtilities::set_private_var($subject, '_pref_conn', BigpipeConnectPreferType::PRIMARY_BROKER_ONLY));
     $this->assertTrue($method->invoke($subject));
     $brokers = TestUtilities::get_private_var($subject, '_brokers');
     $this->assertTrue(false != $brokers);
     $this->assertEquals(1, count($brokers));
     $this->assertEquals(BigpipeBrokerRole::PRIMARY, $brokers[0]->role);
     // ²âÊÔ5 ³É¹¦²¢Ñ¡È¡secondary broker
     $this->assertTrue(TestUtilities::set_private_var($subject, '_pref_conn', BigpipeConnectPreferType::SECONDARY_BROKER_ONLY));
     $this->assertTrue($method->invoke($subject));
     $brokers = TestUtilities::get_private_var($subject, '_brokers');
     $this->assertTrue(false != $brokers);
     $this->assertEquals(1, count($brokers));
     $this->assertEquals(BigpipeBrokerRole::SECONDARY, $brokers[0]->role);
 }
 /**
  * 测试set_destination接口和close接口
  */
 public function testOther()
 {
     $subject = new BigpipeStompAdapter($this->conf);
     // 定义connection行为
     $this->stub_conn->expects($this->any())->method('set_destinations');
     $this->stub_conn->expects($this->any())->method('close');
     $this->assertTrue(TestUtilities::set_private_var($subject, '_connection', $this->stub_conn));
     // set_destination和close是两个无状态接口
     // 仅覆盖到便可
     $dest = array('fake destination');
     $subject->set_destination($dest);
     $subject->close();
 }
 public function testUninit()
 {
     // ²âÊÔ1 uninitδ³õʼ»¯µÄpublisher
     $subject = new BigpipePublisher();
     $subject->uninit();
     // ²âÊÔ2 uninit³É¹¦
     $stub_meta = $this->getMockBuilder('MetaAgentAdapter')->disableOriginalConstructor()->getMock();
     // configure the stubs
     $stub_meta->expects($this->once())->method('uninit')->will($this->returnValue(null));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_meta_adapter', $stub_meta));
     $this->assertTrue(TestUtilities::set_private_var($subject, '_inited', true));
     $subject->uninit();
     $this->assertTrue(true);
 }