celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pnol...@apache.org
Subject [celix] branch feature/handle_hooks_in_registry updated: Refactors pubsub test to use a service instead of global.
Date Mon, 16 Sep 2019 13:00:53 GMT
This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/handle_hooks_in_registry
in repository https://gitbox.apache.org/repos/asf/celix.git


The following commit(s) were added to refs/heads/feature/handle_hooks_in_registry by this
push:
     new 6fe7eec  Refactors pubsub test to use a service instead of global.
6fe7eec is described below

commit 6fe7eecd11f904c32e76df63db5a08919672b094
Author: Pepijn Noltes <pepijnnoltes@gmail.com>
AuthorDate: Mon Sep 16 15:00:00 2019 +0200

    Refactors pubsub test to use a service instead of global.
    
    Maybe this solve the problem of unstable pubsub test (global with possible race condition)
---
 bundles/pubsub/test/CMakeLists.txt                 | 16 ++---
 .../{test_runner.cc => receive_count_service.h}    | 23 +++----
 bundles/pubsub/test/test/test_runner.cc            | 50 +++++++++++---
 .../test/{tst_activator.cc => tst_activator.c}     | 79 ++++++++-------------
 ...point_activator.cc => tst_endpoint_activator.c} | 80 +++++++++-------------
 5 files changed, 118 insertions(+), 130 deletions(-)

