ChimeraTK-cppext 01.05.02
Loading...
Searching...
No Matches
testExceptions.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"
9
10#include <thread>
11
12BOOST_AUTO_TEST_SUITE(testExceptions)
13
14struct MyException {
15 int value;
16};
17
18/*********************************************************************************************************************/
19
20BOOST_AUTO_TEST_CASE(testExceptions) {
21 // setup a queue
23
24 // push a value into the queue
25 q.push("Hello World");
26
27 // push an exception into the queue
28 try {
29 MyException e1;
30 e1.value = 42;
31 throw e1;
32 }
33 catch(MyException&) {
34 q.push_exception(std::current_exception());
35 }
36
37 // push another value
38 q.push("After exception");
39
40 // push a second exception into the queue
41 try {
42 MyException e2;
43 e2.value = 43;
44 throw e2;
45 }
46 catch(MyException&) {
47 q.push_exception(std::current_exception());
48 }
49
50 // push a third exception into the queue
51 try {
52 MyException e3;
53 e3.value = 44;
54 throw e3;
55 }
56 catch(MyException&) {
57 q.push_exception(std::current_exception());
58 }
59
60 // pop the first value from the queue
61 std::string v;
62 q.pop(v);
63 BOOST_CHECK_EQUAL(v, "Hello World");
64
65 // pop first exception from queue
66 try {
67 q.pop(v);
68 BOOST_ERROR("Exception expected.");
69 }
70 catch(MyException& ep1) {
71 BOOST_CHECK_EQUAL(ep1.value, 42);
72 }
73
74 // pop the second value from the queue
75 q.pop(v);
76 BOOST_CHECK_EQUAL(v, "After exception");
77
78 // pop second exception from queue
79 try {
80 q.pop_wait(v);
81 BOOST_ERROR("Exception expected.");
82 }
83 catch(MyException& ep2) {
84 BOOST_CHECK_EQUAL(ep2.value, 43);
85 }
86
87 // checkout third exception on queue without popping it
88 BOOST_CHECK(q.empty() == false);
89
90 try {
91 v = q.front();
92 BOOST_ERROR("Exception expected.");
93 }
94 catch(MyException& ef) {
95 BOOST_CHECK_EQUAL(ef.value, 44);
96 }
97
98 BOOST_CHECK(q.empty() == false);
99
100 // pop third exception from queue
101 try {
102 q.pop(v);
103 BOOST_ERROR("Exception expected.");
104 }
105 catch(MyException& ep3) {
106 BOOST_CHECK_EQUAL(ep3.value, 44);
107 }
108
109 BOOST_CHECK(q.empty() == true);
110}
111
112/*********************************************************************************************************************/
113
114BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(testExceptions)