1 #define BOOST_TEST_DYN_LINK
2 #define BOOST_TEST_NO_MAIN
4 #include <boost/test/unit_test.hpp>
13 BOOST_AUTO_TEST_SUITE(test_channel
)
15 BOOST_AUTO_TEST_CASE(test_object_queue
)
17 auto [sender
, receiver
] = pdns::channel::createObjectQueue
<MyObject
>();
19 BOOST_CHECK(receiver
.getDescriptor() != -1);
20 BOOST_CHECK_EQUAL(receiver
.isClosed(), false);
22 auto got
= receiver
.receive();
25 auto obj
= std::make_unique
<MyObject
>();
27 BOOST_CHECK_EQUAL(sender
.send(std::move(obj
)), true);
29 got
= receiver
.receive();
30 BOOST_CHECK(got
!= std::nullopt
&& *got
);
31 BOOST_CHECK_EQUAL((*got
)->a
, 42U);
34 BOOST_AUTO_TEST_CASE(test_object_queue_full
)
36 auto [sender
, receiver
] = pdns::channel::createObjectQueue
<MyObject
>();
39 auto got
= receiver
.receive();
43 /* add objects to the queue until it becomes full */
47 auto obj
= std::make_unique
<MyObject
>();
49 blocked
= !sender
.send(std::move(obj
));
59 BOOST_CHECK_GT(queued
, 1U);
65 auto got
= receiver
.receive();
74 BOOST_CHECK_EQUAL(queued
, received
);
76 /* we should be able to write again */
77 auto obj
= std::make_unique
<MyObject
>();
79 BOOST_CHECK(sender
.send(std::move(obj
)));
82 BOOST_AUTO_TEST_CASE(test_object_queue_throw_on_eof
)
84 auto [sender
, receiver
] = pdns::channel::createObjectQueue
<MyObject
>();
86 BOOST_CHECK_THROW(receiver
.receive(), std::runtime_error
);
87 BOOST_CHECK_EQUAL(receiver
.isClosed(), true);
90 BOOST_AUTO_TEST_CASE(test_object_queue_do_not_throw_on_eof
)
92 auto [sender
, receiver
] = pdns::channel::createObjectQueue
<MyObject
>(true, true, 0U, false);
94 auto got
= receiver
.receive();
95 BOOST_CHECK(got
== std::nullopt
);
96 BOOST_CHECK_EQUAL(receiver
.isClosed(), true);
99 BOOST_AUTO_TEST_CASE(test_notification_queue_full
)
101 auto [notifier
, waiter
] = pdns::channel::createNotificationQueue();
103 BOOST_CHECK(waiter
.getDescriptor() != -1);
104 BOOST_CHECK_EQUAL(waiter
.isClosed(), false);
107 /* add notifications until the queue becomes full */
108 bool blocked
= false;
110 blocked
= notifier
.notify();
113 /* clear the queue */
116 /* we should be able to write again */
117 BOOST_CHECK(notifier
.notify());
120 BOOST_AUTO_TEST_CASE(test_notification_queue_throw_on_eof
)
122 auto [notifier
, waiter
] = pdns::channel::createNotificationQueue();
124 BOOST_CHECK(waiter
.getDescriptor() != -1);
125 BOOST_CHECK_EQUAL(waiter
.isClosed(), false);
127 BOOST_CHECK_EQUAL(notifier
.notify(), true);
130 notifier
= pdns::channel::Notifier();
131 BOOST_CHECK_THROW(waiter
.clear(), std::runtime_error
);
134 BOOST_AUTO_TEST_CASE(test_notification_queue_do_not_throw_on_eof
)
136 auto [notifier
, waiter
] = pdns::channel::createNotificationQueue(true, 0, false);
138 BOOST_CHECK(waiter
.getDescriptor() != -1);
139 BOOST_CHECK_EQUAL(waiter
.isClosed(), false);
141 BOOST_CHECK_EQUAL(notifier
.notify(), true);
144 notifier
= pdns::channel::Notifier();
146 BOOST_CHECK_EQUAL(waiter
.isClosed(), true);
149 BOOST_AUTO_TEST_SUITE_END()