Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve issue #749 #752

Merged
merged 2 commits into from
Jun 8, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <unistd.h>
#include <semaphore.h>
#include <cstdlib>
#include <future>

#include <gtest/gtest.h>
#include "CelixEventAdminTestSuiteBaseClass.h"
Expand Down Expand Up @@ -605,36 +606,50 @@ TEST_F(CelixEventAdminTestSuite, PostEventWithInvalidArgumentsTest) {
});
}

static bool g_blockingHandlerCalled = false;
TEST_F(CelixEventAdminTestSuite, AsyncEventQueueFullTest) {
g_blockingHandlerCalled = true;
TestPublishEvent("org/celix/test", nullptr, [](celix_event_admin_t *ea) {
for (int i = 0; i < 512 + 1/*handling event*/; ++i) {
std::promise<void> promise1;
auto future1 = promise1.get_future();
std::promise<void> promise2;
auto future2 = promise2.get_future();
TestPublishEvent("org/celix/test", nullptr, [&promise2, &future1](celix_event_admin_t *ea) {
for (int i = 0; i < 512; ++i) {
auto status = celix_eventAdmin_postEvent(ea, "org/celix/test", nullptr);
EXPECT_EQ(CELIX_SUCCESS, status);
}
usleep(30000);
future1.get();
auto status = celix_eventAdmin_postEvent(ea, "org/celix/test", nullptr);
EXPECT_EQ(CELIX_SUCCESS, status);
status = celix_eventAdmin_postEvent(ea, "org/celix/test", nullptr);
EXPECT_EQ(CELIX_ILLEGAL_STATE, status);
g_blockingHandlerCalled = false;
}, [](void *handle, const char *topic, const celix_properties_t *props) {
promise2.set_value();
}, [&promise1, &future2](void *handle, const char *topic, const celix_properties_t *props) {
(void)handle;
(void)props;
(void)topic;
while (g_blockingHandlerCalled) usleep(1000);
static bool firstCalled{true};
if (firstCalled) {
promise1.set_value();
future2.get();
firstCalled = false;
}
return CELIX_SUCCESS;
});
}

TEST_F(CelixEventAdminTestSuite, RemoveEventHandlerAfterEventAdminStopTest) {
g_blockingHandlerCalled = true;
std::promise<void> promise;
auto future = promise.get_future();
celix_event_handler_service_t handler;
handler.handle = nullptr;
handler.handle = &future;
handler.handleEvent = [](void *handle, const char *topic, const celix_properties_t *props) {
(void)handle;
auto feature = static_cast<std::future<void>*>(handle);
(void)topic;
(void)props;
while (g_blockingHandlerCalled) usleep(1000);
static bool firstCalled{true};
if (firstCalled) {
feature->get();
firstCalled = false;
}
return CELIX_SUCCESS;
};
auto props = celix_properties_create();
Expand Down Expand Up @@ -669,7 +684,7 @@ TEST_F(CelixEventAdminTestSuite, RemoveEventHandlerAfterEventAdminStopTest) {
EXPECT_EQ(CELIX_SUCCESS, status);
}

g_blockingHandlerCalled = false;
promise.set_value();
status = celix_eventAdmin_stop(ea);
EXPECT_EQ(CELIX_SUCCESS, status);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef CELIX_CELIX_EVENT_ADMIN_TEST_SUITE_BASE_CLASS_H
#define CELIX_CELIX_EVENT_ADMIN_TEST_SUITE_BASE_CLASS_H

#include <functional>

#include "celix_event_admin.h"
#include "celix_event_handler_service.h"
Expand Down Expand Up @@ -137,16 +138,22 @@ class CelixEventAdminTestSuiteBaseClass : public ::testing::Test {
celix_eventAdmin_destroy(ea);
}

void TestPublishEvent(const char *handlerTopics, const char *eventFilter, void (*testBody)(celix_event_admin_t *ea),
celix_status_t (*onHandleEvent)(void *handle, const char *topic, const celix_properties_t *props), bool asyncUnordered = false) {
void TestPublishEvent(const char *handlerTopics, const char *eventFilter, std::function<void(celix_event_admin_t*)> testBody,
std::function<celix_status_t (void*, const char*, const celix_properties_t*)> onHandleEvent, bool asyncUnordered = false) {
auto ea = celix_eventAdmin_create(ctx.get());
EXPECT_TRUE(ea != nullptr);
auto status = celix_eventAdmin_start(ea);
EXPECT_EQ(CELIX_SUCCESS, status);

struct celix_handle_event_callback_data {
std::function<celix_status_t (void*, const char*, const celix_properties_t*)> onHandleEvent;
void* handle;
} data{onHandleEvent, ea};
celix_event_handler_service_t handler;
handler.handle = ea;
handler.handleEvent = onHandleEvent;
handler.handle = &data;
handler.handleEvent = [](void *handle, const char *topic, const celix_properties_t *props) {
auto data = static_cast<celix_handle_event_callback_data*>(handle);
return data->onHandleEvent(data->handle, topic, props);
};
auto props = celix_properties_create();
celix_properties_set(props, CELIX_FRAMEWORK_SERVICE_VERSION, CELIX_EVENT_HANDLER_SERVICE_VERSION);
celix_properties_set(props, CELIX_EVENT_TOPIC, handlerTopics);
Expand Down
Loading