Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 0 additions & 31 deletions build-support/gen-pulsar-version-macro.py

This file was deleted.

25 changes: 21 additions & 4 deletions templates/Version.h.in → include/pulsar/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,29 @@
* specific language governing permissions and limitations
* under the License.
*/
#pragma once

#include <pulsar/defines.h>

#include <string>

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
4 changes: 2 additions & 2 deletions lib/Commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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());
Expand Down
3 changes: 1 addition & 2 deletions lib/HTTPLookupService.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
20 changes: 12 additions & 8 deletions tests/VersionTest.cc → lib/Version.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
* specific language governing permissions and limitations
* under the License.
*/
#include <gtest/gtest.h>
#include <pulsar/Version.h>

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
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<$<CONFIG:Debug>:${GMOCKD_LIBRARY_PATH}> $<$<CONFIG:Debug>:${GTESTD_LIBRARY_PATH}> $<$<NOT:$<CONFIG:Debug>>:${GMOCK_LIBRARY_PATH}> $<$<NOT:$<CONFIG:Debug>>:${GTEST_LIBRARY_PATH}>)

Expand Down
24 changes: 18 additions & 6 deletions tests/ClientTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <pulsar/Version.h>

#include <chrono>
#include <fstream>
#include <future>

#include "HttpHelper.h"
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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);
}