Skip to content

Commit

Permalink
fix(tests): Priority queue multithreaded test cleanup
Browse files Browse the repository at this point in the history
This cleans up the multithreaded test for the priority queue by
changing the variable names to be more clear, only declaring them in the scope
they are used in, formatting the code and removing
unnecessary comments.
  • Loading branch information
DNedic committed Dec 27, 2023
1 parent d2d8225 commit c6f16e3
Showing 1 changed file with 32 additions and 47 deletions.
79 changes: 32 additions & 47 deletions tests/spsc/priority_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,6 @@ TEST_CASE("Multithreaded read/write", "[pq_multithreaded]") {
std::vector<uint64_t> written;
std::vector<uint64_t> read;

/* The following code pushes a large number of values into a priority
queue using four different priorities. The priority is also encoded
into the value pushed, as its two lower significance bits.
Both consumer and producer store in two vectors the numbers written
and read.
After the multi-threaded execution, special code (described in detail
below) checks no higher priority value was written to the priority
queue at the time a lower priority one was read.
*/

// consumer, it just pops values from the queue and stores them in the
// main thread vector
threads.emplace_back([&]() {
Expand All @@ -93,8 +81,8 @@ TEST_CASE("Multithreaded read/write", "[pq_multithreaded]") {
do {
bool pop_success = queue.Pop(value);
if (pop_success) {
read.push_back(value);
cnt++;
read.push_back(value);
cnt++;
}
} while (cnt < TEST_MT_TRANSFER_CNT);
});
Expand All @@ -106,15 +94,16 @@ TEST_CASE("Multithreaded read/write", "[pq_multithreaded]") {
uint64_t value = 0;
uint8_t prio = 0;
do {
value = cnt << 2 + prio;
bool push_success = queue.Push(value, prio);
if (push_success) {
written.push_back(value);
prio = (prio + 1) % 4; // this could be also randomly generated
cnt++;
}
value = cnt << 2 + prio;
bool push_success = queue.Push(value, prio);
if (push_success) {
written.push_back(value);
prio = (prio + 1) % 4; // this could be also randomly generated
cnt++;
}
} while (cnt < TEST_MT_TRANSFER_CNT + 1);
});

for (auto &t : threads) {
t.join();
}
Expand All @@ -124,33 +113,29 @@ TEST_CASE("Multithreaded read/write", "[pq_multithreaded]") {
It needs to keep track which values were already read, it does that with
the help of the `consumed` Boolean vector.
*/
std::vector<bool> consumed(written.size(), false);
uint64_t value1, value2;
uint8_t prio1, prio2;
bool found;
for(size_t idx=0; idx<read.size(); idx++) {
// the value was read
value1 = read[idx];
// extract the priority encoded in the value
prio1 = value1 & ((1<<2) - 1);

found = false;
for(size_t idx2=0; idx2<written.size(); idx2++) {
if(consumed[idx2]) { // consumed values are skipped
continue;
}
// find when the value was written
value2 = written[idx2];
prio2 = value2 & ((1<<2) - 1);
if(written[idx2] == value1) {
consumed[idx2] = true; // this value is now accounted for
found = true;
break;
} else { // intermediate value, should be lower priority
REQUIRE(prio2 <= prio1);
std::vector<bool> consumed(written.size(), false);

for (size_t read_idx = 0; read_idx < read.size(); read_idx++) {
const uint64_t read_value = read[read_idx];
const uint64_t read_priority = read_value & ((1 << 2) - 1);

bool found_value = false;
for (size_t write_idx = 0; write_idx < written.size(); write_idx++) {
if (consumed[write_idx]) { // consumed values are skipped
continue;
}
// find when the value was written
const uint64_t written_value = written[write_idx];
const uint64_t written_priority = written_value & ((1 << 2) - 1);
if (written[write_idx] == read_value) {
consumed[write_idx] = true; // this value is now accounted for
found_value = true;
break;
} else { // intermediate value, should be lower priority
REQUIRE(written_priority <= read_value);
}
}
}
REQUIRE(found);
REQUIRE(found_value);
}
}

Expand Down

0 comments on commit c6f16e3

Please sign in to comment.