diff --git a/.gitignore b/.gitignore index ee4f28fb..9053c1f0 100644 --- a/.gitignore +++ b/.gitignore @@ -54,9 +54,6 @@ apache-pulsar-client-cpp-*.tar.gz /perf/perfConsumer /system-test/SystemTest -# Files generated from templates by CMAKE -include/pulsar/Version.h - # IDE generated files .csettings .cproject diff --git a/CMakeLists.txt b/CMakeLists.txt index a8f56aa9..7a76ec2c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,13 +25,8 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake_modules" execute_process(COMMAND cat ${PROJECT_SOURCE_DIR}/version.txt OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE PULSAR_CLIENT_VERSION) message(STATUS "Pulsar Client version: ${PULSAR_CLIENT_VERSION}") - -execute_process(COMMAND ${PROJECT_SOURCE_DIR}/build-support/gen-pulsar-version-macro.py OUTPUT_STRIP_TRAILING_WHITESPACE - OUTPUT_VARIABLE PULSAR_CLIENT_VERSION_MACRO) -message(STATUS "Pulsar Client version macro: ${PULSAR_CLIENT_VERSION_MACRO}") - -set(PVM_COMMENT "This is generated from Version.h.in by CMAKE. DO NOT EDIT DIRECTLY") -configure_file(templates/Version.h.in include/pulsar/Version.h @ONLY) +set(PULSAR_CLIENT_VERSION "Pulsar-CPP-v${PULSAR_CLIENT_VERSION}") +add_definitions(-DPULSAR_CLIENT_VERSION="${PULSAR_CLIENT_VERSION}") option(LINK_STATIC "Link against static libraries" OFF) if (VCPKG_TRIPLET) diff --git a/build-support/gen-pulsar-version-macro.py b/build-support/gen-pulsar-version-macro.py deleted file mode 100755 index 99c31b90..00000000 --- a/build-support/gen-pulsar-version-macro.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python3 -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# - -import re, sys, os - -dirname = os.path.abspath(os.path.dirname(sys.argv[0])) -version_file = os.path.join(dirname, "..", "version.txt") -version = open(version_file).read() -m = re.search(r'^(\d+)\.(\d+)\.(\d+)', version) - -version_macro = 0 -for i in range(3): - version_macro += int(m.group(3 - i)) * (1000 ** i) -print(version_macro) diff --git a/templates/Version.h.in b/include/pulsar/Version.h similarity index 57% rename from templates/Version.h.in rename to include/pulsar/Version.h index 7db246ad..c3283c3a 100644 --- a/templates/Version.h.in +++ b/include/pulsar/Version.h @@ -16,12 +16,29 @@ * specific language governing permissions and limitations * under the License. */ +#pragma once + +#include + +#include + +namespace pulsar { /** - * @PVM_COMMENT@ + * Get the client version string. */ -#pragma once +PULSAR_PUBLIC std::string getClientVersion(); -#define PULSAR_VERSION @PULSAR_CLIENT_VERSION_MACRO@ +/** + * Set the client version string. + * + * The client version will be sent to the broker as the identifier of the current client. The broker will + * show the client version when querying which clients are connected. + * + * @param version the custom version string + * + * NOTE: This function is not thread safe, so you have to call it before creating any `Client` instance. + */ +PULSAR_PUBLIC void setClientVersion(const std::string& version); -#define PULSAR_VERSION_STR "@PULSAR_CLIENT_VERSION@" +} // namespace pulsar diff --git a/lib/Commands.cc b/lib/Commands.cc index a04a386e..7c2b73da 100644 --- a/lib/Commands.cc +++ b/lib/Commands.cc @@ -266,7 +266,7 @@ SharedBuffer Commands::newConnect(const AuthenticationPtr& authentication, const BaseCommand cmd; cmd.set_type(BaseCommand::CONNECT); CommandConnect* connect = cmd.mutable_connect(); - connect->set_client_version(std::string("Pulsar-CPP-v") + PULSAR_VERSION_STR); + connect->set_client_version(getClientVersion()); connect->set_auth_method_name(authentication->getAuthMethodName()); connect->set_protocol_version(ProtocolVersion_MAX); @@ -294,7 +294,7 @@ SharedBuffer Commands::newAuthResponse(const AuthenticationPtr& authentication, BaseCommand cmd; cmd.set_type(BaseCommand::AUTH_RESPONSE); CommandAuthResponse* authResponse = cmd.mutable_authresponse(); - authResponse->set_client_version(std::string("Pulsar-CPP-v") + PULSAR_VERSION_STR); + authResponse->set_client_version(getClientVersion()); AuthData* authData = authResponse->mutable_response(); authData->set_auth_method_name(authentication->getAuthMethodName()); diff --git a/lib/HTTPLookupService.cc b/lib/HTTPLookupService.cc index 0ce68391..5854ce88 100644 --- a/lib/HTTPLookupService.cc +++ b/lib/HTTPLookupService.cc @@ -192,7 +192,6 @@ Result HTTPLookupService::sendHTTPRequest(std::string completeUrl, std::string & while (++reqCount <= maxLookupRedirects_) { CURL *handle; CURLcode res; - std::string version = std::string("Pulsar-CPP-v") + PULSAR_VERSION_STR; handle = curl_easy_init(); if (!handle) { @@ -218,7 +217,7 @@ Result HTTPLookupService::sendHTTPRequest(std::string completeUrl, std::string & curl_easy_setopt(handle, CURLOPT_TIMEOUT, lookupTimeoutInSeconds_); // Set User Agent - curl_easy_setopt(handle, CURLOPT_USERAGENT, version.c_str()); + curl_easy_setopt(handle, CURLOPT_USERAGENT, getClientVersion()); // Fail if HTTP return code >=400 curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1L); diff --git a/tests/VersionTest.cc b/lib/Version.cc similarity index 72% rename from tests/VersionTest.cc rename to lib/Version.cc index 0fa790dc..e3ce0e09 100644 --- a/tests/VersionTest.cc +++ b/lib/Version.cc @@ -16,14 +16,18 @@ * specific language governing permissions and limitations * under the License. */ -#include #include -TEST(VersionTest, testMacro) { -#ifdef PULSAR_VERSION - ASSERT_GE(PULSAR_VERSION, 2000000); - ASSERT_LE(PULSAR_VERSION, 999999999); -#else - FAIL(); +#ifndef PULSAR_CLIENT_VERSION +#define PULSAR_CLIENT_VERSION "Unknown" #endif -} + +namespace pulsar { + +static std::string gClientVersion = PULSAR_CLIENT_VERSION; + +std::string getClientVersion() { return gClientVersion; } + +void setClientVersion(const std::string& version) { gClientVersion = version; } + +} // namespace pulsar diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 05ca139c..9865f040 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -57,6 +57,7 @@ file(GLOB TEST_SOURCES *.cc c/*.cc) add_executable(pulsar-tests ${TEST_SOURCES} ${PROTO_SOURCES}) target_include_directories(pulsar-tests PRIVATE ${PROJECT_SOURCE_DIR}/lib ${AUTOGEN_DIR}/lib) +target_compile_definitions(pulsar-tests PRIVATE -D PULSAR_VERSION_FILE="${CMAKE_CURRENT_SOURCE_DIR}/../version.txt") target_link_libraries(pulsar-tests ${CLIENT_LIBS} pulsarStatic $<$:${GMOCKD_LIBRARY_PATH}> $<$:${GTESTD_LIBRARY_PATH}> $<$>:${GMOCK_LIBRARY_PATH}> $<$>:${GTEST_LIBRARY_PATH}>) diff --git a/tests/ClientTest.cc b/tests/ClientTest.cc index d59633f3..387bd633 100644 --- a/tests/ClientTest.cc +++ b/tests/ClientTest.cc @@ -21,6 +21,7 @@ #include #include +#include #include #include "HttpHelper.h" @@ -311,10 +312,7 @@ TEST(ClientTest, testCloseClient) { } } -TEST(ClientTest, testClientVersion) { - const std::string topic = "testClientVersion" + std::to_string(time(nullptr)); - const std::string expectedVersion = std::string("Pulsar-CPP-v") + PULSAR_VERSION_STR; - +static void testClientVersion(const std::string &topic) { Client client(lookupUrl); std::string responseData; @@ -326,7 +324,7 @@ TEST(ClientTest, testClientVersion) { makeGetRequest(adminUrl + "admin/v2/persistent/public/default/" + topic + "/stats", responseData); ASSERT_TRUE(res == 200) << "res: " << res; - ASSERT_TRUE(responseData.find(expectedVersion) != std::string::npos); + ASSERT_TRUE(responseData.find(getClientVersion()) != std::string::npos); producer.close(); responseData.clear(); @@ -336,8 +334,22 @@ TEST(ClientTest, testClientVersion) { res = makeGetRequest(adminUrl + "admin/v2/persistent/public/default/" + topic + "/stats", responseData); ASSERT_TRUE(res == 200) << "res: " << res; - ASSERT_TRUE(responseData.find(expectedVersion) != std::string::npos); + ASSERT_TRUE(responseData.find(getClientVersion()) != std::string::npos); consumer.close(); client.close(); } + +TEST(ClientTest, testClientVersion) { + const std::string topic = "testClientVersion" + std::to_string(time(nullptr)); + std::ifstream fin(PULSAR_VERSION_FILE); + std::string version; + fin >> version; + fin.close(); + ASSERT_EQ("Pulsar-CPP-v" + version, pulsar::getClientVersion()); + testClientVersion(topic); + + setClientVersion("custom-client-1.0.0"); + ASSERT_EQ("custom-client-1.0.0", pulsar::getClientVersion()); + testClientVersion(topic); +}