________________________________________________________________
| 65 | |
| 66 | // ________________________________________________________________ |
| 67 | TEST(ThreadSafeQueue, BufferSizeIsRespected) { |
| 68 | auto runTest = [](auto queue) { |
| 69 | std::atomic<size_t> numPushed = 0; |
| 70 | auto push = makePush(queue); |
| 71 | |
| 72 | // Asynchronous worker thread that pushes incremental values to the queue. |
| 73 | ad_utility::JThread t([&numPushed, &push, &queue] { |
| 74 | while (numPushed < numValues) { |
| 75 | push(numPushed++); |
| 76 | } |
| 77 | queue.finish(); |
| 78 | }); |
| 79 | |
| 80 | size_t numPopped = 0; |
| 81 | while (auto opt = queue.pop()) { |
| 82 | // We have only one thread pushing, so the elements in the queue are |
| 83 | // ordered. |
| 84 | EXPECT_EQ(opt.value(), numPopped); |
| 85 | ++numPopped; |
| 86 | // Check that the size of the queue is respected. The pushing thread must |
| 87 | // only continue to push once enough elements have been `pop`ped. The `+1` |
| 88 | // is necessary because the calls to `pop` and `push` are not synchronized |
| 89 | // with the atomic value `numPushed`. |
| 90 | EXPECT_LE(numPushed, numPopped + queueSize + 1); |
| 91 | } |
| 92 | }; |
| 93 | runWithBothQueueTypes(runTest); |
| 94 | } |
| 95 | |
| 96 | // _______________________________________________________________ |
| 97 | TEST(ThreadSafeQueue, ReturnValueOfPush) { |
nothing calls this directly
no test coverage detected