-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.cpp
More file actions
244 lines (202 loc) · 7.37 KB
/
Copy pathlibrary.cpp
File metadata and controls
244 lines (202 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "MPSCQueue.h"
#include <iostream>
#include <thread>
#include <utility>
#include <vector>
#include <atomic>
#include <mutex>
#include <unordered_set>
#include <chrono>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <string>
#include <sstream>
#include "Handles.h"
// Utility for test reporting
std::mutex cout_mutex;
#define SAFE_COUT(msg) { std::lock_guard<std::mutex> lock(cout_mutex); std::cout << msg << std::endl; }
// Test status tracking
struct TestResult {
bool success = true;
std::string message;
};
TestResult basic_test() {
SAFE_COUT("\n=== BASIC TEST (2 producers, 1 consumer) ===");
using namespace mpsc::sync_queue;
MPSCQueue<int, 4> queue;
auto producer_fn = [&queue](const int start_v) {
SAFE_COUT("Basic Producer " << start_v << " start");
for (int i = 0; i < 10; i++) {
queue.insert(start_v + i);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
SAFE_COUT("Basic Producer " << start_v << " end");
};
auto consumer_fn = [&queue]() {
SAFE_COUT("Basic Consumer start");
bool terminate = false;
while (!terminate) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
const auto val = queue.remove().value();
SAFE_COUT("Basic consumer retrieved: " << val);
terminate = val == 19; // Changed to 19 (10 + 9) to match last item from second producer
}
SAFE_COUT("Basic Consumer end");
};
std::vector<std::thread> threads;
threads.emplace_back(producer_fn, 0);
threads.emplace_back(producer_fn, 10);
threads.emplace_back(consumer_fn);
for (auto& t : threads) {
t.join();
}
return {true, "Basic test completed"};
}
// Simple struct to pass between producers and consumers
struct Item {
int producer_id;
int value;
};
// Simple stress test to verify MPSC safety
TestResult stress_test() {
SAFE_COUT("\n=== STRESS TEST (3 producers, 1 consumer) ===");
using namespace mpsc::sync_queue;
// Queue with 32 slots
MPSCQueue<Item, 5> queue;
// Configuration
constexpr int ITEMS_PER_PRODUCER = 1000;
constexpr int NUM_PRODUCERS = 2;
// Tracking
std::atomic<int> items_produced{0};
std::atomic<int> items_consumed{0};
std::mutex consumed_mutex;
std::unordered_set<int> consumed_values;
std::map<int, int> index_freqs;
bool found_duplicate = false;
// Producer function
auto producer_fn = [&](int id) {
SAFE_COUT("Producer " << id << " starting");
for (int i = 0; i < ITEMS_PER_PRODUCER; i++) {
// Each producer generates unique values
// Producer 0: 0, 3, 6, 9...
// Producer 1: 1, 4, 7, 10...
// Producer 2: 2, 5, 8, 11...
Item item{id, id + (i * NUM_PRODUCERS)};
queue.insert(std::move(item));
items_produced++;
// Small random delay (0-100 microseconds)
if (i % 50 == 0) {
std::this_thread::sleep_for(std::chrono::microseconds(rand() % 100));
}
}
SAFE_COUT("Producer " << id << " finished");
};
// Consumer function
auto consumer_fn = [&]() {
SAFE_COUT("Consumer starting");
while (items_consumed.load() < NUM_PRODUCERS * ITEMS_PER_PRODUCER) {
auto [idx, item] = queue.remove_dbg();
auto [producer_id, value] = item.value();
items_consumed++;
// Check for duplicates
{
std::lock_guard<std::mutex> lock(consumed_mutex);
index_freqs[idx] ++;
// Check if we've seen this value before
if (!consumed_values.insert(value).second) {
SAFE_COUT("ERROR: Duplicate value detected: " << value
<< " from producer " << producer_id << " at index " << idx);
SAFE_COUT("Frequency of removed indices: ");
for (auto&& [k, v] : index_freqs) {
SAFE_COUT(" " << k << ": " << v);
}
found_duplicate = true;
}
}
// Progress report
if (items_consumed % 500 == 0) {
SAFE_COUT("Progress: " << items_consumed << " items consumed");
}
}
SAFE_COUT("Consumer finished");
};
// Start all threads
std::vector<std::thread> threads;
// Start producers
for (int i = 0; i < NUM_PRODUCERS; i++) {
threads.emplace_back(producer_fn, i);
}
// Start consumer
threads.emplace_back(consumer_fn);
// Wait for all threads to finish
for (auto& t : threads) {
t.join();
}
// Verify results
bool success = true;
std::stringstream message;
// Check counts
if (items_produced != items_consumed) {
success = false;
message << "ERROR: Produced " << items_produced << " items but consumed "
<< items_consumed << "! ";
}
// Check for duplicates
if (found_duplicate) {
success = false;
message << "ERROR: Found duplicate values! ";
}
// Check correct count of unique values
if (consumed_values.size() != NUM_PRODUCERS * ITEMS_PER_PRODUCER) {
success = false;
message << "ERROR: Expected " << (NUM_PRODUCERS * ITEMS_PER_PRODUCER)
<< " unique values but got " << consumed_values.size() << "! ";
}
if (success) {
message << "Successfully processed " << items_consumed << " items with "
<< NUM_PRODUCERS << " producers and 1 consumer";
}
SAFE_COUT(message.str());
return {success, message.str()};
}
std::thread auto_exit_test1_util(bool* test_passed) {
auto [tx, rx] = mpsc::channel::create<int>();
auto consumer_fn = [rx = std::move(rx), &test_passed]() {
SAFE_COUT("Consumer starting");
*test_passed = !rx->remove().has_value();
SAFE_COUT("Consumer finished");
};
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::thread consumer_fn_thread(std::move(consumer_fn));
return consumer_fn_thread;
}
// Test to make sure the consumer gets unblocked if the queue is dropped
TestResult auto_exit_consumer_test1() {
SAFE_COUT("\n=== Auto exit consumer test 1: empty queue ===");
using namespace mpsc::sync_queue;
bool test_passed = true;
std::thread consumer_thread = auto_exit_test1_util(&test_passed);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
consumer_thread.join();
if (test_passed) {
return {true, "Consumer received empty value when the producer is dropped"};
}
return {false, "Auto exit consumer test 1 failed: expecting empty value to unblock consumer"};
}
int main() {
srand(static_cast<unsigned>(time(nullptr)));
using namespace mpsc::channel;
using namespace mpsc;
// Run tests
std::map<std::string, TestResult> results;
// results["Basic Test"] = basic_test();
results["Stress Test"] = stress_test();
// results["Auto exit Test 1"] = auto_exit_consumer_test1();
// Report results
SAFE_COUT("\n=== TEST RESULTS ===");
for (auto&& [k, v]: results) {
SAFE_COUT(k << ": " << (v.success ? "succeeded" : "failed") << " - " << v.message );
}
return 0;
}