celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erjanalt...@apache.org
Subject celix git commit: Added test for etcdlib
Date Tue, 06 Feb 2018 15:39:00 GMT
Repository: celix
Updated Branches:
  refs/heads/develop 0a5ef69a6 -> ac36f55a0


Added test for etcdlib


Project: http://git-wip-us.apache.org/repos/asf/celix/repo
Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/ac36f55a
Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/ac36f55a
Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/ac36f55a

Branch: refs/heads/develop
Commit: ac36f55a0d3d084aa15818a21273c85484623e56
Parents: 0a5ef69
Author: Erjan Altena <erjan.altena@gmail.com>
Authored: Tue Feb 6 16:38:35 2018 +0100
Committer: Erjan Altena <erjan.altena@gmail.com>
Committed: Tue Feb 6 16:38:35 2018 +0100

----------------------------------------------------------------------
 etcdlib/CMakeLists.txt      |  4 ++
 etcdlib/src/etcd.c          |  1 -
 etcdlib/test/etcdlib_test.c | 98 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 102 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/ac36f55a/etcdlib/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/etcdlib/CMakeLists.txt b/etcdlib/CMakeLists.txt
index d9909dc..8c64858 100644
--- a/etcdlib/CMakeLists.txt
+++ b/etcdlib/CMakeLists.txt
@@ -61,6 +61,7 @@ target_link_libraries(etcdlib ${CURL_LIBRARIES} ${JANSSON_LIBRARIES})
 add_library(etcdlib_static STATIC
     src/etcd.c
 )
+
 target_include_directories(etcdlib_static PUBLIC 
     $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/api>
     $<INSTALL_INTERFACE:include/etcdlib>
@@ -73,6 +74,9 @@ target_include_directories(etcdlib_static PRIVATE
 set_target_properties(etcdlib_static PROPERTIES "SOVERSION" 1)
 target_link_libraries(etcdlib_static ${CURL_LIBRARIES} ${JANSSON_LIBRARY})
 
+add_executable(etcdlib_test ${CMAKE_CURRENT_SOURCE_DIR}/test/etcdlib_test.c)
+target_link_libraries(etcdlib_test etcdlib_static)
+
 install(DIRECTORY api/ DESTINATION include/etcdlib COMPONENT ${ETCDLIB_CMP})
 if (NOT COMMAND celix_subproject) 
     install(TARGETS etcdlib etcdlib_static DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT
${ETCDLIB_CMP})

http://git-wip-us.apache.org/repos/asf/celix/blob/ac36f55a/etcdlib/src/etcd.c
----------------------------------------------------------------------
diff --git a/etcdlib/src/etcd.c b/etcdlib/src/etcd.c
index ff9f827..f75fa03 100644
--- a/etcdlib/src/etcd.c
+++ b/etcdlib/src/etcd.c
@@ -259,7 +259,6 @@ int etcd_set(const char* key, const char* value, int ttl, bool prevExist)
{
 	}
 
 	res = performRequest(url, PUT, WriteMemoryCallback, request, (void*) &reply);
-
 	if(url) {
 		free(url);
 	}

http://git-wip-us.apache.org/repos/asf/celix/blob/ac36f55a/etcdlib/test/etcdlib_test.c
----------------------------------------------------------------------
diff --git a/etcdlib/test/etcdlib_test.c b/etcdlib/test/etcdlib_test.c
new file mode 100644
index 0000000..200b1a2
--- /dev/null
+++ b/etcdlib/test/etcdlib_test.c
@@ -0,0 +1,98 @@
+/**
+ *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.
+ */
+
+/**
+ * Test program for testing the etcdlib.
+ * Prerequisite is that etcdlib is started on localhost on default port (2379)
+ * tested with etcd 2.3.7
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <curl/curl.h>
+#include "etcd.h"
+
+#include <pthread.h>
+
+int simplewritetest() {
+    int res = 0;
+    char*value = NULL;
+    etcd_set("simplekey", "testvalue", 5, false);
+    etcd_get("simplekey", &value, NULL);
+    if (value && strcmp(value, "testvalue")) {
+        printf("etcdlib test error: expected testvalue got %s\n", value);
+        res = -1;
+    }
+    return res;
+}
+
+void* waitForChange(void*arg) {
+    int *idx = (int*)arg;
+    char *action = NULL;
+    char *prevValue;
+    char *value;
+    char *rkey;
+    long long modifiedIndex;
+    printf("Watching for index %d\n", *idx);
+    etcd_watch("hier/ar", *idx, &action, &prevValue, &value, &rkey, &modifiedIndex);
+    printf(" New value from watch : [%s]%s => %s\n", rkey, prevValue, value);
+    *idx = modifiedIndex+1;
+    etcd_watch("hier/ar", *idx, &action, &prevValue, &value, &rkey, &modifiedIndex);
+    printf(" New value from watch : [%s]%s => %s\n", rkey, prevValue, value);
+    free (action);
+    free(prevValue);
+    free(rkey);
+    return value;
+}
+
+int waitforchangetest() {
+    int res = 0;
+    char*value = NULL;
+
+    etcd_set("hier/ar/chi/cal", "testvalue1", 5, false);
+
+    int index;
+    etcd_get("hier/ar/chi/cal", &value, &index);
+    pthread_t waitThread;
+    index++;
+    pthread_create(&waitThread, NULL, waitForChange, &index);
+    sleep(1);
+    etcd_set("hier/ar/chi/cal", "testvalue2", 5, false);
+    sleep(1);
+    etcd_set("hier/ar/chi/cal", "testvalue3", 5, false);
+    void *resVal;
+    pthread_join(waitThread, &resVal);
+    if(strcmp((char*)resVal,"testvalue3" )) {
+        printf("etcdtest::waitforchange1 expected testvalue3, got %s\n", (char*)resVal);
+        res = -1;
+    }
+    return res;
+}
+
+int main (void) {
+    etcd_init("localhost", 2379, 0);
+
+    int res = simplewritetest(); if(res) return res; else printf("simplewrite test success\n");
+    res = waitforchangetest(); if(res) return res;else printf("waitforchange1 test success\n");
+
+    return 0;
+}
+


Mime
View raw message