Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
concurrency_test_utils_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
32
33#include <gtest/gtest.h>
34
35#include <algorithm>
36#include <atomic>
37#include <chrono>
38#include <cstddef>
39#include <mutex>
40#include <queue>
41#include <set>
42#include <thread>
43#include <vector>
44
45using namespace Aleph::Testing;
46using namespace std::chrono_literals;
47
48TEST(ConcurrencyTestUtils, StartGateReleasesAllWorkersTogether)
49{
51 std::atomic<size_t> passed{0};
52 std::vector<std::thread> threads;
53
54 for (size_t i = 0; i < 3; ++i)
55 threads.emplace_back([&]
56 {
57 gate.arrive_and_wait();
58 passed.fetch_add(1, std::memory_order_relaxed);
59 });
60
61 gate.wait_until_ready();
62 EXPECT_EQ(gate.arrived(), 3u);
63 EXPECT_EQ(passed.load(std::memory_order_relaxed), 0u);
64
65 gate.release();
66 for (auto &thread : threads)
67 thread.join();
68
69 EXPECT_EQ(passed.load(std::memory_order_relaxed), 3u);
70}
71
72TEST(ConcurrencyTestUtils, ProducerConsumerStressConservesValues)
73{
74 std::queue<size_t> queue;
75 std::mutex mutex;
76
78 config.producers = 4;
79 config.consumers = 3;
80 config.items_per_producer = 128;
81 config.timeout = 5s;
82
83 auto result = run_producer_consumer_stress(
84 config,
85 [&](const size_t value)
86 {
87 std::lock_guard lock(mutex);
88 queue.push(value);
89 },
90 [&](size_t &value)
91 {
92 std::lock_guard lock(mutex);
93 if (queue.empty())
94 return false;
95 value = queue.front();
96 queue.pop();
97 return true;
98 });
99
100 ASSERT_FALSE(result.timed_out);
101 ASSERT_EQ(result.size(), config.producers * config.items_per_producer);
102
103 std::sort(result.consumed.begin(), result.consumed.end());
104 for (size_t i = 0; i < result.consumed.size(); ++i)
105 EXPECT_EQ(result.consumed[i], i);
106}
107
108TEST(ConcurrencyTestUtils, RandomOperationTraceIsDeterministic)
109{
110 const auto first = make_random_operation_trace(
111 64, 12345, 11,
112 {
113 Trace_Operation_Kind::insert,
114 Trace_Operation_Kind::erase,
115 Trace_Operation_Kind::contains
116 });
117 const auto second = make_random_operation_trace(
118 64, 12345, 11,
119 {
120 Trace_Operation_Kind::insert,
121 Trace_Operation_Kind::erase,
122 Trace_Operation_Kind::contains
123 });
124 const auto different = make_random_operation_trace(
125 64, 54321, 11,
126 {
127 Trace_Operation_Kind::insert,
128 Trace_Operation_Kind::erase,
129 Trace_Operation_Kind::contains
130 });
131
132 EXPECT_EQ(first, second);
133 EXPECT_NE(first, different);
134 for (const auto &op : first)
135 EXPECT_LT(op.key, 11u);
136}
137
138TEST(ConcurrencyTestUtils, TraceReplayDetectsNoMismatchForEquivalentSets)
139{
140 const auto trace = make_random_operation_trace(256, 99, 17);
141 std::set<size_t> subject;
142 std::set<size_t> reference;
143
144 auto apply = [](std::set<size_t> &set, const Trace_Operation &op)
145 {
146 switch (op.kind)
147 {
148 case Trace_Operation_Kind::insert:
149 return set.insert(op.key).second;
150 case Trace_Operation_Kind::erase:
151 return set.erase(op.key) != 0;
152 case Trace_Operation_Kind::contains:
153 return set.find(op.key) != set.end();
154 default:
155 return false;
156 }
157 };
158
159 const auto mismatches = replay_trace_and_collect_mismatches(
160 trace,
161 [&](const Trace_Operation &op) { return apply(subject, op); },
162 [&](const Trace_Operation &op) { return apply(reference, op); });
163
164 EXPECT_TRUE(mismatches.empty());
165}
166
167TEST(ConcurrencyTestUtils, TraceReplayReportsMismatches)
168{
169 std::vector<Trace_Operation> trace =
170 {
171 {Trace_Operation_Kind::insert, 1, 0},
172 {Trace_Operation_Kind::contains, 1, 0},
173 {Trace_Operation_Kind::erase, 1, 0},
174 {Trace_Operation_Kind::contains, 1, 0}
175 };
176 std::set<size_t> subject;
177 std::set<size_t> reference;
178
179 const auto mismatches = replay_trace_and_collect_mismatches(
180 trace,
181 [&](const Trace_Operation &op)
182 {
183 if (op.kind == Trace_Operation_Kind::insert)
184 return subject.insert(op.key).second;
185 if (op.kind == Trace_Operation_Kind::erase)
186 return subject.erase(op.key) != 0;
187 return true;
188 },
189 [&](const Trace_Operation &op)
190 {
191 if (op.kind == Trace_Operation_Kind::insert)
192 return reference.insert(op.key).second;
193 if (op.kind == Trace_Operation_Kind::erase)
194 return reference.erase(op.key) != 0;
195 return reference.find(op.key) != reference.end();
196 });
197
198 ASSERT_EQ(mismatches.size(), 1u);
199 EXPECT_EQ(mismatches[0], 3u);
200}
size_t size_t int32_t value
Definition ca-c-api.h:116
Single-use deterministic start gate for concurrent tests.
void wait_until_ready()
Wait until all expected workers are ready, or the gate is cancelled.
size_t arrived() const
Return the number of arrived workers.
void release()
Release all waiting workers.
Reusable helpers for concurrent data-structure tests.
#define TEST(name)
Producer_Consumer_Stress_Result run_producer_consumer_stress(const Producer_Consumer_Stress_Config &config, Push push, TryPop try_pop)
Run a deterministic producer/consumer stress scenario.
std::vector< Trace_Operation > make_random_operation_trace(const size_t count, const uint32_t seed, const size_t key_range, const std::initializer_list< Trace_Operation_Kind > kinds={ Trace_Operation_Kind::insert, Trace_Operation_Kind::erase, Trace_Operation_Kind::contains })
Build a deterministic pseudo-random operation trace.
std::vector< size_t > replay_trace_and_collect_mismatches(const std::vector< Trace_Operation > &trace, SubjectStep subject_step, ReferenceStep reference_step)
Replay a trace against a subject and a reference implementation.
Configuration for producer/consumer stress helpers.
One randomized operation trace entry.