diff --git a/bundles/pubsub/test/CMakeLists.txt b/bundles/pubsub/test/CMakeLists.txt
index f2f9374..7820dfa 100644
--- a/bundles/pubsub/test/CMakeLists.txt
+++ b/bundles/pubsub/test/CMakeLists.txt
@@ -38,7 +38,7 @@ celix_bundle_files(pubsub_endpoint_sut
 add_celix_bundle(pubsub_endpoint_tst
         #Test bundle containing cpputests and uses celix_test_runner launcher instead of
the celix launcher
         SOURCES
-        test/tst_endpoint_activator.cc
+        test/tst_endpoint_activator.c
         VERSION 1.0.0
         )
 target_link_libraries(pubsub_endpoint_tst PRIVATE Celix::framework Celix::pubsub_api)
@@ -93,7 +93,7 @@ celix_bundle_files(pubsub_sut
 add_celix_bundle(pubsub_tst
     #Test bundle containing cpputests and uses celix_test_runner launcher instead of the
celix launcher
     SOURCES
-        test/tst_activator.cc
+        test/tst_activator.c
     VERSION 1.0.0
 )
 target_link_libraries(pubsub_tst PRIVATE Celix::framework Celix::pubsub_api)
@@ -121,7 +121,7 @@ add_celix_container(pubsub_udpmc_tests
             pubsub_tst
 )
 target_link_libraries(pubsub_udpmc_tests PRIVATE Celix::pubsub_api ${CPPUTEST_LIBRARIES}
${JANSSON_LIBRARIES} Celix::dfi)
-target_include_directories(pubsub_udpmc_tests PRIVATE ${CPPUTEST_INCLUDE_DIR})
+target_include_directories(pubsub_udpmc_tests PRIVATE ${CPPUTEST_INCLUDE_DIR} test)
 message(WARNING "TODO fix issues with UDPMC and reanble test again")
 #add_test(NAME pubsub_udpmc_tests COMMAND pubsub_udpmc_tests WORKING_DIRECTORY $<TARGET_PROPERTY:pubsub_udpmc_tests,CONTAINER_LOC>)
 #SETUP_TARGET_FOR_COVERAGE(pubsub_udpmc_tests_cov pubsub_udpmc_tests ${CMAKE_BINARY_DIR}/coverage/pubsub_udpmc_tests/pubsub_udpmc_tests
..)
@@ -142,7 +142,7 @@ add_celix_container(pubsub_tcp_tests
         pubsub_tst
         )
 target_link_libraries(pubsub_tcp_tests PRIVATE Celix::pubsub_api ${CPPUTEST_LIBRARIES} ${JANSSON_LIBRARIES}
Celix::dfi)
-target_include_directories(pubsub_tcp_tests PRIVATE ${CPPUTEST_INCLUDE_DIR})
+target_include_directories(pubsub_tcp_tests PRIVATE ${CPPUTEST_INCLUDE_DIR} test)
 add_test(NAME pubsub_tcp_tests COMMAND pubsub_tcp_tests WORKING_DIRECTORY $<TARGET_PROPERTY:pubsub_tcp_tests,CONTAINER_LOC>)
 SETUP_TARGET_FOR_COVERAGE(pubsub_tcp_tests_cov pubsub_tcp_tests ${CMAKE_BINARY_DIR}/coverage/pubsub_tcp_tests/pubsub_tcp_tests
..)
 
@@ -164,7 +164,7 @@ add_celix_container(pubsub_tcp_endpoint_tests
         pubsub_endpoint_tst
         )
 target_link_libraries(pubsub_tcp_endpoint_tests PRIVATE Celix::pubsub_api ${CPPUTEST_LIBRARIES}
${JANSSON_LIBRARIES} Celix::dfi)
-target_include_directories(pubsub_tcp_endpoint_tests PRIVATE ${CPPUTEST_INCLUDE_DIR})
+target_include_directories(pubsub_tcp_endpoint_tests PRIVATE ${CPPUTEST_INCLUDE_DIR} test)
 
 #TCP Endpoint test is disabled because the test is not stable when running on Travis
 if (ENABLE_PUBSUB_PSA_TCP_ENDPOINT_TEST)
@@ -189,7 +189,7 @@ add_celix_container(pubsub_websocket_tests
             pubsub_tst
 )
 target_link_libraries(pubsub_websocket_tests PRIVATE Celix::pubsub_api ${CPPUTEST_LIBRARIES}
${JANSSON_LIBRARIES} Celix::dfi)
-target_include_directories(pubsub_websocket_tests PRIVATE ${CPPUTEST_INCLUDE_DIR})
+target_include_directories(pubsub_websocket_tests PRIVATE ${CPPUTEST_INCLUDE_DIR} test)
 add_test(NAME pubsub_websocket_tests COMMAND pubsub_websocket_tests WORKING_DIRECTORY $<TARGET_PROPERTY:pubsub_websocket_tests,CONTAINER_LOC>)
 SETUP_TARGET_FOR_COVERAGE(pubsub_websocket_tests_cov pubsub_websocket_tests ${CMAKE_BINARY_DIR}/coverage/pubsub_websocket_tests/pubsub_websocket_tests
..)
 
@@ -209,7 +209,7 @@ if (BUILD_PUBSUB_PSA_ZMQ)
     )
 
     target_link_libraries(pubsub_zmq_tests PRIVATE Celix::pubsub_api ${CPPUTEST_LIBRARIES}
${JANSSON_LIBRARIES} Celix::dfi)
-    target_include_directories(pubsub_zmq_tests PRIVATE ${CPPUTEST_INCLUDE_DIR})
+    target_include_directories(pubsub_zmq_tests PRIVATE ${CPPUTEST_INCLUDE_DIR} test)
     add_test(NAME pubsub_zmq_tests COMMAND pubsub_zmq_tests WORKING_DIRECTORY $<TARGET_PROPERTY:pubsub_zmq_tests,CONTAINER_LOC>)
     SETUP_TARGET_FOR_COVERAGE(pubsub_zmq_tests_cov pubsub_zmq_tests ${CMAKE_BINARY_DIR}/coverage/pubsub_zmq_tests/pubsub_zmq_tests
..)
 
