Parcourir la source

ZOOKEEPER-4556: Add zoo_version_str to return ZOO_VERSION for runtime library

ZOOKEEPER-4556: Add zoo_version_str to return ZOO_VERSION for runtime library
This method allows a calling program to determine at runtime whether the
version of the dynamically loaded zookeeper library (a.k.a. zoo_version_str())
is same as the version of the library the calling program was compiled against
(a.k.a. ZOO_VERSION).
Reviewers: eolivelli, kezhuw
Author: gjcarrette
Closes #1893 from gjcarrette/fix/ZOOKEEPER-4556
George J. Carrette il y a 1 semaine
Parent
commit
1ebd57b270

+ 13 - 0
zookeeper-client/zookeeper-client-c/include/zookeeper.h

@@ -733,6 +733,19 @@ ZOOAPI sasl_callback_t *zoo_sasl_make_password_callbacks(const char *user,
 
 #endif /* HAVE_CYRUS_SASL_H */
 
+
+/**
+ * \brief return ZOO_VERSION from loaded library as a string.
+ *
+ * This method allows a calling program to determine at runtime whether the
+ * version of the dynamically loaded zookeeper library (a.k.a. zoo_version_str())
+ * is same as the version of the library the calling program was compiled against
+ * (a.k.a. ZOO_VERSION).
+ *
+ * \return a string in shape "MAJOR.MINOR.PATCH", say "3.10.0"
+ */
+ZOOAPI const char* zoo_version_str();
+
 /**
  * \brief update the list of servers this client will connect to.
  *

+ 8 - 0
zookeeper-client/zookeeper-client-c/src/zookeeper.c

@@ -1295,6 +1295,14 @@ static void log_env(zhandle_t *zh) {
 #endif
 }
 
+/**
+ * Return string of "MAJOR.MINOR.PATCH"
+ */
+const char *zoo_version_str()
+{
+    return ZOO_VERSION;
+}
+
 /**
  * Create a zookeeper handle associated with the given host and port.
  */

+ 6 - 0
zookeeper-client/zookeeper-client-c/tests/TestZookeeperInit.cc

@@ -36,6 +36,7 @@ using namespace std;
 class Zookeeper_init : public CPPUNIT_NS::TestFixture
 {
     CPPUNIT_TEST_SUITE(Zookeeper_init);
+    CPPUNIT_TEST(testVersion);
     CPPUNIT_TEST(testBasic);
     CPPUNIT_TEST(testAddressResolution);
     CPPUNIT_TEST(testMultipleAddressResolution);
@@ -90,6 +91,11 @@ public:
 #endif
     }
 
+    void testVersion()
+    {
+        CPPUNIT_ASSERT_EQUAL(string(ZOO_VERSION), string(zoo_version_str()));
+    }
+
     void testBasic()
     {
         const string EXPECTED_HOST("127.0.0.1:2121");