@Test
  public void testRenewLeaseFailed() throws Exception {
    long id = CorrelationIdGenerator.generateCorrelationId() + 1;

    ConsumerNotifier notifier = lookup(ConsumerNotifier.class);

    metaProxyActions4LeaseOperation(LeaseAnswer.SUCCESS, LeaseAnswer.SUCCESS);
    brokerActions4PollMessageCmd(
        PullMessageAnswer.BASIC.channel(m_channel).creator(SIMPLE_CREATOR));

    TestMessageListener listener = new TestMessageListener().receiveCount(1);
    ConsumerHolder holder = Consumer.getInstance().start(TEST_TOPIC, TEST_GROUP, listener);
    waitUntilConsumerStarted(holder);
    listener.waitUntilReceivedAllMessage();
    Assert.assertEquals(1, listener.getReceivedMessages().size());
    holder.close();
    listener.countDownAll();

    metaProxyActions4LeaseOperation(LeaseAnswer.SUCCESS, LeaseAnswer.FAILED);
    brokerActions4PollMessageCmd(
        PullMessageAnswer.BASIC.channel(m_channel).creator(SIMPLE_CREATOR));

    listener = new TestMessageListener().receiveCount(1);
    ConsumerHolder holder_failed = Consumer.getInstance().start(TEST_TOPIC, TEST_GROUP, listener);
    waitUntilConsumerStarted(holder_failed);
    listener.waitUntilReceivedAllMessage();
    Assert.assertNull(notifier.find(id));
    holder_failed.close();
    listener.countDownAll();
  }
 @Test(expected = IllegalArgumentException.class)
 public void testGroupNotAllowed() {
   ConsumerHolder holder =
       Consumer.getInstance().start(TEST_TOPIC, "non.exist.group", new TestMessageListener());
   Assert.assertFalse(((DefaultConsumerHolder) holder).isConsuming());
   holder.close();
 }
 private void doTestConsumeFailed() throws Exception {
   brokerActions4PollMessageCmd(
       PullMessageAnswer.BASIC.channel(m_channel).creator(SIMPLE_CREATOR));
   TestMessageListener listener = new TestMessageListener().receiveCount(1);
   ConsumerHolder holder = Consumer.getInstance().start(TEST_TOPIC, TEST_GROUP, listener);
   waitUntilConsumerStarted(holder);
   listener.waitUntilReceivedAllMessage(300);
   holder.close();
   Assert.assertEquals(0, listener.getReceivedMessages().size());
   listener.countDownAll();
 }
  @Test
  public void testGetEndpointFailed() throws Exception {
    brokerActions4PollMessageCmd(
        PullMessageAnswer.BASIC.channel(m_channel).creator(SIMPLE_CREATOR));
    TestMessageListener listener = new TestMessageListener().receiveCount(1);
    ConsumerHolder holder = Consumer.getInstance().start(TEST_TOPIC, TEST_GROUP, listener);
    long preCost = listener.waitUntilReceivedAllMessage();
    Assert.assertEquals(1, listener.getReceivedMessages().size());
    holder.close();
    listener.countDownAll();

    Meta meta = loadLocalMeta();
    meta.getEndpoints().clear();
    when(m_metaHolder.getMeta()).thenReturn(meta);

    brokerActions4PollMessageCmd(
        PullMessageAnswer.BASIC.channel(m_channel).creator(SIMPLE_CREATOR));
    listener = new TestMessageListener().receiveCount(1);
    holder = Consumer.getInstance().start(TEST_TOPIC, TEST_GROUP, listener);
    listener.waitUntilReceivedAllMessage(preCost * 2);
    Assert.assertEquals(0, listener.getReceivedMessages().size());
    holder.close();
    listener.countDownAll();
  }
  @Test
  public void testNoResponseWhenPullMessage() throws Exception {
    brokerActions4PollMessageCmd(PullMessageAnswer.NO_ANSWER);

    TestMessageListener msgListener = new TestMessageListener().receiveCount(1);
    ConsumerHolder consumer = Consumer.getInstance().start(TEST_TOPIC, TEST_GROUP, msgListener);

    waitUntilConsumerStarted(consumer);
    msgListener.waitUntilReceivedAllMessage(100);

    consumer.close();

    Assert.assertEquals(0, msgListener.getReceivedMessages().size());
    msgListener.countDownAll();
  }
  @Test
  public void testConsumeFailed() {
    brokerActions4PollMessageCmd(
        PullMessageAnswer.BASIC.channel(m_channel).creator(SIMPLE_CREATOR));

    TestMessageListener msgListener = new TestMessageListener().receiveCount(1).withError(true);
    ConsumerHolder consumer = Consumer.getInstance().start(TEST_TOPIC, TEST_GROUP, msgListener);

    msgListener.waitUntilReceivedAllMessage();

    consumer.close();

    Assert.assertEquals(1, msgListener.getReceivedMessages().size());
    Assert.assertEquals(TEST_MSG, msgListener.getReceivedMessages().get(0));
    msgListener.countDownAll();
  }
  @Test
  public void testBasicMultipleMessageConsume() throws Exception {
    final int batchCount = 3;
    final int msgCountPerBatch = 5;
    brokerActions4PollMessageCmd(
        PullMessageAnswer.BASIC
            .channel(m_channel)
            .creator(
                new RawMessageCreator<TestObjectMessage>() {
                  @Override
                  public List<List<TestObjectMessage>> createRawMessages() {
                    List<List<TestObjectMessage>> batchs = new ArrayList<>();
                    for (int i = 0; i < batchCount; i++) {
                      List<TestObjectMessage> msgs = new ArrayList<>();
                      for (int j = 0; j < msgCountPerBatch; j++) {
                        msgs.add(
                            new TestObjectMessage(
                                "hermes.msg." + (i * msgCountPerBatch + j),
                                i * msgCountPerBatch + j,
                                new byte[] {17}));
                      }
                      batchs.add(msgs);
                    }
                    return batchs;
                  }
                }));

    TestMessageListener msgListener =
        new TestMessageListener().receiveCount(batchCount * msgCountPerBatch);
    ConsumerHolder consumer = Consumer.getInstance().start(TEST_TOPIC, TEST_GROUP, msgListener);

    msgListener.waitUntilReceivedAllMessage();

    consumer.close();
    System.out.println(msgListener.getReceivedMessages());

    Assert.assertFalse(!((DefaultConsumerHolder) consumer).isConsuming());
    Assert.assertEquals(batchCount * msgCountPerBatch, msgListener.getReceivedMessages().size());
    for (int i = 0; i < batchCount; i++) {
      for (int j = 0; j < msgCountPerBatch; j++) {
        Assert.assertEquals(
            17,
            msgListener.getReceivedMessages().get(i * msgCountPerBatch + j).getByteArrayValue()[0]);
      }
    }
    msgListener.countDownAll();
  }
  @Test
  public void testPullMessageTimeout() throws Exception {
    brokerActions4PollMessageCmd(
        PullMessageAnswer.BASIC.withDelay(300).channel(m_channel).creator(SIMPLE_CREATOR));

    TestMessageListener msgListener = new TestMessageListener().receiveCount(1);
    ConsumerHolder consumer = Consumer.getInstance().start(TEST_TOPIC, TEST_GROUP, msgListener);

    msgListener.waitUntilReceivedAllMessage();

    consumer.close();

    Assert.assertFalse(!((DefaultConsumerHolder) consumer).isConsuming());
    Assert.assertEquals(1, msgListener.getReceivedMessages().size());
    Assert.assertEquals(TEST_MSG, msgListener.getReceivedMessages().get(0));
    msgListener.countDownAll();
  }
 @Test(expected = IllegalArgumentException.class)
 public void testTopicNotAllowed() {
   Consumer.getInstance().start("non.exist.topic", TEST_GROUP, new TestMessageListener());
 }
  public ConsumerHolder startPusher(final SubscriptionView sub) {
    final String[] urls = sub.getEndpoints().split(",");

    final ConsumerHolder consumerHolder =
        Consumer.getInstance()
            .start(
                sub.getTopic(),
                sub.getGroup(),
                new BaseMessageListener<RawMessage>() {

                  @Override
                  protected void onMessage(final ConsumerMessage<RawMessage> msg) {
                    while (msg.getStatus() == MessageStatus.NOT_SET) {
                      boolean isCouldAck = false;
                      for (final String url : urls) {
                        BizEvent pushEvent = new BizEvent("Rest.push");
                        Timer.Context timerGlobal = pushTimerGlobal.time();
                        Timer.Context timer = getPushTimer(msg.getTopic()).time();
                        try {
                          pushEvent.addData("topic", sub.getTopic());
                          pushEvent.addData("group", sub.getGroup());
                          pushEvent.addData("refKey", msg.getRefKey());
                          pushEvent.addData("endpoint", url);

                          updateRequestMeter(msg.getTopic());
                          updateRequestSizeHistogram(
                              msg.getTopic(), msg.getBody().getEncodedMessage().length);

                          SubscriptionPushCommand command =
                              new SubscriptionPushCommand(m_httpClient, m_requestConfig, msg, url);
                          HttpResponse pushResponse = command.execute();

                          pushEvent.addData("result", pushResponse.getStatusLine().getStatusCode());
                          if (pushResponse.getStatusLine().getStatusCode()
                              == Response.Status.OK.getStatusCode()) {
                            isCouldAck = true;
                            break;
                          } else if (pushResponse.getStatusLine().getStatusCode()
                              == Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
                            m_logger.warn(
                                "Push message failed, will nack, endpoint:{} reason:{} topic:{} partition:{} offset:{} refKey:{}",
                                url,
                                pushResponse.getStatusLine().getReasonPhrase(),
                                msg.getTopic(),
                                msg.getPartition(),
                                msg.getOffset(),
                                msg.getRefKey());
                            break;
                          } else {
                            m_logger.warn(
                                "Push message failed, will retry, endpoint:{} reason:{} topic:{} partition:{} offset:{} refKey:{}",
                                url,
                                pushResponse.getStatusLine().getReasonPhrase(),
                                msg.getTopic(),
                                msg.getPartition(),
                                msg.getOffset(),
                                msg.getRefKey());
                          }

                          updateFailedMeter(msg.getTopic());
                          if (command.isCircuitBreakerOpen()) {
                            long errorCount =
                                command.getMetrics().getHealthCounts().getErrorCount();
                            m_logger.warn(
                                "Push message CircuitBreak is open, sleep {} seconds", errorCount);
                            Thread.sleep(1000 * errorCount);
                          }
                        } catch (Exception e) {
                          m_logger.warn("Push message exception", e);
                        } finally {
                          m_bizLogger.log(pushEvent);
                          timer.close();
                          timerGlobal.close();
                        }
                      }

                      if (isCouldAck) {
                        msg.ack();
                      } else {
                        msg.nack();
                      }
                    }
                  }
                });
    return consumerHolder;
  }