Skip to content

Commit 70a5c6a

Browse files
author
Maxim Egorushkin
committed
Stress unit-test made invariant to the number of producers/consumers.
1 parent 400a510 commit 70a5c6a

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/tests.cc

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ void stress() {
2626
constexpr int PRODUCERS = 3;
2727
constexpr int CONSUMERS = 3;
2828
constexpr unsigned N = 1000000;
29+
using T = typename Queue::value_type;
30+
constexpr T STOP = -1;
2931

3032
Queue q;
3133
Barrier barrier;
@@ -34,7 +36,7 @@ void stress() {
3436
for(unsigned i = 0; i < PRODUCERS; ++i)
3537
producers[i] = std::thread([&q, &barrier, N=N]() {
3638
barrier.wait();
37-
for(unsigned n = N; n; --n)
39+
for(T n = N; n; --n)
3840
q.push(n);
3941
});
4042

@@ -44,31 +46,27 @@ void stress() {
4446
consumers[i] = std::thread([&q, &barrier, &r = results[i]]() {
4547
barrier.wait();
4648
uint64_t result = 0;
47-
for(;;) {
48-
unsigned n = q.pop();
49+
for(T n; (n = q.pop()) != STOP;)
4950
result += n;
50-
if(n == 1)
51-
break;
52-
}
5351
r = result;
5452
});
5553

5654
barrier.release(PRODUCERS + CONSUMERS);
57-
5855
for(auto& t : producers)
5956
t.join();
57+
for(int i = CONSUMERS; i--;)
58+
q.push(STOP);
6059
for(auto& t : consumers)
6160
t.join();
6261

6362
constexpr uint64_t expected_result = (N + 1) / 2. * N;
64-
6563
uint64_t result = 0;
6664
for(auto& r : results) {
6765
BOOST_WARN_GT(r, (expected_result / CONSUMERS) / 10); // Make sure a consumer didn't starve. False positives are possible here.
6866
result += r;
6967
}
7068

71-
int64_t result_diff = result / CONSUMERS - expected_result;
69+
int64_t result_diff = result / PRODUCERS - expected_result;
7270
BOOST_CHECK_EQUAL(result_diff, 0);
7371
}
7472

0 commit comments

Comments
 (0)