Blame tests/0063-clusterid.cpp

Packit 2997f0
/*
Packit 2997f0
 * librdkafka - Apache Kafka C library
Packit 2997f0
 *
Packit 2997f0
 * Copyright (c) 2016, Magnus Edenhill
Packit 2997f0
 * All rights reserved.
Packit 2997f0
 *
Packit 2997f0
 * Redistribution and use in source and binary forms, with or without
Packit 2997f0
 * modification, are permitted provided that the following conditions are met:
Packit 2997f0
 *
Packit 2997f0
 * 1. Redistributions of source code must retain the above copyright notice,
Packit 2997f0
 *    this list of conditions and the following disclaimer.
Packit 2997f0
 * 2. Redistributions in binary form must reproduce the above copyright notice,
Packit 2997f0
 *    this list of conditions and the following disclaimer in the documentation
Packit 2997f0
 *    and/or other materials provided with the distribution.
Packit 2997f0
 *
Packit 2997f0
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 2997f0
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 2997f0
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 2997f0
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit 2997f0
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit 2997f0
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit 2997f0
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 2997f0
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit 2997f0
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit 2997f0
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit 2997f0
 * POSSIBILITY OF SUCH DAMAGE.
Packit 2997f0
 */
Packit 2997f0
Packit 2997f0
#include <iostream>
Packit 2997f0
#include <cstring>
Packit 2997f0
#include <cstdlib>
Packit 2997f0
#include "testcpp.h"
Packit 2997f0
Packit 2997f0
/**
Packit 2997f0
 * Test Handle::clusterid()
Packit 2997f0
 */
Packit 2997f0
Packit 2997f0
static void do_test_clusterid (void) {
Packit 2997f0
  /*
Packit 2997f0
   * Create client with appropriate protocol support for
Packit 2997f0
   * retrieving clusterid
Packit 2997f0
   */
Packit 2997f0
  RdKafka::Conf *conf;
Packit 2997f0
  Test::conf_init(&conf, NULL, 10);
Packit 2997f0
  Test::conf_set(conf, "api.version.request", "true");
Packit 2997f0
  std::string errstr;
Packit 2997f0
  RdKafka::Producer *p_good = RdKafka::Producer::create(conf, errstr);
Packit 2997f0
  if (!p_good)
Packit 2997f0
    Test::Fail("Failed to create client: " + errstr);
Packit 2997f0
  delete conf;
Packit 2997f0
Packit 2997f0
  /*
Packit 2997f0
   * Create client with lacking protocol support.
Packit 2997f0
   */
Packit 2997f0
  Test::conf_init(&conf, NULL, 10);
Packit 2997f0
  Test::conf_set(conf, "api.version.request", "false");
Packit 2997f0
  Test::conf_set(conf, "broker.version.fallback", "0.9.0");
Packit 2997f0
  RdKafka::Producer *p_bad = RdKafka::Producer::create(conf, errstr);
Packit 2997f0
  if (!p_bad)
Packit 2997f0
    Test::Fail("Failed to create client: " + errstr);
Packit 2997f0
  delete conf;
Packit 2997f0
Packit 2997f0
Packit 2997f0
  std::string clusterid;
Packit 2997f0
Packit 2997f0
  /*
Packit 2997f0
   * good producer, give the first call a timeout to allow time
Packit 2997f0
   * for background metadata requests to finish.
Packit 2997f0
   */
Packit 2997f0
  std::string clusterid_good_1 = p_good->clusterid(tmout_multip(2000));
Packit 2997f0
  if (clusterid_good_1.empty())
Packit 2997f0
    Test::Fail("good producer(w timeout): ClusterId is empty");
Packit 2997f0
  Test::Say("good producer(w timeout): ClusterId " + clusterid_good_1 + "\n");
Packit 2997f0
Packit 2997f0
  /* Then retrieve a cached copy. */
Packit 2997f0
  std::string clusterid_good_2 = p_good->clusterid(0);
Packit 2997f0
  if (clusterid_good_2.empty())
Packit 2997f0
    Test::Fail("good producer(0): ClusterId is empty");
Packit 2997f0
  Test::Say("good producer(0): ClusterId " + clusterid_good_2 + "\n");
Packit 2997f0
Packit 2997f0
  if (clusterid_good_1 != clusterid_good_2)
Packit 2997f0
    Test::Fail("Good ClusterId mismatch: " + clusterid_good_1 +
Packit 2997f0
               " != " + clusterid_good_2);
Packit 2997f0
Packit 2997f0
  /*
Packit 2997f0
   * Try bad producer, should return empty string.
Packit 2997f0
   */
Packit 2997f0
  std::string clusterid_bad_1 = p_bad->clusterid(tmout_multip(2000));
Packit 2997f0
  if (!clusterid_bad_1.empty())
Packit 2997f0
    Test::Fail("bad producer(w timeout): ClusterId should be empty, not " +
Packit 2997f0
               clusterid_bad_1);
Packit 2997f0
  std::string clusterid_bad_2 = p_bad->clusterid(0);
Packit 2997f0
  if (!clusterid_bad_2.empty())
Packit 2997f0
    Test::Fail("bad producer(0): ClusterId should be empty, not " +
Packit 2997f0
               clusterid_bad_2);
Packit 2997f0
Packit 2997f0
  delete p_good;
Packit 2997f0
  delete p_bad;
Packit 2997f0
}
Packit 2997f0
Packit 2997f0
extern "C" {
Packit 2997f0
  int main_0063_clusterid (int argc, char **argv) {
Packit 2997f0
    do_test_clusterid();
Packit 2997f0
    return 0;
Packit 2997f0
  }
Packit 2997f0
}