Skip to content

Commit

Permalink
Merge pull request wazuh#24924 from wazuh/enhancement/24352_multithre…
Browse files Browse the repository at this point in the history
…ad-event-dispacher

Enhancement multithreading dispatching
  • Loading branch information
Dwordcito authored Aug 8, 2024
2 parents edbb13c + 36dfc9c commit 207873a
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 57 deletions.
47 changes: 43 additions & 4 deletions src/shared_modules/utils/tests/threadEventDispatcher_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void ThreadEventDispatcherTest::TearDown() {
};

constexpr auto BULK_SIZE {50};
TEST_F(ThreadEventDispatcherTest, Ctor)
TEST_F(ThreadEventDispatcherTest, ConstructorTestSingleThread)
{
static const std::vector<int> MESSAGES_TO_SEND_LIST {120, 100};

Expand Down Expand Up @@ -60,7 +60,47 @@ TEST_F(ThreadEventDispatcherTest, Ctor)
}
}

TEST_F(ThreadEventDispatcherTest, CtorNoWorker)
TEST_F(ThreadEventDispatcherTest, ConstructorTestMultiThread)
{
static const std::vector<int> MESSAGES_TO_SEND_LIST {120, 100};
static const auto NUM_THREADS = 4;

for (auto MESSAGES_TO_SEND : MESSAGES_TO_SEND_LIST)
{
std::atomic<size_t> counter {0};
std::promise<void> promise;
auto index {0};

ThreadEventDispatcher<std::string, std::function<void(std::queue<std::string>&)>, NUM_THREADS> dispatcher(
[&counter, &index, &MESSAGES_TO_SEND, &promise](std::queue<std::string>& data)
{
counter += data.size();
while (!data.empty())
{
auto value = data.front();
data.pop();
EXPECT_EQ(std::to_string(index), value);
++index;
}

if (counter == MESSAGES_TO_SEND)
{
promise.set_value();
}
},
"test.db",
BULK_SIZE);

for (int i = 0; i < MESSAGES_TO_SEND; ++i)
{
dispatcher.push(std::to_string(i));
}
promise.get_future().wait_for(std::chrono::seconds(10));
EXPECT_EQ(MESSAGES_TO_SEND, counter);
}
}

TEST_F(ThreadEventDispatcherTest, CtorNoWorkerSingleThread)
{
static const std::vector<int> MESSAGES_TO_SEND_LIST {120, 100};

Expand Down Expand Up @@ -101,7 +141,7 @@ TEST_F(ThreadEventDispatcherTest, CtorNoWorker)
}
}

TEST_F(ThreadEventDispatcherTest, CtorPopFeature)
TEST_F(ThreadEventDispatcherTest, CtorPopFeatureSingleThread)
{
constexpr auto MESSAGES_TO_SEND {1000};

Expand Down Expand Up @@ -144,4 +184,3 @@ TEST_F(ThreadEventDispatcherTest, CtorPopFeature)
promise.get_future().wait_for(std::chrono::seconds(10));
EXPECT_EQ(MESSAGES_TO_SEND, counter);
}

Loading

0 comments on commit 207873a

Please sign in to comment.