]>
Commit | Line | Data |
---|---|---|
1c2d079d | 1 | #ifndef BOOST_TEST_DYN_LINK |
cd93bea9 | 2 | #define BOOST_TEST_DYN_LINK |
1c2d079d FM |
3 | #endif |
4 | ||
cd93bea9 RG |
5 | #define BOOST_TEST_NO_MAIN |
6 | ||
7 | #include <boost/test/unit_test.hpp> | |
8 | ||
9 | #include "channel.hh" | |
10 | ||
11 | struct MyObject | |
12 | { | |
13 | uint64_t a{0}; | |
14 | }; | |
15 | ||
16 | BOOST_AUTO_TEST_SUITE(test_channel) | |
17 | ||
3084c593 RG |
18 | BOOST_AUTO_TEST_CASE(test_object_queue) |
19 | { | |
cd93bea9 RG |
20 | auto [sender, receiver] = pdns::channel::createObjectQueue<MyObject>(); |
21 | ||
22 | BOOST_CHECK(receiver.getDescriptor() != -1); | |
23 | BOOST_CHECK_EQUAL(receiver.isClosed(), false); | |
24 | ||
25 | auto got = receiver.receive(); | |
26 | BOOST_CHECK(!got); | |
27 | ||
28 | auto obj = std::make_unique<MyObject>(); | |
29 | obj->a = 42U; | |
30 | BOOST_CHECK_EQUAL(sender.send(std::move(obj)), true); | |
31 | BOOST_CHECK(!obj); | |
32 | got = receiver.receive(); | |
33 | BOOST_CHECK(got != std::nullopt && *got); | |
34 | BOOST_CHECK_EQUAL((*got)->a, 42U); | |
35 | } | |
36 | ||
3084c593 RG |
37 | BOOST_AUTO_TEST_CASE(test_object_queue_full) |
38 | { | |
cd93bea9 RG |
39 | auto [sender, receiver] = pdns::channel::createObjectQueue<MyObject>(); |
40 | ||
41 | { | |
42 | auto got = receiver.receive(); | |
43 | BOOST_CHECK(!got); | |
44 | } | |
45 | ||
46 | /* add objects to the queue until it becomes full */ | |
47 | bool blocked = false; | |
48 | size_t queued = 0; | |
49 | while (!blocked) { | |
50 | auto obj = std::make_unique<MyObject>(); | |
51 | obj->a = 42U; | |
ebd45ede | 52 | blocked = !sender.send(std::move(obj)); |
cd93bea9 RG |
53 | if (blocked) { |
54 | BOOST_CHECK(obj); | |
55 | } | |
56 | else { | |
57 | BOOST_CHECK(!obj); | |
58 | ++queued; | |
59 | } | |
60 | } | |
61 | ||
62 | BOOST_CHECK_GT(queued, 1U); | |
63 | ||
64 | /* clear the queue */ | |
65 | blocked = false; | |
66 | size_t received = 0; | |
67 | while (!blocked) { | |
68 | auto got = receiver.receive(); | |
69 | if (got) { | |
70 | ++received; | |
71 | } | |
72 | else { | |
73 | blocked = true; | |
74 | } | |
75 | } | |
76 | ||
77 | BOOST_CHECK_EQUAL(queued, received); | |
78 | ||
79 | /* we should be able to write again */ | |
80 | auto obj = std::make_unique<MyObject>(); | |
81 | obj->a = 42U; | |
82 | BOOST_CHECK(sender.send(std::move(obj))); | |
204754fc RG |
83 | /* and to get it */ |
84 | { | |
85 | auto got = receiver.receive(); | |
86 | BOOST_CHECK(got); | |
87 | } | |
cd93bea9 RG |
88 | } |
89 | ||
3084c593 RG |
90 | BOOST_AUTO_TEST_CASE(test_object_queue_throw_on_eof) |
91 | { | |
cd93bea9 RG |
92 | auto [sender, receiver] = pdns::channel::createObjectQueue<MyObject>(); |
93 | sender.close(); | |
94 | BOOST_CHECK_THROW(receiver.receive(), std::runtime_error); | |
95 | BOOST_CHECK_EQUAL(receiver.isClosed(), true); | |
96 | } | |
97 | ||
3084c593 RG |
98 | BOOST_AUTO_TEST_CASE(test_object_queue_do_not_throw_on_eof) |
99 | { | |
c1d76521 | 100 | auto [sender, receiver] = pdns::channel::createObjectQueue<MyObject>(pdns::channel::SenderBlockingMode::SenderNonBlocking, pdns::channel::ReceiverBlockingMode::ReceiverNonBlocking, 0U, false); |
cd93bea9 RG |
101 | sender.close(); |
102 | auto got = receiver.receive(); | |
103 | BOOST_CHECK(got == std::nullopt); | |
104 | BOOST_CHECK_EQUAL(receiver.isClosed(), true); | |
105 | } | |
106 | ||
3084c593 RG |
107 | BOOST_AUTO_TEST_CASE(test_notification_queue_full) |
108 | { | |
cd93bea9 RG |
109 | auto [notifier, waiter] = pdns::channel::createNotificationQueue(); |
110 | ||
111 | BOOST_CHECK(waiter.getDescriptor() != -1); | |
112 | BOOST_CHECK_EQUAL(waiter.isClosed(), false); | |
113 | waiter.clear(); | |
114 | ||
115 | /* add notifications until the queue becomes full */ | |
116 | bool blocked = false; | |
3084c593 | 117 | while (!blocked) { |
cd93bea9 RG |
118 | blocked = notifier.notify(); |
119 | } | |
120 | ||
121 | /* clear the queue */ | |
122 | waiter.clear(); | |
123 | ||
124 | /* we should be able to write again */ | |
125 | BOOST_CHECK(notifier.notify()); | |
126 | } | |
127 | ||
3084c593 RG |
128 | BOOST_AUTO_TEST_CASE(test_notification_queue_throw_on_eof) |
129 | { | |
cd93bea9 RG |
130 | auto [notifier, waiter] = pdns::channel::createNotificationQueue(); |
131 | ||
132 | BOOST_CHECK(waiter.getDescriptor() != -1); | |
133 | BOOST_CHECK_EQUAL(waiter.isClosed(), false); | |
134 | ||
135 | BOOST_CHECK_EQUAL(notifier.notify(), true); | |
136 | waiter.clear(); | |
137 | ||
138 | notifier = pdns::channel::Notifier(); | |
139 | BOOST_CHECK_THROW(waiter.clear(), std::runtime_error); | |
140 | } | |
141 | ||
3084c593 RG |
142 | BOOST_AUTO_TEST_CASE(test_notification_queue_do_not_throw_on_eof) |
143 | { | |
cd93bea9 RG |
144 | auto [notifier, waiter] = pdns::channel::createNotificationQueue(true, 0, false); |
145 | ||
146 | BOOST_CHECK(waiter.getDescriptor() != -1); | |
147 | BOOST_CHECK_EQUAL(waiter.isClosed(), false); | |
148 | ||
149 | BOOST_CHECK_EQUAL(notifier.notify(), true); | |
150 | waiter.clear(); | |
151 | ||
152 | notifier = pdns::channel::Notifier(); | |
153 | waiter.clear(); | |
154 | BOOST_CHECK_EQUAL(waiter.isClosed(), true); | |
155 | } | |
156 | ||
157 | BOOST_AUTO_TEST_SUITE_END() |