@@ -229,7 +229,7 @@ if (BUILD_PUBSUB_PSA_ZMQ)
                 pubsub_tst
     )
     target_link_libraries(pubsub_zmq_zerocopy_tests PRIVATE Celix::pubsub_api ${CPPUTEST_LIBRARIES}
${JANSSON_LIBRARIES} Celix::dfi)
-    target_include_directories(pubsub_zmq_zerocopy_tests PRIVATE ${CPPUTEST_INCLUDE_DIR})
+    target_include_directories(pubsub_zmq_zerocopy_tests PRIVATE ${CPPUTEST_INCLUDE_DIR}
test)
     add_test(NAME pubsub_zmq_zerocopy_tests COMMAND pubsub_zmq_zerocopy_tests WORKING_DIRECTORY
$<TARGET_PROPERTY:pubsub_zmq_zerocopy_tests,CONTAINER_LOC>)
     SETUP_TARGET_FOR_COVERAGE(pubsub_zmq_zerocopy_tests_cov pubsub_zmq_zerocopy_tests ${CMAKE_BINARY_DIR}/coverage/pubsub_zmq_tests/pubsub_zmq_zerocopy_tests
..)
 endif ()
diff --git a/bundles/pubsub/test/test/test_runner.cc b/bundles/pubsub/test/test/receive_count_service.h
similarity index 65%
copy from bundles/pubsub/test/test/test_runner.cc
copy to bundles/pubsub/test/test/receive_count_service.h
index 693c400..8f86c80 100644
--- a/bundles/pubsub/test/test/test_runner.cc
+++ b/bundles/pubsub/test/test/receive_count_service.h
@@ -17,21 +17,14 @@
  * under the License.
  */
 
-#include "celix_api.h"
+#ifndef CELIX_RECEIVE_COUNT_SERVICE_H
+#define CELIX_RECEIVE_COUNT_SERVICE_H
 
-#include <CppUTest/TestHarness.h>
-#include <CppUTest/CommandLineTestRunner.h>
+#define CELIX_RECEIVE_COUNT_SERVICE_NAME "celix_receive_count_service"
 
-int main(int argc, char **argv) {
-    celix_framework_t *fw = NULL;
-    celixLauncher_launch("config.properties", &fw);
+typedef struct celix_receive_count_service {
+    void *handle;
+    size_t (*receiveCount)(void *handle);
+} celix_receive_count_service_t;
 
-    MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
-    int rc = RUN_ALL_TESTS(argc, argv);
-
-    celixLauncher_stop(fw);
-    celixLauncher_waitForShutdown(fw);
-    celixLauncher_destroy(fw);
-
-    return rc;
-}
+#endif //CELIX_RECEIVE_COUNT_SERVICE_H
diff --git a/bundles/pubsub/test/test/test_runner.cc b/bundles/pubsub/test/test/test_runner.cc
index 693c400..9846d02 100644
--- a/bundles/pubsub/test/test/test_runner.cc
+++ b/bundles/pubsub/test/test/test_runner.cc
@@ -18,20 +18,54 @@
  */
 
 #include "celix_api.h"
+#include <unistd.h>
+#include "receive_count_service.h"
 
 #include <CppUTest/TestHarness.h>
 #include <CppUTest/CommandLineTestRunner.h>
 
 int main(int argc, char **argv) {
-    celix_framework_t *fw = NULL;
-    celixLauncher_launch("config.properties", &fw);
-
     MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
     int rc = RUN_ALL_TESTS(argc, argv);
-
-    celixLauncher_stop(fw);
-    celixLauncher_waitForShutdown(fw);
-    celixLauncher_destroy(fw);
-
     return rc;
 }
