13#include <boost/test/unit_test.hpp>
16#include <condition_variable>
19#include <unordered_set>
30#ifdef DEBUG_LOCKCONTENTION
52 static std::atomic<size_t>
n_calls;
55 n_calls.fetch_add(1, std::memory_order_relaxed);
107 static std::atomic<uint64_t>
nFrozen;
108 static std::condition_variable
cv;
119 std::unique_lock<std::mutex>
l(
m);
120 nFrozen.store(1, std::memory_order_relaxed);
122 cv.wait(
l, []{
return nFrozen.load(std::memory_order_relaxed) == 0;});
128 other.should_freeze =
false;
133 other.should_freeze =
false;
143std::unordered_multiset<size_t> UniqueCheck::results;
163 std::vector<FakeCheckCheckCompletion>
vChecks;
165 for (
const size_t i :
range) {
186 std::vector<size_t>
range;
187 range.push_back(
size_t{0});
188 Correct_Queue_range(
range);
194 std::vector<size_t>
range;
195 range.push_back(
size_t{1});
196 Correct_Queue_range(
range);
202 std::vector<size_t>
range;
203 range.push_back(100000);
204 Correct_Queue_range(
range);
210 std::vector<size_t>
range;
211 range.reserve(100000/1000);
212 for (
size_t i = 2; i < 100000; i += std::max((
size_t)1, (
size_t)m_rng.randrange(std::min((
size_t)1000, ((
size_t)100000) - i))))
214 Correct_Queue_range(
range);
222 for (
size_t i = 0; i < 1001; ++i) {
226 size_t r = m_rng.randrange(10);
228 std::vector<FixedCheck>
vChecks;
231 vChecks.emplace_back(
remaining == 1 ? std::make_optional<int>(17 * i) : std::nullopt);
234 auto result =
control.Complete();
236 BOOST_REQUIRE(result.has_value() && *result ==
static_cast<int>(17 * i));
248 for (
const bool end_fails : {
true,
false}) {
251 std::vector<FixedCheck>
vChecks;
256 bool r = !
control.Complete().has_value();
268 size_t COUNT = 100000;
269 size_t total =
COUNT;
273 size_t r = m_rng.randrange(10);
274 std::vector<UniqueCheck>
vChecks;
275 for (
size_t k = 0;
k < r && total;
k++)
284 for (
size_t i = 0; i <
COUNT; ++i) {
285 r = r && UniqueCheck::results.count(i) == 1;
300 for (
size_t i = 0; i < 1000; ++i) {
305 size_t r = m_rng.randrange(10);
306 std::vector<MemoryCheck>
vChecks;
307 for (
size_t k = 0;
k < r && total;
k++) {
311 vChecks.emplace_back(total == 0 || total == i || total == i/2);
326 std::thread
t0([&]() {
328 std::vector<FrozenCleanupCheck>
vChecks(1);
330 auto result =
control.Complete();
339 for (
auto x = 0; x < 100 && !
fails; ++x) {
360 std::vector<std::thread>
tg;
363 std::atomic<int>
fails {0};
364 for (
size_t i = 0; i < 3; ++i) {
374 for (
auto& thread:
tg) {
375 if (thread.joinable()) thread.join();
380 std::vector<std::thread>
tg;
382 std::condition_variable cv;
388 std::unique_lock<std::mutex>
l(
m);
391 std::unique_lock<std::mutex>
ll(
m);
404 for (
auto x = 0; x < 100 && !
fails; ++x) {
409 cv.wait(
l, [&](){
return done;});
415 for (
auto& thread:
tg) {
416 if (thread.joinable()) thread.join();
CCheckQueue< FakeCheckCheckCompletion > Correct_Queue
CCheckQueue< FrozenCleanupCheck > FrozenCleanup_Queue
static const int SCRIPT_CHECK_THREADS
CCheckQueue< UniqueCheck > Unique_Queue
CCheckQueue< FixedCheck > Fixed_Queue
CCheckQueue< FakeCheck > Standard_Queue
CCheckQueue< MemoryCheck > Memory_Queue
BOOST_AUTO_TEST_CASE(test_CheckQueue_Correct_Zero)
Test that 0 checks is correct.
static const unsigned int QUEUE_BATCH_SIZE
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Queue for verifications that have to be performed.
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
void Correct_Queue_range(std::vector< size_t > range)
This test case checks that the CCheckQueue works properly with each specified size_t Checks pushed.
static std::atomic< size_t > n_calls
std::optional< int > operator()()
std::optional< int > operator()() const
std::optional< int > m_result
FixedCheck(std::optional< int > result)
std::optional< int > operator()() const
static std::atomic< uint64_t > nFrozen
static std::condition_variable cv
std::optional< int > operator()() const
FrozenCleanupCheck & operator=(FrozenCleanupCheck &&other) noexcept
FrozenCleanupCheck(FrozenCleanupCheck &&other) noexcept
FrozenCleanupCheck()=default
static std::atomic< size_t > fake_allocated_memory
std::optional< int > operator()() const
MemoryCheck(const MemoryCheck &x)
Identical to TestingSetup but excludes lock contention logging if DEBUG_LOCKCONTENTION is defined,...
NoLockLoggingTestingSetup()
Testing setup that configures a complete environment.
static std::unordered_multiset< size_t > results GUARDED_BY(m)
std::optional< int > operator()()
UniqueCheck(size_t check_id_in)
void UninterruptibleSleep(const std::chrono::microseconds &n)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.