36 TEST_CASE(
"PacketQueue's can be constructed.",
"[AddressPool]")
38 SECTION(
"Without a push callback.")
42 SECTION(
"With a push callback.")
45 auto ping = std::make_shared<packet_v2::Packet>(to_vector(PingV2()));
51 TEST_CASE(
"PacketQueue's 'push' adds a packet to the queue.",
"[PacketQueue]")
53 SECTION(
"Ensures the packet is not null.")
56 REQUIRE_THROWS_AS(queue.
push(
nullptr), std::invalid_argument);
58 queue.
push(
nullptr),
"Given packet pointer is null.");
60 SECTION(
"Calls the push callback.")
62 auto ping = std::make_shared<packet_v2::Packet>(to_vector(PingV2()));
68 REQUIRE_FALSE(called);
75 TEST_CASE(
"PacketQueue's 'empty' method determines if the queue is empty or " 76 "not.",
"[PacketQueue]")
78 auto ping = std::make_shared<packet_v2::Packet>(to_vector(PingV2()));
80 REQUIRE(queue.empty());
82 REQUIRE_FALSE(queue.empty());
86 TEST_CASE(
"PacketQueue's can be managed with 'push' and 'pop' methods.",
90 std::make_shared<packet_v2::Packet>(to_vector(HeartbeatV2()));
92 std::make_shared<packet_v2::Packet>(to_vector(PingV2()));
94 std::make_shared<packet_v2::Packet>(to_vector(SetModeV2()));
95 auto mission_set_current =
96 std::make_shared<packet_v2::Packet>(to_vector(MissionSetCurrentV2()));
97 auto encapsulated_data =
98 std::make_shared<packet_v2::Packet>(to_vector(EncapsulatedDataV2()));
99 auto param_ext_request_list =
100 std::make_shared<packet_v2::Packet>(to_vector(ParamExtRequestListV2()));
102 SECTION(
"Maintains order among the same priority")
106 queue.push(set_mode);
107 queue.push(mission_set_current, 0);
108 queue.push(encapsulated_data);
109 queue.push(param_ext_request_list, 0);
111 auto packet = queue.pop(0s);
112 REQUIRE(packet !=
nullptr);
115 packet = queue.pop(0s);
116 REQUIRE(packet !=
nullptr);
117 REQUIRE(*packet == *
ping);
119 packet = queue.pop(0s);
120 REQUIRE(packet !=
nullptr);
121 REQUIRE(*packet == *set_mode);
123 packet = queue.pop(0s);
124 REQUIRE(packet !=
nullptr);
125 REQUIRE(*packet == *mission_set_current);
127 packet = queue.pop(0s);
128 REQUIRE(packet !=
nullptr);
129 REQUIRE(*packet == *encapsulated_data);
131 packet = queue.pop(0s);
132 REQUIRE(packet !=
nullptr);
133 REQUIRE(*packet == *param_ext_request_list);
135 SECTION(
"Maintains priority order.")
139 queue.push(set_mode, 1);
140 queue.push(mission_set_current, -3);
141 queue.push(encapsulated_data, -2);
142 queue.push(param_ext_request_list, 3);
144 auto packet = queue.pop(0s);
145 REQUIRE(packet !=
nullptr);
146 REQUIRE(*packet == *param_ext_request_list);
148 packet = queue.pop(0s);
149 REQUIRE(packet !=
nullptr);
150 REQUIRE(*packet == *set_mode);
152 packet = queue.pop(0s);
153 REQUIRE(packet !=
nullptr);
154 REQUIRE(*packet == *
ping);
156 packet = queue.pop(0s);
157 REQUIRE(packet !=
nullptr);
160 packet = queue.pop(0s);
161 REQUIRE(packet !=
nullptr);
162 REQUIRE(*packet == *encapsulated_data);
164 packet = queue.pop(0s);
165 REQUIRE(packet !=
nullptr);
166 REQUIRE(*packet == *mission_set_current);
168 SECTION(
"Maintains order among the same priority as well as " 173 queue.push(set_mode);
174 queue.push(mission_set_current, 2);
175 queue.push(encapsulated_data);
176 queue.push(param_ext_request_list, 2);
178 auto packet = queue.pop();
179 REQUIRE(packet !=
nullptr);
180 REQUIRE(*packet == *
ping);
182 packet = queue.pop(0s);
183 REQUIRE(packet !=
nullptr);
184 REQUIRE(*packet == *mission_set_current);
186 packet = queue.pop(0s);
187 REQUIRE(packet !=
nullptr);
188 REQUIRE(*packet == *param_ext_request_list);
190 packet = queue.pop(0s);
191 REQUIRE(packet !=
nullptr);
194 packet = queue.pop(0s);
195 REQUIRE(packet !=
nullptr);
196 REQUIRE(*packet == *set_mode);
198 packet = queue.pop(0s);
199 REQUIRE(packet !=
nullptr);
200 REQUIRE(*packet == *encapsulated_data);
205 TEST_CASE(
"PacketQueue's 'pop' method blocks by default.",
"[PacketQueue]")
207 auto ping = std::make_shared<packet_v2::Packet>(to_vector(PingV2()));
209 SECTION(
"And will be released when a packet becomes available.")
211 auto future = std::async(std::launch::async, [&]()
215 REQUIRE(future.wait_for(0s) != std::future_status::ready);
217 auto result = future.get();
218 REQUIRE(result !=
nullptr);
219 REQUIRE(*result == *
ping);
221 SECTION(
"And will be released when the 'close' method is called.")
223 auto future1 = std::async(std::launch::async, [&]()
227 auto future2 = std::async(std::launch::async, [&]()
231 REQUIRE(future1.wait_for(0s) != std::future_status::ready);
232 REQUIRE(future2.wait_for(0s) != std::future_status::ready);
234 REQUIRE(future1.get() ==
nullptr);
235 REQUIRE(future2.get() ==
nullptr);
240 TEST_CASE(
"PacketQueue's 'pop' method optionally has a timeout.",
243 auto ping = std::make_shared<packet_v2::Packet>(to_vector(PingV2()));
245 SECTION(
"And will be released when a packet becomes available.")
247 auto future = std::async(std::launch::async, [&]()
249 return queue.pop(10s);
251 auto status = future.wait_for(0s);
252 REQUIRE(status != std::future_status::ready);
254 auto result = future.get();
255 REQUIRE(result !=
nullptr);
256 REQUIRE(*result == *
ping);
258 SECTION(
"And will be released when the timeout expires.")
260 auto future = std::async(std::launch::async, [&]()
262 return queue.pop(1ms);
264 auto status = future.wait_for(0s);
265 REQUIRE(future.wait_for(0ms) != std::future_status::ready);
266 REQUIRE(future.wait_for(10ms) == std::future_status::ready);
267 REQUIRE(future.get() ==
nullptr);
269 SECTION(
"And will be released when the 'close' method is called.")
271 auto future1 = std::async(std::launch::async, [&]()
273 return queue.pop(10s);
275 auto future2 = std::async(std::launch::async, [&]()
277 return queue.pop(10s);
279 REQUIRE(future1.wait_for(0s) != std::future_status::ready);
280 REQUIRE(future2.wait_for(0s) != std::future_status::ready);
282 REQUIRE(future1.get() ==
nullptr);
283 REQUIRE(future2.get() ==
nullptr);
288 TEST_CASE(
"PacketQueue's 'pop' method is non blocking when given a 0 second " 289 "timeout.",
"[PacketQueue]")
291 auto ping = std::make_shared<packet_v2::Packet>(to_vector(PingV2()));
293 SECTION(
"Returns the packet when it is available.")
296 auto packet = queue.pop(0s);
297 REQUIRE(packet !=
nullptr);
298 REQUIRE(*packet == *
ping);
300 SECTION(
"Returns nullptr when the queue is empty.")
302 REQUIRE(queue.pop(0s) ==
nullptr);
TEST_VIRTUAL void push(std::shared_ptr< const Packet > packet, int priority=0)
TEST_CASE("PacketQueue's can be constructed.", "[AddressPool]")