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());
 }
 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));
 }
 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));
 }
 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);
 }