diff options
| author | Dimitris <dimstav23@gmail.com> | 2025-07-15 15:11:36 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-15 15:11:36 +0200 |
| commit | 73e505f04d17eba36c41fce7b48bc4d6884b8fd0 (patch) | |
| tree | 44b5f4627309a48d6f22b54bb2ad9a2976e8601b /archive/2025/summer/bsc_karidas/tests/integration/test_WriterQueue.cpp | |
| parent | ca92e7ad181a02890496872012ecc6c1d08b1658 (diff) | |
| parent | d8c365681a41961ebe2daea5701a4d56f5400d1d (diff) | |
| download | research-work-archive-artifacts-73e505f04d17eba36c41fce7b48bc4d6884b8fd0.tar.gz research-work-archive-artifacts-73e505f04d17eba36c41fce7b48bc4d6884b8fd0.zip | |
Merge pull request #6 from chriskari/upload-artifacts
Add bsc_karidas
Diffstat (limited to 'archive/2025/summer/bsc_karidas/tests/integration/test_WriterQueue.cpp')
| -rw-r--r-- | archive/2025/summer/bsc_karidas/tests/integration/test_WriterQueue.cpp | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/archive/2025/summer/bsc_karidas/tests/integration/test_WriterQueue.cpp b/archive/2025/summer/bsc_karidas/tests/integration/test_WriterQueue.cpp new file mode 100644 index 000000000..00fff50f0 --- /dev/null +++ b/archive/2025/summer/bsc_karidas/tests/integration/test_WriterQueue.cpp @@ -0,0 +1,117 @@ +#include <gtest/gtest.h> +#include "Writer.hpp" +#include "BufferQueue.hpp" +#include "SegmentedStorage.hpp" +#include <chrono> +#include <thread> +#include <vector> +#include <filesystem> + +class WriterIntegrationTest : public ::testing::Test +{ +protected: + void SetUp() override + { + // Create a temporary directory for test log segments + testDir = "test_logs"; + std::filesystem::create_directories(testDir); + logQueue = std::make_unique<BufferQueue>(1024, 4); + + // Create a SegmentedStorage instance with reduced sizes for testing + storage = std::make_shared<SegmentedStorage>( + testDir, + "test_logsegment", + 1024 * 1024 // Maximum segment size (1 MB for testing) + ); + + writer = std::make_unique<Writer>(*logQueue, storage); + } + + void TearDown() override + { + if (writer) + { + writer->stop(); + } + std::filesystem::remove_all(testDir); + } + + std::unique_ptr<BufferQueue> logQueue; + std::unique_ptr<Writer> writer; + std::shared_ptr<SegmentedStorage> storage; + std::string testDir; + + QueueItem createTestItem(int id) + { + QueueItem item; + item.entry = LogEntry( + LogEntry::ActionType::UPDATE, + "location" + std::to_string(id), + "controller" + std::to_string(id), + "processor" + std::to_string(id), + "subject" + std::to_string(id % 10)); + return item; + } +}; + +// Test basic processing functionality +TEST_F(WriterIntegrationTest, BasicWriteOperation) +{ + BufferQueue::ProducerToken producerToken = logQueue->createProducerToken(); + const int NUM_ENTRIES = 500; + for (int i = 0; i < NUM_ENTRIES; ++i) + { + ASSERT_TRUE(logQueue->enqueueBlocking(createTestItem(i), producerToken, std::chrono::milliseconds(100))) + << "Failed to enqueue entry " << i; + } + + EXPECT_EQ(logQueue->size(), 500); + + writer->start(); + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + writer->stop(); + + EXPECT_EQ(logQueue->size(), 0) << "Not all entries were processed"; +} + +// Test concurrent writing and processing +TEST_F(WriterIntegrationTest, ConcurrentWriteAndProcess) +{ + const int NUM_ENTRIES = 1000; + const int NUM_PRODUCERS = 4; + + // Function to simulate producers adding log entries + auto producer = [this](int start, int count) + { + BufferQueue::ProducerToken producerToken = logQueue->createProducerToken(); + for (int i = start; i < start + count; ++i) + { + // Introduce a small delay to simulate variability + std::this_thread::sleep_for(std::chrono::milliseconds(rand() % 10)); + logQueue->enqueueBlocking(createTestItem(i), producerToken, std::chrono::milliseconds(500)); + } + }; + + writer->start(); + + std::vector<std::thread> producerThreads; + for (int i = 0; i < NUM_PRODUCERS; ++i) + { + producerThreads.emplace_back(producer, i * (NUM_ENTRIES / NUM_PRODUCERS), + NUM_ENTRIES / NUM_PRODUCERS); + } + + // Wait for all producer threads to finish + for (auto &t : producerThreads) + { + t.join(); + } + + // Allow some time for the writer to process the entries + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + writer->stop(); + + EXPECT_EQ(logQueue->size(), 0) << "Not all entries were processed"; +} \ No newline at end of file |