Skip to content

Commit

Permalink
feat(tests): Parametrize multi-threaded tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DNedic committed Dec 7, 2023
1 parent d879260 commit 37bd4c3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ add_executable(tests
mpmc/priority_queue.cpp
)

if (NOT DEFINED TEST_MT_TRANSFER_CNT)
set(TEST_MT_TRANSFER_CNT 10240)
endif()

target_compile_definitions(tests PRIVATE TEST_MT_TRANSFER_CNT=${TEST_MT_TRANSFER_CNT})

# Required in order to test the std::span API as well
target_compile_features(tests PRIVATE cxx_std_20)

Expand Down
9 changes: 9 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ ctest --output-on-failure --test-dir build/tests
```
or by executing the `build/tests/tests` binary.

## Multi-threaded tests
Apart from regular unit tests, the library also contains multi-threaded tests.
As these tests are not deterministic by their nature, and can give false negatives, the number of elements copied is parametrized.

To define the number of elements to transfer in multi-threaded tests, pass the `TEST_MT_TRANSFER_CNT` variable to CMake:
```
cmake -DTEST_MT_TRANSFER_CNT=100000 -B build
```

## Writing tests
If adding a new feature, or fixing a bug, it is necessary to add tests in order to avoid future regressions.
You can take a look at existing tests for examples.
Expand Down
5 changes: 2 additions & 3 deletions tests/spsc/bipartite_buf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ TEST_CASE("Multithreaded read/write multiple", "[bb_multithread_multiple]") {
std::vector<unsigned int> read;

const size_t data_size = 59; // Intentionally a prime number
const size_t elements_to_transfer = 2048;

// consumer
threads.emplace_back([&]() {
Expand All @@ -256,7 +255,7 @@ TEST_CASE("Multithreaded read/write multiple", "[bb_multithread_multiple]") {
bb.ReadRelease(read_region.second);
read_count += read_region.second;
}
} while (read_count < elements_to_transfer);
} while (read_count < TEST_MT_TRANSFER_CNT);
});

// producer
Expand All @@ -275,7 +274,7 @@ TEST_CASE("Multithreaded read/write multiple", "[bb_multithread_multiple]") {
written.insert(written.end(), &data[0], &data[data_size]);
write_count += data_size;
}
} while (write_count < elements_to_transfer);
} while (write_count < TEST_MT_TRANSFER_CNT);
});

for (auto &t : threads) {
Expand Down
4 changes: 2 additions & 2 deletions tests/spsc/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ TEST_CASE("Multithreaded read/write", "[q_multithread]") {
if (read_success) {
read.push_back(element);
}
} while (element < 2047);
} while (element < TEST_MT_TRANSFER_CNT);
});

// producer
Expand All @@ -115,7 +115,7 @@ TEST_CASE("Multithreaded read/write", "[q_multithread]") {
written.push_back(element);
element++;
}
} while (element < 2048);
} while (element < TEST_MT_TRANSFER_CNT + 1);
});

for (auto &t : threads) {
Expand Down
9 changes: 4 additions & 5 deletions tests/spsc/ring_buf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ TEST_CASE("Multithreaded read/write", "[rb_multithread]") {
if (read_success) {
read.push_back(data[0]);
}
} while (data[0] < 2047);
} while (data[0] < TEST_MT_TRANSFER_CNT);
});
// producer
threads.emplace_back([&]() {
Expand All @@ -349,7 +349,7 @@ TEST_CASE("Multithreaded read/write", "[rb_multithread]") {
written.push_back(cnt);
cnt++;
}
} while (cnt < 2048);
} while (cnt < TEST_MT_TRANSFER_CNT + 1);
});
for (auto &t : threads) {
t.join();
Expand All @@ -365,7 +365,6 @@ TEST_CASE("Multithreaded read/write multiple", "[rb_multithread_multiple]") {
std::vector<unsigned int> read;

const size_t data_size = 59; // Intentionally a prime number
const size_t elements_to_transfer = 2048;

// consumer
threads.emplace_back([&]() {
Expand All @@ -377,7 +376,7 @@ TEST_CASE("Multithreaded read/write multiple", "[rb_multithread_multiple]") {
read.insert(read.end(), &data[0], &data[data_size]);
read_count += data_size;
}
} while (read_count < elements_to_transfer);
} while (read_count < TEST_MT_TRANSFER_CNT);
});

// producer
Expand All @@ -394,7 +393,7 @@ TEST_CASE("Multithreaded read/write multiple", "[rb_multithread_multiple]") {
written.insert(written.end(), &data[0], &data[data_size]);
write_count += data_size;
}
} while (write_count < elements_to_transfer);
} while (write_count < TEST_MT_TRANSFER_CNT);
});

for (auto &t : threads) {
Expand Down

0 comments on commit 37bd4c3

Please sign in to comment.