This is an automated email from the ASF dual-hosted git repository.
zike pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pulsar-client-cpp.git
The following commit(s) were added to refs/heads/main by this push:
new 070e96f [improve] Refactor client version format (#206)
070e96f is described below
commit 070e96f49f327426a057177c3c2df310f4c7a136
Author: Zike Yang <[email protected]>
AuthorDate: Wed Mar 1 15:50:54 2023 +0800
[improve] Refactor client version format (#206)
## Motivation
Currently, the client_version only shows the version information. And
clients in different languages use their own separate version numbers. This can
lead to conflict. For example, the java client 2.10.2 uses the same value
2.10.2 as the C++ client 2.10.2. And C++ client 3.0.0 may conflict with the
future java client 3.0.0. The information of client_version is incomplete. We
could not determine what language(or specific library) the client uses. This
raises inconvenience for debugging.
Please see the discussion:
https://lists.apache.org/thread/n59k537fhthjnzkfxtc2p4zk4l0cv3mp
## Modification
* Change the client version value to Pulsar-C++-vXXX
---
lib/Commands.cc | 4 ++--
tests/ClientTest.cc | 33 +++++++++++++++++++++++++++++++++
tests/HttpHelper.cc | 25 +++++++++++++++++++++----
tests/HttpHelper.h | 1 +
4 files changed, 57 insertions(+), 6 deletions(-)
diff --git a/lib/Commands.cc b/lib/Commands.cc
index bfd9a46..a04a386 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(PULSAR_VERSION_STR);
+ connect->set_client_version(std::string("Pulsar-CPP-v") +
PULSAR_VERSION_STR);
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(PULSAR_VERSION_STR);
+ authResponse->set_client_version(std::string("Pulsar-CPP-v") +
PULSAR_VERSION_STR);
AuthData* authData = authResponse->mutable_response();
authData->set_auth_method_name(authentication->getAuthMethodName());
diff --git a/tests/ClientTest.cc b/tests/ClientTest.cc
index 07fe22f..d59633f 100644
--- a/tests/ClientTest.cc
+++ b/tests/ClientTest.cc
@@ -18,6 +18,7 @@
*/
#include <gtest/gtest.h>
#include <pulsar/Client.h>
+#include <pulsar/Version.h>
#include <chrono>
#include <future>
@@ -34,6 +35,7 @@ DECLARE_LOG_OBJECT()
using namespace pulsar;
static std::string lookupUrl = "pulsar://localhost:6650";
+static std::string adminUrl = "http://localhost:8080/";
TEST(ClientTest, testChecksumComputation) {
std::string data = "test";
@@ -308,3 +310,34 @@ TEST(ClientTest, testCloseClient) {
client.close();
}
}
+
+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;
+
+ Client client(lookupUrl);
+
+ std::string responseData;
+
+ Producer producer;
+ Result result = client.createProducer(topic, producer);
+ ASSERT_EQ(ResultOk, result);
+ int 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);
+ producer.close();
+
+ responseData.clear();
+ Consumer consumer;
+ result = client.subscribe(topic, "consumer-1", consumer);
+ ASSERT_EQ(ResultOk, result);
+ 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);
+ consumer.close();
+
+ client.close();
+}
diff --git a/tests/HttpHelper.cc b/tests/HttpHelper.cc
index c4118e6..69d5990 100644
--- a/tests/HttpHelper.cc
+++ b/tests/HttpHelper.cc
@@ -20,7 +20,13 @@
#include <curl/curl.h>
-static int makeRequest(const std::string& method, const std::string& url,
const std::string& body) {
+static size_t curlWriteCallback(void* contents, size_t size, size_t nmemb,
void* responseDataPtr) {
+ ((std::string*)responseDataPtr)->append((char*)contents, size * nmemb);
+ return size * nmemb;
+}
+
+static int makeRequest(const std::string& method, const std::string& url,
const std::string& body,
+ const std::string& responseData) {
CURL* curl = curl_easy_init();
struct curl_slist* list = NULL;
@@ -33,6 +39,11 @@ static int makeRequest(const std::string& method, const
std::string& url, const
if (!body.empty()) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
}
+
+ // Write callback
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseData);
+
int res = curl_easy_perform(curl);
curl_slist_free_all(list); /* free the list again */
@@ -46,10 +57,16 @@ static int makeRequest(const std::string& method, const
std::string& url, const
return (int)httpResult;
}
-int makePutRequest(const std::string& url, const std::string& body) { return
makeRequest("PUT", url, body); }
+int makePutRequest(const std::string& url, const std::string& body) {
+ return makeRequest("PUT", url, body, "");
+}
int makePostRequest(const std::string& url, const std::string& body) {
- return makeRequest("POST", url, body);
+ return makeRequest("POST", url, body, "");
}
-int makeDeleteRequest(const std::string& url) { return makeRequest("DELETE",
url, ""); }
+int makeDeleteRequest(const std::string& url) { return makeRequest("DELETE",
url, "", ""); }
+
+int makeGetRequest(const std::string& url, const std::string& responseData) {
+ return makeRequest("GET", url, "", responseData);
+}
diff --git a/tests/HttpHelper.h b/tests/HttpHelper.h
index 68119a7..1a03ac5 100644
--- a/tests/HttpHelper.h
+++ b/tests/HttpHelper.h
@@ -24,5 +24,6 @@
int makePutRequest(const std::string& url, const std::string& body);
int makePostRequest(const std::string& url, const std::string& body);
int makeDeleteRequest(const std::string& url);
+int makeGetRequest(const std::string& url, const std::string& responseData);
#endif /* end of include guard: HTTP_HELPER */