+
+TEST_GROUP(PUBSUB_INT_GROUP) {
+    celix_framework_t *fw = NULL;
+    celix_bundle_context_t *ctx = NULL;
+    void setup() {
+        celixLauncher_launch("config.properties", &fw);
+        ctx = celix_framework_getFrameworkContext(fw);
+    }
+
+        void teardown() {
+            celixLauncher_stop(fw);
+            celixLauncher_waitForShutdown(fw);
+            celixLauncher_destroy(fw);
+            ctx = NULL;
+            fw = NULL;
+        }
+        };
+
+TEST(PUBSUB_INT_GROUP, recvTest) {
+    constexpr int TRIES = 25;
+    constexpr int TIMEOUT = 250000;
+    constexpr int MSG_COUNT = 100;
+
+    int count = 0;
+
+    for (int i = 0; i < TRIES; ++i) {
+        count = 0;
+        celix_bundleContext_useService(ctx, CELIX_RECEIVE_COUNT_SERVICE_NAME, &count,
[](void *handle, void *svc) {
+            auto* count_ptr = static_cast<int*>(handle);
+            auto* count = static_cast<celix_receive_count_service_t*>(svc);
+            *count_ptr = count->receiveCount(count->handle);
+        });
+        printf("Current msg count is %i, waiting for at least %i\n", count, MSG_COUNT);
+        if (count >= MSG_COUNT) {
+            break;
+        }
+        usleep(TIMEOUT);
+    }
+    CHECK(count >= MSG_COUNT);
+}
\ No newline at end of file
diff --git a/bundles/pubsub/test/test/tst_activator.cc b/bundles/pubsub/test/test/tst_activator.c
similarity index 58%
rename from bundles/pubsub/test/test/tst_activator.cc
rename to bundles/pubsub/test/test/tst_activator.c
index 4430adb..5a2ca21 100644
--- a/bundles/pubsub/test/test/tst_activator.cc
+++ b/bundles/pubsub/test/test/tst_activator.c
@@ -26,41 +26,45 @@
 #include "pubsub/api.h"
 
 #include "msg.h"
