about summary refs log tree commit diff stats
path: root/archive/2025/summer/bsc_karidas/tests/integration/test_WriterQueue.cpp
diff options
context:
space:
mode:
authorDimitris <dimstav23@gmail.com>2025-07-15 15:11:36 +0200
committerGitHub <noreply@github.com>2025-07-15 15:11:36 +0200
commit73e505f04d17eba36c41fce7b48bc4d6884b8fd0 (patch)
tree44b5f4627309a48d6f22b54bb2ad9a2976e8601b /archive/2025/summer/bsc_karidas/tests/integration/test_WriterQueue.cpp
parentca92e7ad181a02890496872012ecc6c1d08b1658 (diff)
parentd8c365681a41961ebe2daea5701a4d56f5400d1d (diff)
downloadresearch-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.cpp117
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