ChimeraTK-cppext 01.05.02
Loading...
Searching...
No Matches
testWhenAll.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#define BOOST_TEST_MODULE test_future_queue
4#include <boost/test/included/unit_test.hpp>
5using namespace boost::unit_test_framework;
6
7#include "future_queue.hpp"
8
9BOOST_AUTO_TEST_SUITE(testWhenAll)
10
11// test with a custom data type which is not known to the queue
12// we intentionally do not use a convenient standard-like interface to avoid
13// accidental usage of common operators etc.
14struct MovableDataType {
15 constexpr static int undef = -987654321;
17 explicit MovableDataType(int value) : _value(value) {}
18 MovableDataType(MovableDataType&& other) : _value(other._value) { other._value = undef; }
20 _value = other._value;
21 other._value = undef;
22 return *this;
23 }
24 int value() const { return _value; }
25
26 private:
27 int _value{undef};
28};
29constexpr int MovableDataType::undef;
30
31/*********************************************************************************************************************/
32
33BOOST_AUTO_TEST_CASE(singleThreaded) {
34 // create queues
35 std::vector<cppext::future_queue<MovableDataType>> q;
36 for(size_t i = 0; i < 20; ++i) q.emplace_back(10);
37
38 // create notification queue
39 auto nq = when_all(q.begin(), q.end());
40
41 // create threads filling the queues
42 std::vector<std::thread> threads;
43 for(size_t i = 0; i < 20; ++i) {
44 threads.emplace_back([q, i]() mutable {
45 for(size_t k = 0; k < 10; ++k) {
46 usleep(i * 1000);
47 MovableDataType x(i * 100 + k);
48 q[i].push(std::move(x));
49 }
50 });
51 }
52
53 // receive data after checking the notification queue
54 for(size_t k = 0; k < 10; ++k) {
55 nq.pop_wait();
56 for(size_t i = 0; i < 20; ++i) {
57 BOOST_CHECK(q[i].empty() == false);
59 q[i].pop(x);
60 BOOST_CHECK_EQUAL(x.value(), i * 100 + k);
61 }
62 }
63 usleep(100000);
64 BOOST_CHECK(nq.empty() == true);
65
66 for(auto& t : threads) t.join();
67}
68
69/*********************************************************************************************************************/
70
71BOOST_AUTO_TEST_SUITE_END()
MovableDataType(MovableDataType &&other)
MovableDataType & operator=(MovableDataType &&other)
static constexpr int undef
Definition testEmpty.cc:18
MovableDataType(int value)
int value() const
BOOST_AUTO_TEST_CASE(singleThreaded)