-
-#include <CppUTest/TestHarness.h>
-#include <CppUTestExt/MockSupport.h>
-#include <constants.h>
-
-extern "C" {
+#include "receive_count_service.h"
 
 static int tst_receive(void *handle, const char *msgType, unsigned int msgTypeId, void *msg,
bool *release);
+static size_t tst_count(void *handle);
 
 struct activator {
     pubsub_subscriber_t subSvc;
     long subSvcId;
 
+    celix_receive_count_service_t countSvc;
+    long countSvcId;
+
     pthread_mutex_t mutex;
-    unsigned int count = 0;
+    unsigned int count;
 };
 
-static struct activator *g_act = NULL; //global
-
 celix_status_t bnd_start(struct activator *act, celix_bundle_context_t *ctx) {
     pthread_mutex_init(&act->mutex, NULL);
 
-    celix_properties_t *props = celix_properties_create();
-    celix_properties_set(props, PUBSUB_SUBSCRIBER_TOPIC, "ping");
-    act->subSvc.handle = act;
-    act->subSvc.receive = tst_receive;
-    act->subSvcId = celix_bundleContext_registerService(ctx, &act->subSvc, PUBSUB_SUBSCRIBER_SERVICE_NAME,
props);
+    {
+        celix_properties_t *props = celix_properties_create();
+        celix_properties_set(props, PUBSUB_SUBSCRIBER_TOPIC, "ping");
+        act->subSvc.handle = act;
+        act->subSvc.receive = tst_receive;
+        act->subSvcId = celix_bundleContext_registerService(ctx, &act->subSvc,
PUBSUB_SUBSCRIBER_SERVICE_NAME, props);
+    }
 
-    g_act = act;
+    {
+        act->countSvc.handle = act;
+        act->countSvc.receiveCount = tst_count;
+        act->countSvcId = celix_bundleContext_registerService(ctx, &act->countSvc,
CELIX_RECEIVE_COUNT_SERVICE_NAME, NULL);
+    }
 
     return CELIX_SUCCESS;
 }
 
 celix_status_t bnd_stop(struct activator *act, celix_bundle_context_t *ctx) {
     celix_bundleContext_unregisterService(ctx, act->subSvcId);
+    celix_bundleContext_unregisterService(ctx, act->countSvcId);
     pthread_mutex_destroy(&act->mutex);
     return CELIX_SUCCESS;
 }
@@ -68,10 +72,10 @@ celix_status_t bnd_stop(struct activator *act, celix_bundle_context_t
*ctx) {
 CELIX_GEN_BUNDLE_ACTIVATOR(struct activator, bnd_start, bnd_stop) ;
 
 
-static int tst_receive(void *handle, const char * /*msgType*/, unsigned int /*msgTypeId*/,
void * voidMsg, bool */*release*/) {
-    struct activator *act = static_cast<struct activator *>(handle);
+static int tst_receive(void *handle, const char * msgType __attribute__((unused)), unsigned
int msgTypeId  __attribute__((unused)), void * voidMsg, bool *release  __attribute__((unused)))
{
+    struct activator *act = handle;
 
-    msg_t *msg = static_cast<msg_t*>(voidMsg);
+    msg_t *msg = voidMsg;
     static int prevSeqNr = 0;
     int delta = msg->seqNr - prevSeqNr;
     if (delta != 1) {
@@ -85,36 +89,11 @@ static int tst_receive(void *handle, const char * /*msgType*/, unsigned
int /*ms
     return CELIX_SUCCESS;
 }
 
-} //end extern C
-
-TEST_GROUP(PUBSUB_INT_GROUP)
-{
-    void setup() {
-        //nop
-    }
-
-    void teardown() {
-        //nop
-    }
-};
-
-TEST(PUBSUB_INT_GROUP, recvTest) {
-    constexpr int TRIES = 25;
-    constexpr int TIMEOUT = 250000;
-    constexpr int MSG_COUNT = 100;
-
-    int count = 0;
-
-    for (int i = 0; i < TRIES; ++i) {
-        pthread_mutex_lock(&g_act->mutex);
-        count = g_act->count;
-        pthread_mutex_unlock(&g_act->mutex);
-        printf("Current msg count is %i, waiting for at least %i\n", count, MSG_COUNT);
-        if (count >= MSG_COUNT) {
-            break;
-        }
-        usleep(TIMEOUT);
-    }
-    CHECK(count >= MSG_COUNT);
-
+static size_t tst_count(void *handle) {
+    struct activator *act = handle;
+    size_t count;
+    pthread_mutex_lock(&act->mutex);
+    count = act->count;
+    pthread_mutex_unlock(&act->mutex);
+    return count;
 }
diff --git a/bundles/pubsub/test/test/tst_endpoint_activator.cc b/bundles/pubsub/test/test/tst_endpoint_activator.c
similarity index 58%
rename from bundles/pubsub/test/test/tst_endpoint_activator.cc
rename to bundles/pubsub/test/test/tst_endpoint_activator.c
index 652a9af..cf9f6cb 100644
--- a/bundles/pubsub/test/test/tst_endpoint_activator.cc
+++ b/bundles/pubsub/test/test/tst_endpoint_activator.c
@@ -26,41 +26,48 @@
 #include "pubsub/api.h"
 
 #include "msg.h"
+#include "receive_count_service.h"
 
-#include <CppUTest/TestHarness.h>
-#include <CppUTestExt/MockSupport.h>
-#include <constants.h>
-
-extern "C" {
 
 static int tst_receive(void *handle, const char *msgType, unsigned int msgTypeId, void *msg,
bool *release);
+static size_t tst_count(void *handle);
 
 struct activator {
     pubsub_subscriber_t subSvc;
     long subSvcId;
 
+    celix_receive_count_service_t countSvc;
+    long countSvcId;
+
     pthread_mutex_t mutex;
-    unsigned int count = 0;
+    unsigned int count;
 };
 
-static struct activator *g_act = NULL; //global
-
 celix_status_t bnd_start(struct activator *act, celix_bundle_context_t *ctx) {
     pthread_mutex_init(&act->mutex, NULL);
 
-    celix_properties_t *props = celix_properties_create();
-    celix_properties_set(props, PUBSUB_SUBSCRIBER_TOPIC, "ping2");
-    act->subSvc.handle = act;
-    act->subSvc.receive = tst_receive;
-    act->subSvcId = celix_bundleContext_registerService(ctx, &act->subSvc, PUBSUB_SUBSCRIBER_SERVICE_NAME,
props);
+    {
+        celix_properties_t *props = celix_properties_create();
+        celix_properties_set(props, PUBSUB_SUBSCRIBER_TOPIC, "ping2");
+        act->subSvc.handle = act;
+        act->subSvc.receive = tst_receive;
+        act->subSvcId = celix_bundleContext_registerService(ctx, &act->subSvc,
PUBSUB_SUBSCRIBER_SERVICE_NAME, props);
+    }
+
+
+    {
+        act->countSvc.handle = act;
+        act->countSvc.receiveCount = tst_count;
+        act->countSvcId = celix_bundleContext_registerService(ctx, &act->countSvc,
CELIX_RECEIVE_COUNT_SERVICE_NAME, NULL);
+    }
 
-    g_act = act;
 
     return CELIX_SUCCESS;
 }
 
 celix_status_t bnd_stop(struct activator *act, celix_bundle_context_t *ctx) {
     celix_bundleContext_unregisterService(ctx, act->subSvcId);
+    celix_bundleContext_unregisterService(ctx, act->countSvcId);
     pthread_mutex_destroy(&act->mutex);
     return CELIX_SUCCESS;
 }
@@ -68,10 +75,10 @@ celix_status_t bnd_stop(struct activator *act, celix_bundle_context_t
*ctx) {
 CELIX_GEN_BUNDLE_ACTIVATOR(struct activator, bnd_start, bnd_stop) ;
 
 
-static int tst_receive(void *handle, const char * /*msgType*/, unsigned int /*msgTypeId*/,
void * voidMsg, bool */*release*/) {
-    struct activator *act = static_cast<struct activator *>(handle);
+static int tst_receive(void *handle, const char * msgType __attribute__((unused)), unsigned
int msgTypeId  __attribute__((unused)), void * voidMsg, bool *release  __attribute__((unused)))
{
+    struct activator *act = handle;
 
-    msg_t *msg = static_cast<msg_t*>(voidMsg);
+    msg_t *msg = voidMsg;
     static int prevSeqNr = 0;
     int delta = msg->seqNr - prevSeqNr;
     if (delta != 1) {
@@ -85,36 +92,11 @@ static int tst_receive(void *handle, const char * /*msgType*/, unsigned
int /*ms
     return CELIX_SUCCESS;
 }
 
-} //end extern C
-
-TEST_GROUP(PUBSUB_INT_GROUP)
-{
-	void setup() {
-	    //nop
-	}
-
-	void teardown() {
-		//nop
-	}
-};
-
-TEST(PUBSUB_INT_GROUP, recvTest) {
-    constexpr int TRIES = 25;
-    constexpr int TIMEOUT = 250000;
-    constexpr int MSG_COUNT = 100;
-
-    int count = 0;
-
-    for (int i = 0; i < TRIES; ++i) {
-        pthread_mutex_lock(&g_act->mutex);
-        count = g_act->count;
-        pthread_mutex_unlock(&g_act->mutex);
-        printf("Current msg count is %i, waiting for at least %i\n", count, MSG_COUNT);
-        if (count >= MSG_COUNT) {
-            break;
-        }
-        usleep(TIMEOUT);
-    }
-    CHECK(count >= MSG_COUNT);
-
+static size_t tst_count(void *handle) {
+    struct activator *act = handle;
+    size_t count;
+    pthread_mutex_lock(&act->mutex);
+    count = act->count;
+    pthread_mutex_unlock(&act->mutex);
+    return count;
 }


Mime
View raw message