Implement FileCollector with concurrency and stress tests
This commit is contained in:
parent
d09fb340b7
commit
4c05fe8034
26 changed files with 1745 additions and 0 deletions
38
tests/CMakeLists.txt
Normal file
38
tests/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
find_package(GTest CONFIG QUIET)
|
||||
|
||||
if(NOT GTest_FOUND)
|
||||
include(FetchContent)
|
||||
set(FILE_COLLECTOR_GTEST_SOURCE_DIR "" CACHE PATH "Existing local googletest source checkout")
|
||||
if(FILE_COLLECTOR_GTEST_SOURCE_DIR)
|
||||
add_subdirectory("${FILE_COLLECTOR_GTEST_SOURCE_DIR}"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/googletest" EXCLUDE_FROM_ALL)
|
||||
else()
|
||||
FetchContent_Declare(googletest
|
||||
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
|
||||
)
|
||||
FetchContent_MakeAvailable(googletest)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_executable(file_collector_tests
|
||||
collector_tests.cpp
|
||||
chunk_tests.cpp
|
||||
boundary_tests.cpp
|
||||
concurrency_tests.cpp
|
||||
lifecycle_tests.cpp
|
||||
snapshot_tests.cpp
|
||||
exception_tests.cpp
|
||||
thread_pool_tests.cpp
|
||||
stress_tests.cpp
|
||||
performance_tests.cpp
|
||||
)
|
||||
target_include_directories(file_collector_tests PRIVATE "${PROJECT_SOURCE_DIR}/src")
|
||||
target_link_libraries(file_collector_tests PRIVATE file_collector GTest::gtest_main Threads::Threads)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(file_collector_tests
|
||||
DISCOVERY_MODE PRE_TEST
|
||||
DISCOVERY_TIMEOUT 60
|
||||
PROPERTIES TIMEOUT 120
|
||||
)
|
||||
|
||||
45
tests/boundary_tests.cpp
Normal file
45
tests/boundary_tests.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include "test_data.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <limits>
|
||||
|
||||
using namespace file_collector;
|
||||
|
||||
TEST(Boundary, UnknownIdRejectedEverywhere) {
|
||||
FileCollector collector(1);
|
||||
EXPECT_THROW(collector.OnNewChunk(99, 0, {}), std::out_of_range);
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(99)), std::out_of_range);
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(99, 0, 0)), std::out_of_range);
|
||||
EXPECT_THROW(static_cast<void>(collector.TakeFile(99)), std::out_of_range);
|
||||
EXPECT_THROW(collector.RemoveFile(99), std::out_of_range);
|
||||
EXPECT_THROW(collector.ClearFile(99), std::out_of_range);
|
||||
}
|
||||
|
||||
TEST(Boundary, OffsetsCheckedWithoutOverflow) {
|
||||
FileCollector collector(1);
|
||||
collector.CollectFile(1, 4);
|
||||
EXPECT_THROW(collector.OnNewChunk(1, 5, {}), std::out_of_range);
|
||||
EXPECT_THROW(collector.OnNewChunk(1, 3, FileBytes{1, 2}), std::out_of_range);
|
||||
EXPECT_THROW(collector.OnNewChunk(1, std::numeric_limits<std::size_t>::max(), {}), std::out_of_range);
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(1, 3, 2, false)), std::out_of_range);
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(1, 5, 0, false)), std::out_of_range);
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(1, std::numeric_limits<std::size_t>::max(), 1, false)), std::out_of_range);
|
||||
EXPECT_EQ(collector.GetFile(1, 4, 0)->size(), std::size_t{0});
|
||||
}
|
||||
|
||||
TEST(Boundary, DuplicateRegistrationRejectedUntilRemoved) {
|
||||
FileCollector collector(1);
|
||||
collector.CollectFile(1, 1);
|
||||
EXPECT_THROW(collector.CollectFile(1, 9), std::logic_error);
|
||||
collector.RemoveFile(1);
|
||||
EXPECT_NO_THROW(collector.CollectFile(1, 9));
|
||||
}
|
||||
|
||||
TEST(Boundary, EmptyChunkIsNoopExceptReady) {
|
||||
FileCollector collector(1);
|
||||
collector.CollectFile(1, 2);
|
||||
collector.OnNewChunk(1, 2, {});
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(1)), std::logic_error);
|
||||
collector.OnNewChunk(1, 0, FileBytes{3, 4});
|
||||
EXPECT_EQ(*collector.GetFile(1), (FileBytes{3, 4}));
|
||||
}
|
||||
|
||||
51
tests/chunk_tests.cpp
Normal file
51
tests/chunk_tests.cpp
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#include "test_data.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace file_collector;
|
||||
|
||||
TEST(Chunks, OutOfOrderAndPartialOverlap) {
|
||||
FileCollector collector(3);
|
||||
collector.CollectFile(7, 10);
|
||||
collector.OnNewChunk(7, 6, FileBytes{'G', 'H', 'I', 'J'});
|
||||
collector.OnNewChunk(7, 2, FileBytes{'C', 'D', 'E', 'F'});
|
||||
collector.OnNewChunk(7, 0, FileBytes{'A', 'B', 'C', 'D'});
|
||||
EXPECT_EQ(*collector.GetFile(7), (FileBytes{'A','B','C','D','E','F','G','H','I','J'}));
|
||||
}
|
||||
|
||||
TEST(Chunks, DuplicatesAndContainedOverlaps) {
|
||||
FileCollector collector(4);
|
||||
auto original = test_data::Pattern(127);
|
||||
collector.CollectFile(1, original.size());
|
||||
collector.OnNewChunk(1, 0, test_data::Slice(original, 0, 80));
|
||||
collector.OnNewChunk(1, 12, test_data::Slice(original, 12, 30));
|
||||
collector.OnNewChunk(1, 63, test_data::Slice(original, 63, 64));
|
||||
collector.OnNewChunk(1, 0, FileBytes(original));
|
||||
EXPECT_EQ(*collector.GetFile(1), original);
|
||||
collector.OnNewChunk(1, 0, FileBytes(original)); // ready: ignore without requeue
|
||||
EXPECT_EQ(*collector.GetFile(1), original);
|
||||
}
|
||||
|
||||
TEST(Chunks, WordBoundaries63_64_65) {
|
||||
for (std::size_t size : {std::size_t{1}, std::size_t{63}, std::size_t{64},
|
||||
std::size_t{65}, std::size_t{129}}) {
|
||||
SCOPED_TRACE(size);
|
||||
FileCollector collector(2);
|
||||
auto original = test_data::Pattern(size);
|
||||
collector.CollectFile(1, size);
|
||||
for (std::size_t i = size; i-- > 0;) {
|
||||
collector.OnNewChunk(1, i, FileBytes{original[i]});
|
||||
}
|
||||
EXPECT_EQ(*collector.GetFile(1), original);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Chunks, OriginalZeroBytesAreNotHoles) {
|
||||
FileCollector collector(2);
|
||||
collector.CollectFile(1, 5);
|
||||
collector.OnNewChunk(1, 0, FileBytes{0, 0, 0});
|
||||
EXPECT_EQ(*collector.GetFile(1, 0, 3), (FileBytes{0, 0, 0}));
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(1, 0, 4)), std::logic_error);
|
||||
collector.OnNewChunk(1, 3, FileBytes{0, 0});
|
||||
EXPECT_EQ(*collector.GetFile(1), (FileBytes(5, 0)));
|
||||
}
|
||||
|
||||
53
tests/collector_tests.cpp
Normal file
53
tests/collector_tests.cpp
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#include "test_data.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
using namespace file_collector;
|
||||
|
||||
static_assert(std::is_same_v<decltype(std::declval<IFileCollector&>().GetFile(0)), FileView>);
|
||||
static_assert(std::is_same_v<decltype(std::declval<IFileCollector&>().GetFile(0, 0, 0)), FileView>);
|
||||
static_assert(std::is_same_v<decltype(std::declval<IFileCollector&>().TakeFile(0)), MutableFile>);
|
||||
|
||||
TEST(Collector, PolymorphicInterfaceAndIndependentFiles) {
|
||||
std::unique_ptr<IFileCollector> collector = std::make_unique<FileCollector>(2);
|
||||
const FileBytes one{1, 2, 3};
|
||||
const FileBytes two{4, 5};
|
||||
collector->CollectFile(17, one.size());
|
||||
collector->CollectFile(18, two.size());
|
||||
collector->OnNewChunk(18, 0, FileBytes(two));
|
||||
collector->OnNewChunk(17, 0, FileBytes(one));
|
||||
EXPECT_EQ(*collector->GetFile(17), one);
|
||||
EXPECT_EQ(*collector->GetFile(18), two);
|
||||
}
|
||||
|
||||
TEST(Collector, ZeroLengthReadyImmediately) {
|
||||
FileCollector collector(1);
|
||||
collector.CollectFile(1, 0);
|
||||
EXPECT_TRUE(collector.GetFile(1)->empty());
|
||||
EXPECT_TRUE(collector.GetFile(1, 0, 0)->empty());
|
||||
collector.OnNewChunk(1, 0, {});
|
||||
EXPECT_TRUE(collector.TakeFile(1)->empty());
|
||||
}
|
||||
|
||||
TEST(Collector, IncompleteFileNeedsExplicitOptOut) {
|
||||
FileCollector collector(2);
|
||||
collector.CollectFile(1, 5);
|
||||
collector.OnNewChunk(1, 1, FileBytes{7, 8});
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(1)), std::logic_error);
|
||||
EXPECT_EQ(*collector.GetFile(1, false), (FileBytes{0, 7, 8, 0, 0}));
|
||||
EXPECT_EQ(*collector.GetFile(1, 1, 2), (FileBytes{7, 8}));
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(1, 0, 3)), std::logic_error);
|
||||
}
|
||||
|
||||
TEST(Collector, TakeIncompleteOnlyWhenCheckingDisabled) {
|
||||
FileCollector collector(1);
|
||||
collector.CollectFile(1, 3);
|
||||
collector.OnNewChunk(1, 0, FileBytes{1});
|
||||
EXPECT_THROW(static_cast<void>(collector.TakeFile(1)), std::logic_error);
|
||||
collector.OnNewChunk(1, 2, FileBytes{3});
|
||||
auto bytes = collector.TakeFile(1, false);
|
||||
EXPECT_EQ(*bytes, (FileBytes{1, 0, 3}));
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(1)), std::out_of_range);
|
||||
}
|
||||
|
||||
72
tests/concurrency_tests.cpp
Normal file
72
tests/concurrency_tests.cpp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include "test_data.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <barrier>
|
||||
#include <exception>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using namespace file_collector;
|
||||
|
||||
TEST(Concurrency, ConcurrentDisjointWritesToOneFile) {
|
||||
constexpr std::size_t parts = 8;
|
||||
constexpr std::size_t bytes_per_part = 4096;
|
||||
FileCollector collector(4);
|
||||
auto original = test_data::Pattern(parts * bytes_per_part);
|
||||
collector.CollectFile(1, original.size());
|
||||
std::barrier start(static_cast<std::ptrdiff_t>(parts));
|
||||
std::vector<std::jthread> producers;
|
||||
for (std::size_t part = 0; part < parts; ++part) {
|
||||
producers.emplace_back([&, part] {
|
||||
start.arrive_and_wait();
|
||||
const auto pos = part * bytes_per_part;
|
||||
collector.OnNewChunk(1, pos, test_data::Slice(original, pos, bytes_per_part));
|
||||
});
|
||||
}
|
||||
producers.clear(); // jthread joins
|
||||
EXPECT_EQ(*collector.GetFile(1), original);
|
||||
}
|
||||
|
||||
TEST(Concurrency, ManyProducersOverlappingIdenticalData) {
|
||||
constexpr std::size_t producers_count = 8;
|
||||
FileCollector collector(4);
|
||||
auto original = test_data::Pattern(8192);
|
||||
collector.CollectFile(10, original.size());
|
||||
std::barrier start(static_cast<std::ptrdiff_t>(producers_count));
|
||||
std::vector<std::jthread> producers;
|
||||
for (std::size_t i = 0; i < producers_count; ++i) {
|
||||
producers.emplace_back([&, i] {
|
||||
start.arrive_and_wait();
|
||||
for (std::size_t pos = i * 97 % 512; pos < original.size(); pos += 4096) {
|
||||
const auto count = std::min<std::size_t>(5000, original.size() - pos);
|
||||
collector.OnNewChunk(10, pos, test_data::Slice(original, pos, count));
|
||||
}
|
||||
});
|
||||
}
|
||||
producers.clear();
|
||||
collector.OnNewChunk(10, 0, FileBytes(original));
|
||||
EXPECT_EQ(*collector.GetFile(10), original);
|
||||
}
|
||||
|
||||
TEST(Concurrency, SeveralFilesRemainIndependent) {
|
||||
FileCollector collector(4);
|
||||
std::vector<FileBytes> originals;
|
||||
for (std::uint32_t id = 0; id < 6; ++id) {
|
||||
originals.push_back(test_data::Pattern(4096, 400 + id));
|
||||
collector.CollectFile(id, 4096);
|
||||
}
|
||||
std::barrier start(6);
|
||||
std::vector<std::jthread> producers;
|
||||
for (std::uint32_t id = 0; id < 6; ++id) {
|
||||
producers.emplace_back([&, id] {
|
||||
start.arrive_and_wait();
|
||||
collector.OnNewChunk(id, 2048, test_data::Slice(originals[id], 2048, 2048));
|
||||
collector.OnNewChunk(id, 0, test_data::Slice(originals[id], 0, 2048));
|
||||
});
|
||||
}
|
||||
producers.clear();
|
||||
for (std::uint32_t id = 0; id < 6; ++id) {
|
||||
EXPECT_EQ(*collector.GetFile(id), originals[id]);
|
||||
}
|
||||
}
|
||||
|
||||
112
tests/exception_tests.cpp
Normal file
112
tests/exception_tests.cpp
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
#include "test_data.hpp"
|
||||
#include "fault_injector.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <atomic>
|
||||
#include <latch>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace file_collector;
|
||||
|
||||
namespace {
|
||||
|
||||
class OneShotFault final : public detail::FaultInjector {
|
||||
public:
|
||||
explicit OneShotFault(detail::FaultPoint target) : target_(target) {}
|
||||
|
||||
void Hit(detail::FaultPoint point) override {
|
||||
if (point == target_ && !triggered_.exchange(true)) {
|
||||
throw std::runtime_error("injected failure");
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
detail::FaultPoint target_;
|
||||
std::atomic_bool triggered_{false};
|
||||
};
|
||||
|
||||
class GatedFailure final : public detail::FaultInjector {
|
||||
public:
|
||||
void Hit(detail::FaultPoint point) override {
|
||||
if (point == detail::FaultPoint::AfterReserve && !hit_.exchange(true)) {
|
||||
reached_.count_down();
|
||||
resume_.wait();
|
||||
throw std::runtime_error("gated injected failure");
|
||||
}
|
||||
}
|
||||
|
||||
void WaitUntilReserved() { reached_.wait(); }
|
||||
void Release() { resume_.count_down(); }
|
||||
|
||||
private:
|
||||
std::atomic_bool hit_{false};
|
||||
std::latch reached_{1};
|
||||
std::latch resume_{1};
|
||||
};
|
||||
|
||||
class CountSubmissions final : public detail::FaultInjector {
|
||||
public:
|
||||
void Hit(detail::FaultPoint point) override {
|
||||
if (point == detail::FaultPoint::BeforeEnqueue) {
|
||||
count_.fetch_add(1);
|
||||
}
|
||||
}
|
||||
[[nodiscard]] int Count() const noexcept { return count_.load(); }
|
||||
|
||||
private:
|
||||
std::atomic_int count_{0};
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(Exceptions, BeforeEnqueueFailureRollsBackPendingCounter) {
|
||||
auto injector = std::make_shared<OneShotFault>(detail::FaultPoint::BeforeEnqueue);
|
||||
FileCollector collector(2, injector);
|
||||
collector.CollectFile(1, 3);
|
||||
EXPECT_THROW(collector.OnNewChunk(1, 0, FileBytes{1, 2, 3}), std::runtime_error);
|
||||
EXPECT_EQ(*collector.GetFile(1, false), (FileBytes{0, 0, 0}));
|
||||
collector.OnNewChunk(1, 0, FileBytes{1, 2, 3});
|
||||
EXPECT_EQ(*collector.GetFile(1), (FileBytes{1, 2, 3}));
|
||||
}
|
||||
|
||||
TEST(Exceptions, AllWorkerFaultPointsReportAndRecoverAfterClear) {
|
||||
for (auto point : {detail::FaultPoint::AfterReserve,
|
||||
detail::FaultPoint::BeforeCopy,
|
||||
detail::FaultPoint::BeforeCommit}) {
|
||||
SCOPED_TRACE(static_cast<int>(point));
|
||||
auto injector = std::make_shared<OneShotFault>(point);
|
||||
FileCollector collector(2, injector);
|
||||
collector.CollectFile(1, 65);
|
||||
collector.OnNewChunk(1, 0, FileBytes(65, 7));
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(1, false)), std::runtime_error);
|
||||
EXPECT_THROW(static_cast<void>(collector.TakeFile(1)), std::runtime_error);
|
||||
collector.ClearFile(1);
|
||||
collector.OnNewChunk(1, 0, FileBytes(65, 9));
|
||||
EXPECT_EQ(*collector.GetFile(1), (FileBytes(65, 9)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Exceptions, FailedReservationCannotLeavePhantomCoverage) {
|
||||
auto injector = std::make_shared<GatedFailure>();
|
||||
FileCollector collector(2, injector);
|
||||
collector.CollectFile(7, 64);
|
||||
collector.OnNewChunk(7, 0, FileBytes(64, 42));
|
||||
injector->WaitUntilReserved();
|
||||
collector.OnNewChunk(7, 0, FileBytes(64, 42));
|
||||
injector->Release();
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(7)), std::runtime_error);
|
||||
collector.ClearFile(7);
|
||||
collector.OnNewChunk(7, 0, FileBytes(64, 33));
|
||||
EXPECT_EQ(*collector.GetFile(7), (FileBytes(64, 33)));
|
||||
}
|
||||
|
||||
TEST(Exceptions, CompletedFileDoesNotEnqueueNewChunks) {
|
||||
auto injector = std::make_shared<CountSubmissions>();
|
||||
FileCollector collector(2, injector);
|
||||
collector.CollectFile(1, 3);
|
||||
collector.OnNewChunk(1, 0, FileBytes{1, 2, 3});
|
||||
ASSERT_EQ(*collector.GetFile(1), (FileBytes{1, 2, 3}));
|
||||
const auto initial_submissions = injector->Count();
|
||||
collector.OnNewChunk(1, 0, FileBytes{1, 2, 3});
|
||||
EXPECT_EQ(injector->Count(), initial_submissions);
|
||||
}
|
||||
|
||||
64
tests/lifecycle_tests.cpp
Normal file
64
tests/lifecycle_tests.cpp
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
#include "test_data.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <barrier>
|
||||
#include <thread>
|
||||
|
||||
using namespace file_collector;
|
||||
|
||||
TEST(Lifecycle, TakeTransfersMutableBufferAndAllowsRegistration) {
|
||||
FileCollector collector(2);
|
||||
collector.CollectFile(1, 3);
|
||||
collector.OnNewChunk(1, 0, FileBytes{4, 5, 6});
|
||||
MutableFile bytes = collector.TakeFile(1);
|
||||
EXPECT_EQ(*bytes, (FileBytes{4, 5, 6}));
|
||||
bytes->at(0) = 99;
|
||||
EXPECT_EQ(bytes->front(), 99);
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(1)), std::out_of_range);
|
||||
collector.CollectFile(1, 1);
|
||||
collector.OnNewChunk(1, 0, FileBytes{7});
|
||||
EXPECT_EQ(*collector.GetFile(1), (FileBytes{7}));
|
||||
}
|
||||
|
||||
TEST(Lifecycle, ClearKeepsRegistrationAndSize) {
|
||||
FileCollector collector(2);
|
||||
collector.CollectFile(1, 4);
|
||||
collector.OnNewChunk(1, 0, FileBytes{1, 2, 3, 4});
|
||||
auto previous = collector.GetFile(1);
|
||||
collector.ClearFile(1);
|
||||
EXPECT_THROW(static_cast<void>(collector.GetFile(1)), std::logic_error);
|
||||
EXPECT_EQ(*collector.GetFile(1, false), (FileBytes{0, 0, 0, 0}));
|
||||
collector.OnNewChunk(1, 0, FileBytes{5, 6, 7, 8});
|
||||
EXPECT_EQ(*collector.GetFile(1), (FileBytes{5, 6, 7, 8}));
|
||||
EXPECT_EQ(*previous, (FileBytes{1, 2, 3, 4}));
|
||||
EXPECT_THROW(collector.CollectFile(1, 1), std::logic_error);
|
||||
}
|
||||
|
||||
TEST(Lifecycle, RemovalDoesNotInvalidateView) {
|
||||
FileCollector collector(2);
|
||||
collector.CollectFile(1, 2);
|
||||
collector.OnNewChunk(1, 0, FileBytes{3, 4});
|
||||
auto previous = collector.GetFile(1);
|
||||
collector.RemoveFile(1);
|
||||
EXPECT_EQ(*previous, (FileBytes{3, 4}));
|
||||
collector.CollectFile(1, 2);
|
||||
collector.OnNewChunk(1, 0, FileBytes{5, 6});
|
||||
EXPECT_EQ(*collector.GetFile(1), (FileBytes{5, 6}));
|
||||
}
|
||||
|
||||
TEST(Lifecycle, ConcurrentClearAndSubmissionRemainReusable) {
|
||||
FileCollector collector(2);
|
||||
collector.CollectFile(1, 64);
|
||||
std::barrier start(2);
|
||||
std::jthread sender([&] {
|
||||
start.arrive_and_wait();
|
||||
try { collector.OnNewChunk(1, 0, FileBytes(64, 8)); }
|
||||
catch (const std::logic_error&) {} // Clear may close admission first.
|
||||
});
|
||||
start.arrive_and_wait();
|
||||
collector.ClearFile(1);
|
||||
sender.join();
|
||||
collector.ClearFile(1);
|
||||
collector.OnNewChunk(1, 0, FileBytes(64, 9));
|
||||
EXPECT_EQ(*collector.GetFile(1), (FileBytes(64, 9)));
|
||||
}
|
||||
|
||||
68
tests/performance_tests.cpp
Normal file
68
tests/performance_tests.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
#include "test_data.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#if defined(__APPLE__) || defined(__linux__)
|
||||
#include <sys/resource.h>
|
||||
#endif
|
||||
|
||||
using namespace file_collector;
|
||||
|
||||
namespace {
|
||||
|
||||
double PeakMiB() {
|
||||
#if defined(__APPLE__) || defined(__linux__)
|
||||
rusage usage{};
|
||||
if (getrusage(RUSAGE_SELF, &usage) == 0) {
|
||||
#if defined(__APPLE__)
|
||||
return static_cast<double>(usage.ru_maxrss) / (1024.0 * 1024.0);
|
||||
#else
|
||||
return static_cast<double>(usage.ru_maxrss) / 1024.0;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
return -1.;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(Performance, BaselineMiBPerSecondAndSubmissionLatency) {
|
||||
using Clock = std::chrono::steady_clock;
|
||||
constexpr std::size_t size = 4 * 1024 * 1024;
|
||||
constexpr std::size_t chunk_size = 16 * 1024;
|
||||
auto original = test_data::Pattern(size, 1001);
|
||||
FileCollector collector(4);
|
||||
collector.CollectFile(1, size);
|
||||
std::vector<double> submit_microseconds;
|
||||
submit_microseconds.reserve(size / chunk_size);
|
||||
const auto start = Clock::now();
|
||||
for (std::size_t pos = 0; pos < size; pos += chunk_size) {
|
||||
auto chunk = test_data::Slice(original, pos, chunk_size);
|
||||
const auto t0 = Clock::now();
|
||||
collector.OnNewChunk(1, pos, std::move(chunk));
|
||||
submit_microseconds.push_back(std::chrono::duration<double, std::micro>(Clock::now() - t0).count());
|
||||
}
|
||||
auto restored = collector.GetFile(1);
|
||||
const auto elapsed = std::chrono::duration<double>(Clock::now() - start).count();
|
||||
ASSERT_EQ(*restored, original);
|
||||
std::sort(submit_microseconds.begin(), submit_microseconds.end());
|
||||
const auto percentile = [&](double fraction) {
|
||||
const auto index = static_cast<std::size_t>((submit_microseconds.size() - 1) * fraction);
|
||||
return submit_microseconds[index];
|
||||
};
|
||||
std::cout << std::fixed << std::setprecision(2)
|
||||
<< "profile: size_mib=4 chunk_kib=16 workers=4 seed=1001"
|
||||
<< " throughput_mib_s=" << 4.0 / elapsed
|
||||
<< " submission_p50_us=" << percentile(0.50)
|
||||
<< " submission_p95_us=" << percentile(0.95)
|
||||
<< " peak_process_rss_mib=" << PeakMiB()
|
||||
<< " duplicate_fraction=0" << '\n';
|
||||
// Metrics are informational, with no machine-specific timing thresholds.
|
||||
}
|
||||
|
||||
55
tests/snapshot_tests.cpp
Normal file
55
tests/snapshot_tests.cpp
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#include "test_data.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace file_collector;
|
||||
|
||||
TEST(Snapshots, PartialAndIncompleteCopiesNeverChange) {
|
||||
FileCollector collector(2);
|
||||
collector.CollectFile(1, 5);
|
||||
collector.OnNewChunk(1, 0, FileBytes{1, 2});
|
||||
auto incomplete = collector.GetFile(1, false);
|
||||
auto partial = collector.GetFile(1, 0, 4, false);
|
||||
collector.OnNewChunk(1, 2, FileBytes{3, 4, 5});
|
||||
EXPECT_EQ(*collector.GetFile(1), (FileBytes{1, 2, 3, 4, 5}));
|
||||
EXPECT_EQ(*incomplete, (FileBytes{1, 2, 0, 0, 0}));
|
||||
EXPECT_EQ(*partial, (FileBytes{1, 2, 0, 0}));
|
||||
}
|
||||
|
||||
TEST(Snapshots, ReadyFileUsesSharedReadOnlyBacking) {
|
||||
FileCollector collector(2);
|
||||
collector.CollectFile(1, 3);
|
||||
collector.OnNewChunk(1, 0, FileBytes{1, 2, 3});
|
||||
auto first = collector.GetFile(1);
|
||||
auto second = collector.GetFile(1);
|
||||
EXPECT_EQ(first.get(), second.get());
|
||||
collector.OnNewChunk(1, 0, FileBytes{99});
|
||||
EXPECT_EQ(*first, (FileBytes{1, 2, 3}));
|
||||
}
|
||||
|
||||
TEST(Snapshots, TakeCopiesWhenReadOnlyOwnerExists) {
|
||||
FileCollector collector(2);
|
||||
collector.CollectFile(1, 3);
|
||||
collector.OnNewChunk(1, 0, FileBytes{1, 2, 3});
|
||||
auto read_only = collector.GetFile(1);
|
||||
auto writable = collector.TakeFile(1);
|
||||
EXPECT_NE(read_only.get(), writable.get());
|
||||
writable->at(1) = 99;
|
||||
EXPECT_EQ(*read_only, (FileBytes{1, 2, 3}));
|
||||
EXPECT_EQ(*writable, (FileBytes{1, 99, 3}));
|
||||
}
|
||||
|
||||
TEST(Snapshots, ClearAndReregisterPreservePreviouslyIssuedViews) {
|
||||
FileCollector collector(2);
|
||||
collector.CollectFile(1, 2);
|
||||
collector.OnNewChunk(1, 0, FileBytes{1, 2});
|
||||
auto old_view = collector.GetFile(1);
|
||||
collector.ClearFile(1);
|
||||
collector.OnNewChunk(1, 0, FileBytes{3, 4});
|
||||
auto mid_view = collector.GetFile(1);
|
||||
collector.RemoveFile(1);
|
||||
collector.CollectFile(1, 1);
|
||||
collector.OnNewChunk(1, 0, FileBytes{5});
|
||||
EXPECT_EQ(*old_view, (FileBytes{1, 2}));
|
||||
EXPECT_EQ(*mid_view, (FileBytes{3, 4}));
|
||||
}
|
||||
|
||||
207
tests/stress_tests.cpp
Normal file
207
tests/stress_tests.cpp
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
#include "test_data.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <barrier>
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include <random>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using namespace file_collector;
|
||||
|
||||
namespace {
|
||||
|
||||
struct Chunk {
|
||||
std::size_t pos;
|
||||
FileBytes bytes;
|
||||
};
|
||||
|
||||
std::vector<Chunk> MakeChunks(const FileBytes& original, std::uint32_t seed) {
|
||||
std::mt19937 rng(seed);
|
||||
std::vector<Chunk> chunks;
|
||||
for (std::size_t pos = 0; pos < original.size(); pos += 193) {
|
||||
const std::size_t length = std::min<std::size_t>(257, original.size() - pos);
|
||||
chunks.push_back({pos, test_data::Slice(original, pos, length)});
|
||||
if (rng() % 3 == 0) {
|
||||
chunks.push_back({pos, test_data::Slice(original, pos, length)});
|
||||
}
|
||||
}
|
||||
std::shuffle(chunks.begin(), chunks.end(), rng);
|
||||
return chunks;
|
||||
}
|
||||
|
||||
std::vector<Chunk> MakeRandomOverlappingChunks(const FileBytes& original, std::uint32_t seed) {
|
||||
std::mt19937 rng(seed);
|
||||
std::vector<Chunk> chunks;
|
||||
if (original.empty()) {
|
||||
return chunks;
|
||||
}
|
||||
|
||||
// A randomized partition guarantees complete coverage independently of overlaps.
|
||||
for (std::size_t pos = 0; pos < original.size();) {
|
||||
const auto maximum = std::min<std::size_t>(4096, original.size() - pos);
|
||||
const auto length = std::uniform_int_distribution<std::size_t>(1, maximum)(rng);
|
||||
chunks.push_back({pos, test_data::Slice(original, pos, length)});
|
||||
if (rng() % 5 == 0) {
|
||||
chunks.push_back(chunks.back()); // Full duplicate.
|
||||
}
|
||||
pos += length;
|
||||
}
|
||||
|
||||
// Independent random windows create additional arbitrary partial overlaps.
|
||||
const auto windows = std::min<std::size_t>(4096, original.size() / 31 + 25);
|
||||
for (std::size_t i = 0; i < windows; ++i) {
|
||||
const auto maximum = std::min<std::size_t>(8192, original.size());
|
||||
const auto length = std::uniform_int_distribution<std::size_t>(1, maximum)(rng);
|
||||
const auto pos = std::uniform_int_distribution<std::size_t>(0, original.size() - length)(rng);
|
||||
chunks.push_back({pos, test_data::Slice(original, pos, length)});
|
||||
}
|
||||
|
||||
// Explicitly guarantee 1-byte, weak, medium, heavy and full intersections.
|
||||
if (original.size() >= 4096) {
|
||||
constexpr std::size_t anchor_length = 1024;
|
||||
const auto anchor = std::uniform_int_distribution<std::size_t>(
|
||||
0, original.size() - 2 * anchor_length)(rng);
|
||||
chunks.push_back({anchor, test_data::Slice(original, anchor, anchor_length)});
|
||||
for (const auto overlap : std::array<std::size_t, 8>{1, 102, 256, 512, 768, 922, 1023, 1024}) {
|
||||
const auto pos = anchor + anchor_length - overlap;
|
||||
chunks.push_back({pos, test_data::Slice(original, pos, anchor_length)});
|
||||
}
|
||||
}
|
||||
|
||||
std::shuffle(chunks.begin(), chunks.end(), rng);
|
||||
return chunks;
|
||||
}
|
||||
|
||||
void SubmitRandomChunks(FileCollector& collector, const std::vector<Chunk>& chunks,
|
||||
std::uint32_t file_id, std::size_t producers_count) {
|
||||
std::barrier start(static_cast<std::ptrdiff_t>(producers_count));
|
||||
std::vector<std::exception_ptr> errors(producers_count);
|
||||
std::vector<std::jthread> producers;
|
||||
for (std::size_t producer = 0; producer < producers_count; ++producer) {
|
||||
producers.emplace_back([&, producer] {
|
||||
start.arrive_and_wait();
|
||||
try {
|
||||
for (std::size_t index = producer; index < chunks.size(); index += producers_count) {
|
||||
collector.OnNewChunk(file_id, chunks[index].pos, FileBytes(chunks[index].bytes));
|
||||
}
|
||||
} catch (...) {
|
||||
errors[producer] = std::current_exception();
|
||||
}
|
||||
});
|
||||
}
|
||||
producers.clear(); // Join all producers before propagating errors.
|
||||
for (const auto& error : errors) {
|
||||
if (error) {
|
||||
std::rethrow_exception(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(Stress, DeterministicMultiProducerOverlaps) {
|
||||
constexpr std::size_t file_size = 1024 * 1024;
|
||||
for (std::uint32_t seed : {101u, 202u, 303u}) {
|
||||
SCOPED_TRACE(seed);
|
||||
auto original = test_data::Pattern(file_size, seed);
|
||||
auto chunks = MakeChunks(original, seed);
|
||||
FileCollector collector(4);
|
||||
collector.CollectFile(1, file_size);
|
||||
std::barrier start(8);
|
||||
std::vector<std::jthread> senders;
|
||||
for (std::size_t worker = 0; worker < 8; ++worker) {
|
||||
senders.emplace_back([&, worker] {
|
||||
start.arrive_and_wait();
|
||||
for (std::size_t index = worker; index < chunks.size(); index += 8) {
|
||||
collector.OnNewChunk(1, chunks[index].pos, FileBytes(chunks[index].bytes));
|
||||
}
|
||||
});
|
||||
}
|
||||
senders.clear();
|
||||
EXPECT_EQ(*collector.GetFile(1), original);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Stress, ThousandsOfRepeatedOverlaps) {
|
||||
auto original = test_data::Pattern(8193, 77);
|
||||
FileCollector collector(4);
|
||||
collector.CollectFile(1, original.size());
|
||||
for (int repeat = 0; repeat < 2000; ++repeat) {
|
||||
const std::size_t pos = static_cast<std::size_t>(repeat * 31) % (original.size() - 128);
|
||||
collector.OnNewChunk(1, pos, test_data::Slice(original, pos, 128));
|
||||
}
|
||||
collector.OnNewChunk(1, 0, FileBytes(original));
|
||||
EXPECT_EQ(*collector.GetFile(1), original);
|
||||
}
|
||||
|
||||
TEST(Stress, RandomizedOverlapSpectrumAcrossSizesAndSeeds) {
|
||||
constexpr std::array<std::size_t, 8> file_sizes{0, 1, 63, 64, 65, 4096, 65537, 1048576};
|
||||
constexpr std::array<std::uint32_t, 4> seeds{7, 101, 2026, 0xC0FFEE};
|
||||
for (const auto file_size : file_sizes) {
|
||||
for (const auto seed : seeds) {
|
||||
SCOPED_TRACE(::testing::Message() << "size=" << file_size << " seed=" << seed);
|
||||
const auto original = test_data::Pattern(file_size, seed);
|
||||
const auto chunks = MakeRandomOverlappingChunks(original, seed);
|
||||
FileCollector collector(4);
|
||||
collector.CollectFile(77, file_size);
|
||||
SubmitRandomChunks(collector, chunks, 77, 8);
|
||||
EXPECT_EQ(*collector.GetFile(77), original);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Stress, RandomizedSnapshotsStayImmutableDuringConcurrentIngestion) {
|
||||
constexpr std::size_t file_size = 65537;
|
||||
auto original = test_data::Pattern(file_size, 0xBADC0DE);
|
||||
auto chunks = MakeRandomOverlappingChunks(original, 0xBADC0DE);
|
||||
FileCollector collector(4);
|
||||
collector.CollectFile(21, file_size);
|
||||
const auto initial_snapshot = collector.GetFile(21, false);
|
||||
EXPECT_EQ(*initial_snapshot, (FileBytes(file_size, 0)));
|
||||
|
||||
std::barrier start(5);
|
||||
std::array<std::exception_ptr, 5> errors;
|
||||
std::vector<FileView> snapshots;
|
||||
std::vector<FileBytes> expected_snapshots;
|
||||
std::vector<std::jthread> participants;
|
||||
for (std::size_t producer = 0; producer < 4; ++producer) {
|
||||
participants.emplace_back([&, producer] {
|
||||
start.arrive_and_wait();
|
||||
try {
|
||||
for (std::size_t index = producer; index < chunks.size(); index += 4) {
|
||||
collector.OnNewChunk(21, chunks[index].pos, FileBytes(chunks[index].bytes));
|
||||
}
|
||||
} catch (...) {
|
||||
errors[producer] = std::current_exception();
|
||||
}
|
||||
});
|
||||
}
|
||||
participants.emplace_back([&] {
|
||||
start.arrive_and_wait();
|
||||
try {
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
auto view = collector.GetFile(21, false);
|
||||
expected_snapshots.push_back(*view);
|
||||
snapshots.push_back(std::move(view));
|
||||
}
|
||||
} catch (...) {
|
||||
errors[4] = std::current_exception();
|
||||
}
|
||||
});
|
||||
participants.clear();
|
||||
for (const auto& error : errors) {
|
||||
if (error) {
|
||||
std::rethrow_exception(error);
|
||||
}
|
||||
}
|
||||
|
||||
EXPECT_EQ(*collector.GetFile(21), original);
|
||||
EXPECT_EQ(*initial_snapshot, (FileBytes(file_size, 0)));
|
||||
for (std::size_t index = 0; index < snapshots.size(); ++index) {
|
||||
EXPECT_EQ(*snapshots[index], expected_snapshots[index]);
|
||||
}
|
||||
}
|
||||
|
||||
27
tests/test_data.hpp
Normal file
27
tests/test_data.hpp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include "file_collector/file_collector.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <random>
|
||||
|
||||
namespace test_data {
|
||||
|
||||
using namespace file_collector;
|
||||
|
||||
inline FileBytes Pattern(std::size_t size, std::uint32_t seed = 0xC0FFEE) {
|
||||
std::mt19937 rng(seed);
|
||||
FileBytes bytes(size);
|
||||
std::generate(bytes.begin(), bytes.end(), [&] { return static_cast<std::uint8_t>(rng()); });
|
||||
return bytes;
|
||||
}
|
||||
|
||||
inline FileBytes Slice(const FileBytes& data, std::size_t pos, std::size_t length) {
|
||||
return FileBytes(data.begin() + static_cast<std::ptrdiff_t>(pos),
|
||||
data.begin() + static_cast<std::ptrdiff_t>(pos + length));
|
||||
}
|
||||
|
||||
} // namespace test_data
|
||||
|
||||
42
tests/thread_pool_tests.cpp
Normal file
42
tests/thread_pool_tests.cpp
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#include "thread_pool.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
using file_collector::detail::ThreadPool;
|
||||
|
||||
TEST(ThreadPool, ZeroReportedWorkersFallsBackToOne) {
|
||||
ThreadPool pool(0);
|
||||
EXPECT_EQ(pool.WorkerCount(), std::size_t{1});
|
||||
}
|
||||
|
||||
TEST(ThreadPool, ExplicitWorkerCountAndQueueDrain) {
|
||||
std::atomic_int calls{0};
|
||||
{
|
||||
ThreadPool pool(3);
|
||||
EXPECT_EQ(pool.WorkerCount(), std::size_t{3});
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
pool.Submit([&] { calls.fetch_add(1); });
|
||||
}
|
||||
}
|
||||
EXPECT_EQ(calls.load(), 100);
|
||||
}
|
||||
|
||||
TEST(ThreadPool, WorkersSurviveTaskException) {
|
||||
ThreadPool pool(2);
|
||||
std::promise<void> done;
|
||||
auto result = done.get_future();
|
||||
pool.Submit([] { throw std::runtime_error("test exception"); });
|
||||
pool.Submit([&] { done.set_value(); });
|
||||
EXPECT_EQ(result.wait_for(std::chrono::seconds(5)), std::future_status::ready);
|
||||
}
|
||||
|
||||
TEST(ThreadPool, DefaultUsesHardwareConcurrencyOrOne) {
|
||||
ThreadPool pool;
|
||||
const auto available = std::thread::hardware_concurrency();
|
||||
EXPECT_EQ(pool.WorkerCount(), available == 0 ? 1u : available);
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue