Debian Codegraph @

crt/aws-c-mqtt/tests/v3/operation_statistics_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include "mqtt_mock_server_handler.h"
7
8 #include <aws/mqtt/private/client_impl.h>
9
10 #include <aws/io/channel_bootstrap.h>
11 #include <aws/io/event_loop.h>
12 #include <aws/io/host_resolver.h>
13
14 #include <aws/common/clock.h>
15 #include <aws/common/condition_variable.h>
16
17 #include <aws/testing/aws_test_harness.h>
18
19 static const int TEST_LOG_SUBJECT = 60000;
20 static const int ONE_SEC = 1000000000;
21
22 struct received_publish_packet {
23 struct aws_byte_buf topic;
24 struct aws_byte_buf payload;
25 bool dup;
26 enum aws_mqtt_qos qos;
27 bool retain;
28 };
29
30 struct mqtt_connection_state_test {
31 struct aws_allocator *allocator;
32 struct aws_channel *server_channel;
33 struct aws_channel_handler *mock_server;
34 struct aws_client_bootstrap *client_bootstrap;
35 struct aws_server_bootstrap *server_bootstrap;
36 struct aws_event_loop_group *el_group;
37 struct aws_host_resolver *host_resolver;
38 struct aws_socket_endpoint endpoint;
39 struct aws_socket *listener;
40 struct aws_mqtt_client *mqtt_client;
41 struct aws_mqtt_client_connection *mqtt_connection;
42 struct aws_socket_options socket_options;
43 bool session_present;
44 bool connection_completed;
45 bool client_disconnect_completed;
46 bool server_disconnect_completed;
47 bool connection_interrupted;
48 bool connection_resumed;
49 bool subscribe_completed;
50 bool listener_destroyed;
51 int interruption_error;
52 int subscribe_complete_error;
53 int op_complete_error;
54 enum aws_mqtt_connect_return_code mqtt_return_code;
55 int error;
56 struct aws_condition_variable cvar;
57 struct aws_mutex lock;
58 /* any published messages from mock server, that you may not subscribe to. (Which should not happen in real life) */
59 struct aws_array_list any_published_messages; /* list of struct received_publish_packet */
60 size_t any_publishes_received;
61 size_t expected_any_publishes;
62 /* the published messages from mock server, that you did subscribe to. */
63 struct aws_array_list published_messages; /* list of struct received_publish_packet */
64 size_t publishes_received;
65 size_t expected_publishes;
66 /* The returned QoS from mock server */
67 struct aws_array_list qos_returned; /* list of uint_8 */
68 size_t ops_completed;
69 size_t expected_ops_completed;
70 };
71
72 static struct mqtt_connection_state_test test_data = {0};
73
74 /* ========== HELPER FUNCTIONS FOR THE TEST ========== */
75
76 static void s_operation_statistics_on_any_publish_received(
77 struct aws_mqtt_client_connection *connection,
78 const struct aws_byte_cursor *topic,
79 const struct aws_byte_cursor *payload,
80 bool dup,
81 enum aws_mqtt_qos qos,
82 bool retain,
83 void *userdata);
84
85 static void s_operation_statistics_on_incoming_channel_setup_fn(
86 struct aws_server_bootstrap *bootstrap,
87 int error_code,
88 struct aws_channel *channel,
89 void *user_data) {
90 (void)bootstrap;
91 struct mqtt_connection_state_test *state_test_data = user_data;
92
93 state_test_data->error = error_code;
94
95 if (!error_code) {
96 aws_mutex_lock(&state_test_data->lock);
97 state_test_data->server_disconnect_completed = false;
98 aws_mutex_unlock(&state_test_data->lock);
99 AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "server channel setup completed");
100
101 state_test_data->server_channel = channel;
102 struct aws_channel_slot *test_handler_slot = aws_channel_slot_new(channel);
103 aws_channel_slot_insert_end(channel, test_handler_slot);
104 mqtt_mock_server_handler_update_slot(state_test_data->mock_server, test_handler_slot);
105 aws_channel_slot_set_handler(test_handler_slot, state_test_data->mock_server);
106 }
107 }
108
109 static void s_operation_statistics_on_incoming_channel_shutdown_fn(
110 struct aws_server_bootstrap *bootstrap,
111 int error_code,
112 struct aws_channel *channel,
113 void *user_data) {
114 (void)bootstrap;
115 (void)error_code;
116 (void)channel;
117 struct mqtt_connection_state_test *state_test_data = user_data;
118 aws_mutex_lock(&state_test_data->lock);
119 state_test_data->server_disconnect_completed = true;
120 AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "server channel shutdown completed");
121 aws_mutex_unlock(&state_test_data->lock);
122 aws_condition_variable_notify_one(&state_test_data->cvar);
123 }
124
125 static void s_operation_statistics_on_listener_destroy(struct aws_server_bootstrap *bootstrap, void *user_data) {
126 (void)bootstrap;
127 struct mqtt_connection_state_test *state_test_data = user_data;
128 aws_mutex_lock(&state_test_data->lock);
129 state_test_data->listener_destroyed = true;
130 aws_mutex_unlock(&state_test_data->lock);
131 aws_condition_variable_notify_one(&state_test_data->cvar);
132 }
133
134 static bool s_operation_statistics_is_listener_destroyed(void *arg) {
135 struct mqtt_connection_state_test *state_test_data = arg;
136 return state_test_data->listener_destroyed;
137 }
138
139 static void s_operation_statistics_wait_on_listener_cleanup(struct mqtt_connection_state_test *state_test_data) {
140 aws_mutex_lock(&state_test_data->lock);
141 aws_condition_variable_wait_pred(
142 &state_test_data->cvar, &state_test_data->lock, s_operation_statistics_is_listener_destroyed, state_test_data);
143 aws_mutex_unlock(&state_test_data->lock);
144 }
145
146 static void s_operation_statistics_on_connection_interrupted(
147 struct aws_mqtt_client_connection *connection,
148 int error_code,
149 void *userdata) {
150 (void)connection;
151 (void)error_code;
152 struct mqtt_connection_state_test *state_test_data = userdata;
153
154 aws_mutex_lock(&state_test_data->lock);
155 state_test_data->connection_interrupted = true;
156 state_test_data->interruption_error = error_code;
157 aws_mutex_unlock(&state_test_data->lock);
158 AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "connection interrupted");
159 aws_condition_variable_notify_one(&state_test_data->cvar);
160 }
161
162 static void s_operation_statistics_on_connection_resumed(
163 struct aws_mqtt_client_connection *connection,
164 enum aws_mqtt_connect_return_code return_code,
165 bool session_present,
166 void *userdata) {
167 (void)connection;
168 (void)return_code;
169 (void)session_present;
170 AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "reconnect completed");
171
172 struct mqtt_connection_state_test *state_test_data = userdata;
173
174 aws_mutex_lock(&state_test_data->lock);
175 state_test_data->connection_resumed = true;
176 aws_mutex_unlock(&state_test_data->lock);
177 aws_condition_variable_notify_one(&state_test_data->cvar);
178 }
179
180 /** sets up a unix domain socket server and socket options. Creates an mqtt connection configured to use
181 * the domain socket.
182 */
183 static int s_operation_statistics_setup_mqtt_server_fn(struct aws_allocator *allocator, void *ctx) {
184 aws_mqtt_library_init(allocator);
185
186 struct mqtt_connection_state_test *state_test_data = ctx;
187
188 AWS_ZERO_STRUCT(*state_test_data);
189
190 state_test_data->allocator = allocator;
191 state_test_data->el_group = aws_event_loop_group_new_default(allocator, 1, NULL);
192
193 state_test_data->mock_server = new_mqtt_mock_server(allocator);
194 ASSERT_NOT_NULL(state_test_data->mock_server);
195
196 state_test_data->server_bootstrap = aws_server_bootstrap_new(allocator, state_test_data->el_group);
197 ASSERT_NOT_NULL(state_test_data->server_bootstrap);
198
199 struct aws_socket_options socket_options = {
200 .connect_timeout_ms = 100,
201 .domain = AWS_SOCKET_LOCAL,
202 };
203
204 state_test_data->socket_options = socket_options;
205 ASSERT_SUCCESS(aws_condition_variable_init(&state_test_data->cvar));
206 ASSERT_SUCCESS(aws_mutex_init(&state_test_data->lock));
207
208 aws_socket_endpoint_init_local_address_for_test(&state_test_data->endpoint);
209
210 struct aws_server_socket_channel_bootstrap_options server_bootstrap_options = {
211 .bootstrap = state_test_data->server_bootstrap,
212 .host_name = state_test_data->endpoint.address,
213 .port = state_test_data->endpoint.port,
214 .socket_options = &state_test_data->socket_options,
215 .incoming_callback = s_operation_statistics_on_incoming_channel_setup_fn,
216 .shutdown_callback = s_operation_statistics_on_incoming_channel_shutdown_fn,
217 .destroy_callback = s_operation_statistics_on_listener_destroy,
218 .user_data = state_test_data,
219 };
220 state_test_data->listener = aws_server_bootstrap_new_socket_listener(&server_bootstrap_options);
221
222 ASSERT_NOT_NULL(state_test_data->listener);
223
224 struct aws_host_resolver_default_options resolver_options = {
225 .el_group = state_test_data->el_group,
226 .max_entries = 1,
227 };
228 state_test_data->host_resolver = aws_host_resolver_new_default(allocator, &resolver_options);
229
230 struct aws_client_bootstrap_options bootstrap_options = {
231 .event_loop_group = state_test_data->el_group,
232 .user_data = state_test_data,
233 .host_resolver = state_test_data->host_resolver,
234 };
235
236 state_test_data->client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
237
238 state_test_data->mqtt_client = aws_mqtt_client_new(allocator, state_test_data->client_bootstrap);
239 state_test_data->mqtt_connection = aws_mqtt_client_connection_new(state_test_data->mqtt_client);
240 ASSERT_NOT_NULL(state_test_data->mqtt_connection);
241
242 ASSERT_SUCCESS(aws_mqtt_client_connection_set_connection_interruption_handlers(
243 state_test_data->mqtt_connection,
244 s_operation_statistics_on_connection_interrupted,
245 state_test_data,
246 s_operation_statistics_on_connection_resumed,
247 state_test_data));
248
249 ASSERT_SUCCESS(aws_mqtt_client_connection_set_on_any_publish_handler(
250 state_test_data->mqtt_connection, s_operation_statistics_on_any_publish_received, state_test_data));
251
252 ASSERT_SUCCESS(aws_array_list_init_dynamic(
253 &state_test_data->published_messages, allocator, 4, sizeof(struct received_publish_packet)));
254 ASSERT_SUCCESS(aws_array_list_init_dynamic(
255 &state_test_data->any_published_messages, allocator, 4, sizeof(struct received_publish_packet)));
256 ASSERT_SUCCESS(aws_array_list_init_dynamic(&state_test_data->qos_returned, allocator, 2, sizeof(uint8_t)));
257 return AWS_OP_SUCCESS;
258 }
259
260 static void s_received_publish_packet_list_clean_up(struct aws_array_list *list) {
261 for (size_t i = 0; i < aws_array_list_length(list); ++i) {
262 struct received_publish_packet *val_ptr = NULL;
263 aws_array_list_get_at_ptr(list, (void **)&val_ptr, i);
264 aws_byte_buf_clean_up(&val_ptr->payload);
265 aws_byte_buf_clean_up(&val_ptr->topic);
266 }
267 aws_array_list_clean_up(list);
268 }
269
270 static int s_operation_statistics_clean_up_mqtt_server_fn(
271 struct aws_allocator *allocator,
272 int setup_result,
273 void *ctx) {
274 (void)allocator;
275
276 if (!setup_result) {
277 struct mqtt_connection_state_test *state_test_data = ctx;
278
279 s_received_publish_packet_list_clean_up(&state_test_data->published_messages);
280 s_received_publish_packet_list_clean_up(&state_test_data->any_published_messages);
281 aws_array_list_clean_up(&state_test_data->qos_returned);
282 aws_mqtt_client_connection_release(state_test_data->mqtt_connection);
283 aws_mqtt_client_release(state_test_data->mqtt_client);
284 aws_client_bootstrap_release(state_test_data->client_bootstrap);
285 aws_host_resolver_release(state_test_data->host_resolver);
286 aws_server_bootstrap_destroy_socket_listener(state_test_data->server_bootstrap, state_test_data->listener);
287 s_operation_statistics_wait_on_listener_cleanup(state_test_data);
288 aws_server_bootstrap_release(state_test_data->server_bootstrap);
289 aws_event_loop_group_release(state_test_data->el_group);
290 destroy_mqtt_mock_server(state_test_data->mock_server);
291 }
292
293 aws_mqtt_library_clean_up();
294 return AWS_OP_SUCCESS;
295 }
296
297 static void s_operation_statistics_on_connection_complete_fn(
298 struct aws_mqtt_client_connection *connection,
299 int error_code,
300 enum aws_mqtt_connect_return_code return_code,
301 bool session_present,
302 void *userdata) {
303 (void)connection;
304 struct mqtt_connection_state_test *state_test_data = userdata;
305 aws_mutex_lock(&state_test_data->lock);
306
307 state_test_data->session_present = session_present;
308 state_test_data->mqtt_return_code = return_code;
309 state_test_data->error = error_code;
310 state_test_data->connection_completed = true;
311 aws_mutex_unlock(&state_test_data->lock);
312
313 aws_condition_variable_notify_one(&state_test_data->cvar);
314 }
315
316 static bool s_operation_statistics_is_connection_completed(void *arg) {
317 struct mqtt_connection_state_test *state_test_data = arg;
318 return state_test_data->connection_completed;
319 }
320
321 static void s_operation_statistics_wait_for_connection_to_complete(struct mqtt_connection_state_test *state_test_data) {
322 aws_mutex_lock(&state_test_data->lock);
323 aws_condition_variable_wait_pred(
324 &state_test_data->cvar,
325 &state_test_data->lock,
326 s_operation_statistics_is_connection_completed,
327 state_test_data);
328 state_test_data->connection_completed = false;
329 aws_mutex_unlock(&state_test_data->lock);
330 }
331
332 void s_operation_statistics_on_disconnect_fn(struct aws_mqtt_client_connection *connection, void *userdata) {
333 (void)connection;
334 struct mqtt_connection_state_test *state_test_data = userdata;
335 AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "disconnect completed");
336 aws_mutex_lock(&state_test_data->lock);
337 state_test_data->client_disconnect_completed = true;
338 aws_mutex_unlock(&state_test_data->lock);
339
340 aws_condition_variable_notify_one(&state_test_data->cvar);
341 }
342
343 static bool s_operation_statistics_is_disconnect_completed(void *arg) {
344 struct mqtt_connection_state_test *state_test_data = arg;
345 return state_test_data->client_disconnect_completed && state_test_data->server_disconnect_completed;
346 }
347
348 static void s_operation_statistics_wait_for_disconnect_to_complete(struct mqtt_connection_state_test *state_test_data) {
349 aws_mutex_lock(&state_test_data->lock);
350 aws_condition_variable_wait_pred(
351 &state_test_data->cvar,
352 &state_test_data->lock,
353 s_operation_statistics_is_disconnect_completed,
354 state_test_data);
355 state_test_data->client_disconnect_completed = false;
356 state_test_data->server_disconnect_completed = false;
357 aws_mutex_unlock(&state_test_data->lock);
358 }
359
360 static void s_operation_statistics_on_any_publish_received(
361 struct aws_mqtt_client_connection *connection,
362 const struct aws_byte_cursor *topic,
363 const struct aws_byte_cursor *payload,
364 bool dup,
365 enum aws_mqtt_qos qos,
366 bool retain,
367 void *userdata) {
368 (void)connection;
369 struct mqtt_connection_state_test *state_test_data = userdata;
370
371 struct aws_byte_buf payload_cp;
372 aws_byte_buf_init_copy_from_cursor(&payload_cp, state_test_data->allocator, *payload);
373 struct aws_byte_buf topic_cp;
374 aws_byte_buf_init_copy_from_cursor(&topic_cp, state_test_data->allocator, *topic);
375 struct received_publish_packet received_packet = {
376 .payload = payload_cp,
377 .topic = topic_cp,
378 .dup = dup,
379 .qos = qos,
380 .retain = retain,
381 };
382
383 aws_mutex_lock(&state_test_data->lock);
384 aws_array_list_push_back(&state_test_data->any_published_messages, &received_packet);
385 state_test_data->any_publishes_received++;
386 aws_mutex_unlock(&state_test_data->lock);
387 aws_condition_variable_notify_one(&state_test_data->cvar);
388 }
389
390 static void s_operation_statistics_on_publish_received(
391 struct aws_mqtt_client_connection *connection,
392 const struct aws_byte_cursor *topic,
393 const struct aws_byte_cursor *payload,
394 bool dup,
395 enum aws_mqtt_qos qos,
396 bool retain,
397 void *userdata) {
398
399 (void)connection;
400 (void)topic;
401 struct mqtt_connection_state_test *state_test_data = userdata;
402
403 struct aws_byte_buf payload_cp;
404 aws_byte_buf_init_copy_from_cursor(&payload_cp, state_test_data->allocator, *payload);
405 struct aws_byte_buf topic_cp;
406 aws_byte_buf_init_copy_from_cursor(&topic_cp, state_test_data->allocator, *topic);
407 struct received_publish_packet received_packet = {
408 .payload = payload_cp,
409 .topic = topic_cp,
410 .dup = dup,
411 .qos = qos,
412 .retain = retain,
413 };
414
415 aws_mutex_lock(&state_test_data->lock);
416 aws_array_list_push_back(&state_test_data->published_messages, &received_packet);
417 state_test_data->publishes_received++;
418 aws_mutex_unlock(&state_test_data->lock);
419 aws_condition_variable_notify_one(&state_test_data->cvar);
420 }
421
422 static void s_operation_statistics_on_suback(
423 struct aws_mqtt_client_connection *connection,
424 uint16_t packet_id,
425 const struct aws_byte_cursor *topic,
426 enum aws_mqtt_qos qos,
427 int error_code,
428 void *userdata) {
429 (void)connection;
430 (void)packet_id;
431 (void)topic;
432
433 struct mqtt_connection_state_test *state_test_data = userdata;
434
435 aws_mutex_lock(&state_test_data->lock);
436 if (!error_code) {
437 aws_array_list_push_back(&state_test_data->qos_returned, &qos);
438 }
439 state_test_data->subscribe_completed = true;
440 state_test_data->subscribe_complete_error = error_code;
441 aws_mutex_unlock(&state_test_data->lock);
442 aws_condition_variable_notify_one(&state_test_data->cvar);
443 }
444
445 static void s_on_op_complete(
446 struct aws_mqtt_client_connection *connection,
447 uint16_t packet_id,
448 int error_code,
449 void *userdata) {
450 (void)connection;
451 (void)packet_id;
452
453 struct mqtt_connection_state_test *state_test_data = userdata;
454 AWS_LOGF_DEBUG(TEST_LOG_SUBJECT, "pub op completed");
455 aws_mutex_lock(&state_test_data->lock);
456 state_test_data->ops_completed++;
457 state_test_data->op_complete_error = error_code;
458 aws_mutex_unlock(&state_test_data->lock);
459 aws_condition_variable_notify_one(&state_test_data->cvar);
460 }
461
462 static bool s_is_ops_completed(void *arg) {
463 struct mqtt_connection_state_test *state_test_data = arg;
464 return state_test_data->ops_completed == state_test_data->expected_ops_completed;
465 }
466
467 static void s_wait_for_ops_completed(struct mqtt_connection_state_test *state_test_data) {
468 aws_mutex_lock(&state_test_data->lock);
469 aws_condition_variable_wait_for_pred(
470 &state_test_data->cvar, &state_test_data->lock, 10000000000, s_is_ops_completed, state_test_data);
471 aws_mutex_unlock(&state_test_data->lock);
472 }
473
474 static bool s_operation_statistics_is_subscribe_completed(void *arg) {
475 struct mqtt_connection_state_test *state_test_data = arg;
476 return state_test_data->subscribe_completed;
477 }
478
479 static void s_operation_statistics_wait_for_subscribe_to_complete(struct mqtt_connection_state_test *state_test_data) {
480 aws_mutex_lock(&state_test_data->lock);
481 aws_condition_variable_wait_pred(
482 &state_test_data->cvar, &state_test_data->lock, s_operation_statistics_is_subscribe_completed, state_test_data);
483 state_test_data->subscribe_completed = false;
484 aws_mutex_unlock(&state_test_data->lock);
485 }
486
487 /* ========== PUBLISH TESTS ========== */
488
489 /**
490 * Make a connection, tell the server NOT to send Acks, publish and immediately check the statistics to make sure
491 * it is incomplete, then wait a little bit and check that it was properly marked as UnAcked, then send the PubAck
492 * confirm statistics are zero, and then disconnect
493 */
494 static int s_test_mqtt_operation_statistics_simple_publish(struct aws_allocator *allocator, void *ctx) {
495 (void)allocator;
496 struct mqtt_connection_state_test *state_test_data = ctx;
497
498 struct aws_mqtt_connection_options connection_options = {
499 .user_data = state_test_data,
500 .clean_session = false,
501 .client_id = aws_byte_cursor_from_c_str("client1234"),
502 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
503 .socket_options = &state_test_data->socket_options,
504 .on_connection_complete = s_operation_statistics_on_connection_complete_fn,
505 };
506
507 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
508 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message");
509
510 /* Connect */
511 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
512 s_operation_statistics_wait_for_connection_to_complete(state_test_data);
513
514 /* Stop ACKS so we make sure the operation statistics has time to allow us to identify we sent a packet */
515 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
516
517 /* We want to wait for 1 operation to complete */
518 aws_mutex_lock(&state_test_data->lock);
519 state_test_data->expected_ops_completed = 1;
520 aws_mutex_unlock(&state_test_data->lock);
521
522 /* Publish a packet */
523 uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
524 state_test_data->mqtt_connection,
525 &pub_topic,
526 AWS_MQTT_QOS_AT_LEAST_ONCE,
527 false,
528 &payload_1,
529 s_on_op_complete,
530 state_test_data);
531 ASSERT_TRUE(packet_id_1 > 0);
532
533 /* Wait a little bit to allow the code to put the packet into the socket from the queue, allowing it
534 * to be unacked. If we check right away, we may or may not see it in the un-acked statistics */
535 aws_thread_current_sleep((uint64_t)ONE_SEC);
536
537 /* Make sure the sizes are correct and there is only one operation waiting
538 * (The size of the topic, the size of the payload, 2 for the header, 2 for the packet ID) */
539 uint64_t expected_packet_size = pub_topic.len + payload_1.len + 4;
540 struct aws_mqtt_connection_operation_statistics operation_statistics;
541 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
542 ASSERT_INT_EQUALS(1, operation_statistics.incomplete_operation_count);
543 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.incomplete_operation_size);
544 ASSERT_INT_EQUALS(1, operation_statistics.unacked_operation_count);
545 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.unacked_operation_size);
546
547 /* Send the PubAck and wait for the client to receive it */
548 mqtt_mock_server_send_puback(state_test_data->mock_server, packet_id_1);
549 s_wait_for_ops_completed(state_test_data);
550
551 /* Make sure the operation values are back to zero now that the publish went out */
552 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
553 ASSERT_INT_EQUALS(0, operation_statistics.incomplete_operation_count);
554 ASSERT_INT_EQUALS(0, operation_statistics.incomplete_operation_size);
555 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_count);
556 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_size);
557
558 /* Disconnect */
559 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
560 state_test_data->mqtt_connection, s_operation_statistics_on_disconnect_fn, state_test_data));
561 s_operation_statistics_wait_for_disconnect_to_complete(state_test_data);
562
563 return AWS_OP_SUCCESS;
564 }
565
566 AWS_TEST_CASE_FIXTURE(
567 mqtt_operation_statistics_simple_publish,
568 s_operation_statistics_setup_mqtt_server_fn,
569 s_test_mqtt_operation_statistics_simple_publish,
570 s_operation_statistics_clean_up_mqtt_server_fn,
571 &test_data)
572
573 /**
574 * Make five publishes offline, confirm they are in the incomplete statistics, make a connection to a server
575 * that does not send ACKs, send ConnAck, confirm five publishes are in unacked statistics, send PubAcks,
576 * confirm operation statistics are zero, and then disconnect
577 */
578 static int s_test_mqtt_operation_statistics_offline_publish(struct aws_allocator *allocator, void *ctx) {
579 (void)allocator;
580 struct mqtt_connection_state_test *state_test_data = ctx;
581
582 struct aws_mqtt_connection_options connection_options = {
583 .user_data = state_test_data,
584 .clean_session = false,
585 .client_id = aws_byte_cursor_from_c_str("client1234"),
586 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
587 .socket_options = &state_test_data->socket_options,
588 .on_connection_complete = s_operation_statistics_on_connection_complete_fn,
589 };
590
591 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic_1");
592 struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Test Message");
593
594 /* Stop ACKS so we make sure the operation statistics has time to allow us to identify we sent a packet */
595 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
596
597 uint16_t pub_packet_id_1 = 0;
598 uint16_t pub_packet_id_2 = 0;
599 uint16_t pub_packet_id_3 = 0;
600 uint16_t pub_packet_id_4 = 0;
601 uint16_t pub_packet_id_5 = 0;
602
603 /* Publish the five packets */
604 for (int i = 0; i < 5; i++) {
605 uint16_t packet = aws_mqtt_client_connection_publish(
606 state_test_data->mqtt_connection,
607 &pub_topic,
608 AWS_MQTT_QOS_AT_LEAST_ONCE,
609 false,
610 &payload,
611 s_on_op_complete,
612 state_test_data);
613 ASSERT_TRUE(packet > 0);
614
615 if (i == 0) {
616 pub_packet_id_1 = packet;
617 } else if (i == 1) {
618 pub_packet_id_2 = packet;
619 } else if (i == 2) {
620 pub_packet_id_3 = packet;
621 } else if (i == 3) {
622 pub_packet_id_4 = packet;
623 } else {
624 pub_packet_id_5 = packet;
625 }
626 }
627
628 /* Wait a little bit to make sure the client has processed them (and NOT put them in the un-acked statistics) */
629 aws_thread_current_sleep((uint64_t)ONE_SEC);
630
631 /* Make sure the sizes are correct and there is five operations waiting
632 * Each packet size = (The size of the topic, the size of the payload, 2 for the header, 2 for the packet ID) */
633 uint64_t expected_packet_size = (pub_topic.len + payload.len + 4) * 5;
634 struct aws_mqtt_connection_operation_statistics operation_statistics;
635 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
636 ASSERT_INT_EQUALS(5, operation_statistics.incomplete_operation_count);
637 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.incomplete_operation_size);
638 // The UnAcked operations should be zero, because we are NOT connected
639 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_count);
640 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_size);
641
642 /* Connect */
643 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
644 s_operation_statistics_wait_for_connection_to_complete(state_test_data);
645
646 /* We want to wait for 5 operations to complete */
647 aws_mutex_lock(&state_test_data->lock);
648 state_test_data->expected_ops_completed = 5;
649 aws_mutex_unlock(&state_test_data->lock);
650
651 /* Send the PubAck for each packet and wait for the client to receive it */
652 mqtt_mock_server_send_puback(state_test_data->mock_server, pub_packet_id_1);
653 mqtt_mock_server_send_puback(state_test_data->mock_server, pub_packet_id_2);
654 mqtt_mock_server_send_puback(state_test_data->mock_server, pub_packet_id_3);
655 mqtt_mock_server_send_puback(state_test_data->mock_server, pub_packet_id_4);
656 mqtt_mock_server_send_puback(state_test_data->mock_server, pub_packet_id_5);
657 s_wait_for_ops_completed(state_test_data);
658
659 /* Make sure the operation values are back to zero now that the publish went out */
660 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
661 ASSERT_INT_EQUALS(0, operation_statistics.incomplete_operation_count);
662 ASSERT_INT_EQUALS(0, operation_statistics.incomplete_operation_size);
663 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_count);
664 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_size);
665
666 /* Disconnect */
667 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
668 state_test_data->mqtt_connection, s_operation_statistics_on_disconnect_fn, state_test_data));
669 s_operation_statistics_wait_for_disconnect_to_complete(state_test_data);
670
671 return AWS_OP_SUCCESS;
672 }
673
674 AWS_TEST_CASE_FIXTURE(
675 mqtt_operation_statistics_offline_publish,
676 s_operation_statistics_setup_mqtt_server_fn,
677 s_test_mqtt_operation_statistics_offline_publish,
678 s_operation_statistics_clean_up_mqtt_server_fn,
679 &test_data)
680
681 /**
682 * Make five publishes offline and confirm the operation statistics properly tracks them only in the incomplete
683 * operations, before connecting and confirming post-connect they are also in unacked in the operation statistics.
684 * Then disconnect and confirm the operation statistics are still correct post-disconnect
685 */
686 static int s_test_mqtt_operation_statistics_disconnect_publish(struct aws_allocator *allocator, void *ctx) {
687 (void)allocator;
688 struct mqtt_connection_state_test *state_test_data = ctx;
689
690 struct aws_mqtt_connection_options connection_options = {
691 .user_data = state_test_data,
692 .clean_session = false,
693 .client_id = aws_byte_cursor_from_c_str("client1234"),
694 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
695 .socket_options = &state_test_data->socket_options,
696 .on_connection_complete = s_operation_statistics_on_connection_complete_fn,
697 };
698
699 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic_1");
700 struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Test Message");
701
702 /* Stop ACKS so we make sure the operation statistics has time to allow us to identify we sent a packet */
703 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
704
705 /* Publish the five packets */
706 for (int i = 0; i < 5; i++) {
707 uint16_t packet = aws_mqtt_client_connection_publish(
708 state_test_data->mqtt_connection,
709 &pub_topic,
710 AWS_MQTT_QOS_AT_LEAST_ONCE,
711 false,
712 &payload,
713 s_on_op_complete,
714 state_test_data);
715 ASSERT_TRUE(packet > 0);
716 }
717
718 /* Wait a little bit to make sure the client has processed them (and NOT put them in the un-acked statistics) */
719 aws_thread_current_sleep((uint64_t)ONE_SEC);
720
721 /* Make sure the sizes are correct and there is five operations waiting
722 * Each packet size = (The size of the topic, the size of the payload, 2 for the header, 2 for the packet ID) */
723 uint64_t expected_packet_size = (pub_topic.len + payload.len + 4) * 5;
724 struct aws_mqtt_connection_operation_statistics operation_statistics;
725 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
726 ASSERT_INT_EQUALS(5, operation_statistics.incomplete_operation_count);
727 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.incomplete_operation_size);
728 // The UnAcked operations should be zero, because we are NOT connected
729 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_count);
730 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_size);
731
732 /* Connect */
733 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
734 s_operation_statistics_wait_for_connection_to_complete(state_test_data);
735
736 /* Wait a little bit to make sure the client has had a chance to put the publishes out */
737 aws_thread_current_sleep((uint64_t)ONE_SEC);
738
739 /* Confirm the UnAcked operations are now correct as well */
740 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
741 ASSERT_INT_EQUALS(5, operation_statistics.incomplete_operation_count);
742 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.incomplete_operation_size);
743 ASSERT_INT_EQUALS(5, operation_statistics.unacked_operation_count);
744 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.unacked_operation_size);
745
746 /* Disconnect */
747 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
748 state_test_data->mqtt_connection, s_operation_statistics_on_disconnect_fn, state_test_data));
749 s_operation_statistics_wait_for_disconnect_to_complete(state_test_data);
750
751 /* Wait a little bit just to make sure the client has fully processed the shutdown */
752 aws_thread_current_sleep((uint64_t)ONE_SEC);
753
754 /* Confirm the operation statistics are still correctly tracking post-disconnect */
755 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
756 ASSERT_INT_EQUALS(5, operation_statistics.incomplete_operation_count);
757 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.incomplete_operation_size);
758 ASSERT_INT_EQUALS(5, operation_statistics.unacked_operation_count);
759 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.unacked_operation_size);
760
761 return AWS_OP_SUCCESS;
762 }
763
764 AWS_TEST_CASE_FIXTURE(
765 mqtt_operation_statistics_disconnect_publish,
766 s_operation_statistics_setup_mqtt_server_fn,
767 s_test_mqtt_operation_statistics_disconnect_publish,
768 s_operation_statistics_clean_up_mqtt_server_fn,
769 &test_data)
770
771 /**
772 * Makes a publish offline, checks operation statistics, connects to non-ACK sending server, checks operation
773 * statistics, makes another publish while online, checks operation statistics, disconnects, makes another publish, and
774 * finally checks operation statistics one last time.
775 */
776 static int s_test_mqtt_operation_statistics_reconnect_publish(struct aws_allocator *allocator, void *ctx) {
777 (void)allocator;
778 struct mqtt_connection_state_test *state_test_data = ctx;
779
780 struct aws_mqtt_connection_options connection_options = {
781 .user_data = state_test_data,
782 .clean_session = false,
783 .client_id = aws_byte_cursor_from_c_str("client1234"),
784 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
785 .socket_options = &state_test_data->socket_options,
786 .on_connection_complete = s_operation_statistics_on_connection_complete_fn,
787 };
788
789 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic_1");
790 struct aws_byte_cursor payload = aws_byte_cursor_from_c_str("Test Message");
791
792 /* Stop ACKS so we make sure the operation statistics has time to allow us to identify we sent a packet */
793 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
794
795 /* First publish! */
796 aws_mutex_lock(&state_test_data->lock);
797 state_test_data->expected_ops_completed = 1;
798 aws_mutex_unlock(&state_test_data->lock);
799 uint64_t pub_packet_id_1 = aws_mqtt_client_connection_publish(
800 state_test_data->mqtt_connection,
801 &pub_topic,
802 AWS_MQTT_QOS_AT_LEAST_ONCE,
803 false,
804 &payload,
805 s_on_op_complete,
806 state_test_data);
807 ASSERT_TRUE(pub_packet_id_1 > 0);
808 s_wait_for_ops_completed(state_test_data);
809
810 /* Make sure the sizes are correct and there is five operations waiting
811 * Each packet size = (The size of the topic, the size of the payload, 2 for the header, 2 for the packet ID) */
812 uint64_t expected_packet_size = (pub_topic.len + payload.len + 4);
813 struct aws_mqtt_connection_operation_statistics operation_statistics;
814 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
815 ASSERT_INT_EQUALS(1, operation_statistics.incomplete_operation_count);
816 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.incomplete_operation_size);
817 // The UnAcked operations should be zero, because we are NOT connected
818 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_count);
819 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_size);
820
821 /* Connect */
822 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
823 s_operation_statistics_wait_for_connection_to_complete(state_test_data);
824
825 /* Wait a second to give the MQTT311 client time to move the offline publish to unacked */
826 aws_thread_current_sleep((uint64_t)ONE_SEC);
827
828 /* Confirm the UnAcked operations are now correct as well */
829 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
830 ASSERT_INT_EQUALS(1, operation_statistics.incomplete_operation_count);
831 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.incomplete_operation_size);
832 ASSERT_INT_EQUALS(1, operation_statistics.unacked_operation_count);
833 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.unacked_operation_size);
834
835 /* Second publish! */
836 aws_mutex_lock(&state_test_data->lock);
837 state_test_data->expected_ops_completed = 1;
838 aws_mutex_unlock(&state_test_data->lock);
839 uint64_t pub_packet_id_2 = aws_mqtt_client_connection_publish(
840 state_test_data->mqtt_connection,
841 &pub_topic,
842 AWS_MQTT_QOS_AT_LEAST_ONCE,
843 false,
844 &payload,
845 s_on_op_complete,
846 state_test_data);
847 ASSERT_TRUE(pub_packet_id_2 > 0);
848 s_wait_for_ops_completed(state_test_data);
849
850 /* Confirm both publishes are correct across all statistics */
851 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
852 ASSERT_INT_EQUALS(2, operation_statistics.incomplete_operation_count);
853 ASSERT_INT_EQUALS(expected_packet_size * 2, operation_statistics.incomplete_operation_size);
854 ASSERT_INT_EQUALS(2, operation_statistics.unacked_operation_count);
855 ASSERT_INT_EQUALS(expected_packet_size * 2, operation_statistics.unacked_operation_size);
856
857 /* Disconnect */
858 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
859 state_test_data->mqtt_connection, s_operation_statistics_on_disconnect_fn, state_test_data));
860 s_operation_statistics_wait_for_disconnect_to_complete(state_test_data);
861
862 /* Third publish! */
863 aws_mutex_lock(&state_test_data->lock);
864 state_test_data->expected_ops_completed = 1;
865 aws_mutex_unlock(&state_test_data->lock);
866 uint64_t pub_packet_id_3 = aws_mqtt_client_connection_publish(
867 state_test_data->mqtt_connection,
868 &pub_topic,
869 AWS_MQTT_QOS_AT_LEAST_ONCE,
870 false,
871 &payload,
872 s_on_op_complete,
873 state_test_data);
874 ASSERT_TRUE(pub_packet_id_3 > 0);
875 s_wait_for_ops_completed(state_test_data);
876
877 /* Confirm all three publishes are in the incomplete statistics, but only two are in unacked */
878 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
879 ASSERT_INT_EQUALS(3, operation_statistics.incomplete_operation_count);
880 ASSERT_INT_EQUALS(expected_packet_size * 3, operation_statistics.incomplete_operation_size);
881 ASSERT_INT_EQUALS(2, operation_statistics.unacked_operation_count);
882 ASSERT_INT_EQUALS(expected_packet_size * 2, operation_statistics.unacked_operation_size);
883
884 return AWS_OP_SUCCESS;
885 }
886
887 AWS_TEST_CASE_FIXTURE(
888 mqtt_operation_statistics_reconnect_publish,
889 s_operation_statistics_setup_mqtt_server_fn,
890 s_test_mqtt_operation_statistics_reconnect_publish,
891 s_operation_statistics_clean_up_mqtt_server_fn,
892 &test_data)
893
894 /* ========== SUBSCRIBE TESTS ========== */
895
896 /**
897 * Make a connection, tell the server NOT to send Acks, subscribe and check the statistics to make sure
898 * it is incomplete, then wait a little bit and check that it was properly marked as UnAcked, then send the SubAck
899 * confirm statistics are zero, and then disconnect
900 */
901 static int s_test_mqtt_operation_statistics_simple_subscribe(struct aws_allocator *allocator, void *ctx) {
902 (void)allocator;
903 struct mqtt_connection_state_test *state_test_data = ctx;
904
905 struct aws_mqtt_connection_options connection_options = {
906 .user_data = state_test_data,
907 .clean_session = false,
908 .client_id = aws_byte_cursor_from_c_str("client1234"),
909 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
910 .socket_options = &state_test_data->socket_options,
911 .on_connection_complete = s_operation_statistics_on_connection_complete_fn,
912 };
913
914 struct aws_byte_cursor sub_topic = aws_byte_cursor_from_c_str("/test/topic");
915
916 /* Connect */
917 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
918 s_operation_statistics_wait_for_connection_to_complete(state_test_data);
919
920 /* We want to wait for 1 operation */
921 aws_mutex_lock(&state_test_data->lock);
922 state_test_data->expected_ops_completed = 1;
923 aws_mutex_unlock(&state_test_data->lock);
924
925 /* Stop ACKS so we make sure the operation statistics has time to allow us to identify we sent a packet */
926 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
927
928 // Send a subscribe packet
929 uint16_t packet_id_1 = aws_mqtt_client_connection_subscribe(
930 state_test_data->mqtt_connection,
931 &sub_topic,
932 AWS_MQTT_QOS_AT_LEAST_ONCE,
933 s_operation_statistics_on_publish_received,
934 state_test_data,
935 NULL,
936 s_operation_statistics_on_suback,
937 state_test_data);
938 ASSERT_TRUE(packet_id_1 > 0);
939
940 /* Wait a little bit to allow the code to put the packet into the socket from the queue, allowing it
941 * to be unacked. If we check right away, we may or may not see it in the un-acked statistics */
942 aws_thread_current_sleep((uint64_t)ONE_SEC);
943
944 /* Make sure the sizes are correct and there is only one operation waiting
945 * (The size of the topic + 3 (QoS, MSB, LSB), 2 for the header, 2 for the packet ID) */
946 uint64_t expected_packet_size = sub_topic.len + 7;
947 struct aws_mqtt_connection_operation_statistics operation_statistics;
948 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
949 ASSERT_INT_EQUALS(1, operation_statistics.incomplete_operation_count);
950 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.incomplete_operation_size);
951 ASSERT_INT_EQUALS(1, operation_statistics.unacked_operation_count);
952 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.unacked_operation_size);
953
954 /* Send the SubAck and wait for the client to get the ACK */
955 mqtt_mock_server_send_single_suback(state_test_data->mock_server, packet_id_1, AWS_MQTT_QOS_AT_LEAST_ONCE);
956 s_wait_for_ops_completed(state_test_data);
957
958 /* Make sure the operation statistics are empty */
959 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
960 ASSERT_INT_EQUALS(0, operation_statistics.incomplete_operation_count);
961 ASSERT_INT_EQUALS(0, operation_statistics.incomplete_operation_size);
962 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_count);
963 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_size);
964
965 /* Disconnect */
966 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
967 state_test_data->mqtt_connection, s_operation_statistics_on_disconnect_fn, state_test_data));
968 s_operation_statistics_wait_for_disconnect_to_complete(state_test_data);
969
970 return AWS_OP_SUCCESS;
971 }
972
973 AWS_TEST_CASE_FIXTURE(
974 mqtt_operation_statistics_simple_subscribe,
975 s_operation_statistics_setup_mqtt_server_fn,
976 s_test_mqtt_operation_statistics_simple_subscribe,
977 s_operation_statistics_clean_up_mqtt_server_fn,
978 &test_data)
979
980 /* ========== UNSUBSCRIBE TESTS ========== */
981
982 /**
983 * Make a connection, tell the server NOT to send Acks, publish and immediately check the statistics to make sure
984 * it is incomplete, then wait a little bit and check that it was properly marked as UnAcked, then send the UnsubAck
985 * confirm statistics are zero, and then disconnect
986 */
987 static int s_test_mqtt_operation_statistics_simple_unsubscribe(struct aws_allocator *allocator, void *ctx) {
988 (void)allocator;
989 struct mqtt_connection_state_test *state_test_data = ctx;
990
991 struct aws_mqtt_connection_options connection_options = {
992 .user_data = state_test_data,
993 .clean_session = false,
994 .client_id = aws_byte_cursor_from_c_str("client1234"),
995 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
996 .socket_options = &state_test_data->socket_options,
997 .on_connection_complete = s_operation_statistics_on_connection_complete_fn,
998 };
999
1000 struct aws_byte_cursor unsub_topic = aws_byte_cursor_from_c_str("/test/topic");
1001
1002 /* Connect */
1003 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1004 s_operation_statistics_wait_for_connection_to_complete(state_test_data);
1005
1006 /* We want to wait for 1 operation */
1007 aws_mutex_lock(&state_test_data->lock);
1008 state_test_data->expected_ops_completed = 1;
1009 aws_mutex_unlock(&state_test_data->lock);
1010
1011 /* Stop ACKS so we make sure the operation statistics has time to allow us to identify we sent a packet */
1012 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
1013
1014 /* Send a subscribe packet */
1015 uint16_t packet_id_1 = aws_mqtt_client_connection_unsubscribe(
1016 state_test_data->mqtt_connection, &unsub_topic, s_on_op_complete, state_test_data);
1017 ASSERT_TRUE(packet_id_1 > 0);
1018
1019 /* Wait a little bit to allow the code to put the packet into the socket from the queue, allowing it
1020 * to be unacked. If we check right away, we may or may not see it in the un-acked statistics */
1021 aws_thread_current_sleep((uint64_t)ONE_SEC);
1022
1023 /* Make sure the sizes are correct and there is only one operation waiting
1024 * (The size of the topic, 2 for the header, 2 for the packet ID) */
1025 uint64_t expected_packet_size = unsub_topic.len + 4;
1026 struct aws_mqtt_connection_operation_statistics operation_statistics;
1027 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
1028 ASSERT_INT_EQUALS(1, operation_statistics.incomplete_operation_count);
1029 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.incomplete_operation_size);
1030 ASSERT_INT_EQUALS(1, operation_statistics.unacked_operation_count);
1031 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.unacked_operation_size);
1032
1033 /* Send the UnsubAck and wait for the client to get the ACK */
1034 mqtt_mock_server_send_unsuback(state_test_data->mock_server, packet_id_1);
1035 s_wait_for_ops_completed(state_test_data);
1036
1037 /* Make sure the operation statistics are empty */
1038 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
1039 ASSERT_INT_EQUALS(0, operation_statistics.incomplete_operation_count);
1040 ASSERT_INT_EQUALS(0, operation_statistics.incomplete_operation_size);
1041 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_count);
1042 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_size);
1043
1044 /* Disconnect */
1045 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1046 state_test_data->mqtt_connection, s_operation_statistics_on_disconnect_fn, state_test_data));
1047 s_operation_statistics_wait_for_disconnect_to_complete(state_test_data);
1048
1049 return AWS_OP_SUCCESS;
1050 }
1051
1052 AWS_TEST_CASE_FIXTURE(
1053 mqtt_operation_statistics_simple_unsubscribe,
1054 s_operation_statistics_setup_mqtt_server_fn,
1055 s_test_mqtt_operation_statistics_simple_unsubscribe,
1056 s_operation_statistics_clean_up_mqtt_server_fn,
1057 &test_data)
1058
1059 /* ========== RESUBSCRIBE TESTS ========== */
1060
1061 /**
1062 * Subscribe to multiple topics prior to connection, make a CONNECT, unsubscribe to a topic, disconnect with the broker,
1063 * make a connection with clean_session set to true, then call resubscribe (without the server being able to send ACKs)
1064 * and confirm the operation statistics size is correct, then resubscribe and finally disconnect.
1065 */
1066 static int s_test_mqtt_operation_statistics_simple_resubscribe(struct aws_allocator *allocator, void *ctx) {
1067 (void)allocator;
1068 struct mqtt_connection_state_test *state_test_data = ctx;
1069
1070 struct aws_mqtt_connection_options connection_options = {
1071 .user_data = state_test_data,
1072 .clean_session = false,
1073 .client_id = aws_byte_cursor_from_c_str("client1234"),
1074 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
1075 .socket_options = &state_test_data->socket_options,
1076 .on_connection_complete = s_operation_statistics_on_connection_complete_fn,
1077 };
1078
1079 struct aws_byte_cursor sub_topic_1 = aws_byte_cursor_from_c_str("/test/topic1");
1080 struct aws_byte_cursor sub_topic_2 = aws_byte_cursor_from_c_str("/test/topic2");
1081 struct aws_byte_cursor sub_topic_3 = aws_byte_cursor_from_c_str("/test/topic3");
1082
1083 /* We want to wait for 3 operations */
1084 aws_mutex_lock(&state_test_data->lock);
1085 state_test_data->expected_ops_completed = 3;
1086 aws_mutex_unlock(&state_test_data->lock);
1087
1088 /* Subscribe to the three topics */
1089 uint16_t sub_packet_id_1 = aws_mqtt_client_connection_subscribe(
1090 state_test_data->mqtt_connection,
1091 &sub_topic_1,
1092 AWS_MQTT_QOS_AT_LEAST_ONCE,
1093 s_operation_statistics_on_publish_received,
1094 state_test_data,
1095 NULL,
1096 s_operation_statistics_on_suback,
1097 state_test_data);
1098 ASSERT_TRUE(sub_packet_id_1 > 0);
1099 uint16_t sub_packet_id_2 = aws_mqtt_client_connection_subscribe(
1100 state_test_data->mqtt_connection,
1101 &sub_topic_2,
1102 AWS_MQTT_QOS_AT_LEAST_ONCE,
1103 s_operation_statistics_on_publish_received,
1104 state_test_data,
1105 NULL,
1106 s_operation_statistics_on_suback,
1107 state_test_data);
1108 ASSERT_TRUE(sub_packet_id_2 > 0);
1109 uint16_t sub_packet_id_3 = aws_mqtt_client_connection_subscribe(
1110 state_test_data->mqtt_connection,
1111 &sub_topic_3,
1112 AWS_MQTT_QOS_AT_LEAST_ONCE,
1113 s_operation_statistics_on_publish_received,
1114 state_test_data,
1115 NULL,
1116 s_operation_statistics_on_suback,
1117 state_test_data);
1118 ASSERT_TRUE(sub_packet_id_3 > 0);
1119
1120 /* Wait a little bit to allow the code to put the packet into the socket from the queue, allowing it
1121 * to be unacked. If we check right away, we may or may not see it in the un-acked statistics */
1122 aws_thread_current_sleep((uint64_t)ONE_SEC);
1123
1124 /* Confirm the 3 subscribes are both pending and unacked, and confirm their byte size
1125 * The size = each subscribe: 4 (fixed header + packet ID) + topic filter + 3 (QoS, MSB and LSB length) */
1126 uint64_t expected_packet_size = 12; // fixed packet headers and IDs
1127 expected_packet_size += sub_topic_1.len + 3;
1128 expected_packet_size += sub_topic_2.len + 3;
1129 expected_packet_size += sub_topic_3.len + 3;
1130 /* Check the size (Note: UnAcked will be ZERO because we are not connected) */
1131 struct aws_mqtt_connection_operation_statistics operation_statistics;
1132 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
1133 ASSERT_INT_EQUALS(3, operation_statistics.incomplete_operation_count);
1134 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.incomplete_operation_size);
1135 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_count);
1136 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_size);
1137
1138 /* Connect */
1139 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1140 s_operation_statistics_wait_for_connection_to_complete(state_test_data);
1141
1142 /* Wait for the subscribes to complete */
1143 s_wait_for_ops_completed(state_test_data);
1144
1145 /* We want to wait for 1 operation */
1146 aws_mutex_lock(&state_test_data->lock);
1147 state_test_data->expected_ops_completed = 1;
1148 aws_mutex_unlock(&state_test_data->lock);
1149 /* unsubscribe to the first topic */
1150 uint16_t unsub_packet_id = aws_mqtt_client_connection_unsubscribe(
1151 state_test_data->mqtt_connection, &sub_topic_1, s_on_op_complete, state_test_data);
1152 ASSERT_TRUE(unsub_packet_id > 0);
1153 s_wait_for_ops_completed(state_test_data);
1154
1155 /* Disconnect */
1156 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1157 state_test_data->mqtt_connection, s_operation_statistics_on_disconnect_fn, state_test_data));
1158 s_operation_statistics_wait_for_disconnect_to_complete(state_test_data);
1159 /* Note: The client is still subscribed to both topic_2 and topic_3 */
1160
1161 /* Reconnect to the same server */
1162 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1163 s_operation_statistics_wait_for_connection_to_complete(state_test_data);
1164
1165 /* Get all the packets out of the way */
1166 ASSERT_SUCCESS(mqtt_mock_server_decode_packets(state_test_data->mock_server));
1167
1168 // Stop ACKs - we want to determine the size
1169 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
1170
1171 /* Resubscribes to topic_2 & topic_3 (Note: we do not need a callback for the purpose of this test) */
1172 uint16_t resub_packet_id =
1173 aws_mqtt_resubscribe_existing_topics(state_test_data->mqtt_connection, NULL, state_test_data);
1174 ASSERT_TRUE(resub_packet_id > 0);
1175
1176 /* Wait a little bit to allow the code to put the packet into the socket from the queue, allowing it
1177 * to be unacked. If we check right away, we may or may not see it in the un-acked statistics */
1178 aws_thread_current_sleep((uint64_t)ONE_SEC);
1179
1180 // Make sure the resubscribe packet size is correct and there is only one resubscribe waiting
1181 // The size = 4 (fixed header + packet ID) + [for each topic](topic filter size + 3 (QoS, MSB and LSB length))
1182 expected_packet_size = 4;
1183 expected_packet_size += sub_topic_2.len + 3;
1184 expected_packet_size += sub_topic_3.len + 3;
1185 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
1186 ASSERT_INT_EQUALS(1, operation_statistics.incomplete_operation_count);
1187 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.incomplete_operation_size);
1188 ASSERT_INT_EQUALS(1, operation_statistics.unacked_operation_count);
1189 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.unacked_operation_size);
1190
1191 /* Send the resubscribe */
1192 mqtt_mock_server_send_single_suback(state_test_data->mock_server, resub_packet_id, AWS_MQTT_QOS_AT_LEAST_ONCE);
1193 s_operation_statistics_wait_for_subscribe_to_complete(state_test_data);
1194
1195 /* Enable ACKs again, and then disconnect */
1196 mqtt_mock_server_enable_auto_ack(state_test_data->mock_server);
1197 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1198 state_test_data->mqtt_connection, s_operation_statistics_on_disconnect_fn, state_test_data));
1199 s_operation_statistics_wait_for_disconnect_to_complete(state_test_data);
1200
1201 return AWS_OP_SUCCESS;
1202 }
1203
1204 AWS_TEST_CASE_FIXTURE(
1205 mqtt_operation_statistics_simple_resubscribe,
1206 s_operation_statistics_setup_mqtt_server_fn,
1207 s_test_mqtt_operation_statistics_simple_resubscribe,
1208 s_operation_statistics_clean_up_mqtt_server_fn,
1209 &test_data)
1210
1211 /* ========== OTHER TESTS ========== */
1212
1213 static void s_test_operation_statistics_simple_callback(
1214 struct aws_mqtt_client_connection_311_impl *connection,
1215 void *userdata) {
1216 struct aws_atomic_var *statistics_count = (struct aws_atomic_var *)userdata;
1217 aws_atomic_fetch_add(statistics_count, 1);
1218
1219 // Confirm we can get the operation statistics from the callback
1220 struct aws_mqtt_connection_operation_statistics operation_statistics;
1221 aws_mqtt_client_connection_get_stats(&connection->base, &operation_statistics);
1222 (void)operation_statistics;
1223 }
1224
1225 /**
1226 * Tests the operation statistics callback to make sure it is being called as expected. This is a very simple
1227 * test that just ensures the callback is being called multiple times AND that you can access the operation
1228 * statistics from within the callback.
1229 */
1230 static int s_test_mqtt_operation_statistics_simple_callback(struct aws_allocator *allocator, void *ctx) {
1231 (void)allocator;
1232 struct mqtt_connection_state_test *state_test_data = ctx;
1233
1234 struct aws_mqtt_connection_options connection_options = {
1235 .user_data = state_test_data,
1236 .clean_session = true,
1237 .client_id = aws_byte_cursor_from_c_str("client1234"),
1238 .host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address),
1239 .socket_options = &state_test_data->socket_options,
1240 .on_connection_complete = s_operation_statistics_on_connection_complete_fn,
1241 };
1242
1243 struct aws_byte_cursor pub_topic = aws_byte_cursor_from_c_str("/test/topic");
1244 struct aws_byte_cursor payload_1 = aws_byte_cursor_from_c_str("Test Message");
1245
1246 /* Connect */
1247 ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
1248 s_operation_statistics_wait_for_connection_to_complete(state_test_data);
1249
1250 /* Set the operation statistics callback */
1251 struct aws_atomic_var statistics_count;
1252 aws_atomic_store_int(&statistics_count, 0);
1253 aws_mqtt_client_connection_set_on_operation_statistics_handler(
1254 state_test_data->mqtt_connection->impl, s_test_operation_statistics_simple_callback, &statistics_count);
1255
1256 // /* Stop ACKS so we make sure the operation statistics has time to allow us to identify we sent a packet */
1257 mqtt_mock_server_disable_auto_ack(state_test_data->mock_server);
1258
1259 // /* We want to wait for 1 operation to complete */
1260 aws_mutex_lock(&state_test_data->lock);
1261 state_test_data->expected_ops_completed = 1;
1262 aws_mutex_unlock(&state_test_data->lock);
1263
1264 /* Publish a packet */
1265 uint16_t packet_id_1 = aws_mqtt_client_connection_publish(
1266 state_test_data->mqtt_connection,
1267 &pub_topic,
1268 AWS_MQTT_QOS_AT_LEAST_ONCE,
1269 false,
1270 &payload_1,
1271 s_on_op_complete,
1272 state_test_data);
1273 ASSERT_TRUE(packet_id_1 > 0);
1274
1275 /* Wait a little bit to allow the code to put the packet into the socket from the queue, allowing it
1276 * to be unacked. If we check right away, we may or may not see it in the un-acked statistics */
1277 aws_thread_current_sleep((uint64_t)ONE_SEC);
1278
1279 /* Make sure the sizes are correct and there is only one operation waiting
1280 * (The size of the topic, the size of the payload, 2 for the header, 2 for the packet ID) */
1281 uint64_t expected_packet_size = pub_topic.len + payload_1.len + 4;
1282 struct aws_mqtt_connection_operation_statistics operation_statistics;
1283 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
1284 ASSERT_INT_EQUALS(1, operation_statistics.incomplete_operation_count);
1285 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.incomplete_operation_size);
1286 ASSERT_INT_EQUALS(1, operation_statistics.unacked_operation_count);
1287 ASSERT_INT_EQUALS(expected_packet_size, operation_statistics.unacked_operation_size);
1288
1289 /* Assert the callback was called twice (first for putting in incomplete, second for putting in unacked) */
1290 ASSERT_INT_EQUALS(2, aws_atomic_load_int(&statistics_count));
1291
1292 /* Send the PubAck and wait for the client to receive it */
1293 mqtt_mock_server_send_puback(state_test_data->mock_server, packet_id_1);
1294 s_wait_for_ops_completed(state_test_data);
1295
1296 // /* Assert the callback was called */
1297 aws_thread_current_sleep((uint64_t)ONE_SEC);
1298 ASSERT_INT_EQUALS(3, aws_atomic_load_int(&statistics_count));
1299
1300 /* Make sure the operation values are back to zero now that the publish went out */
1301 ASSERT_SUCCESS(aws_mqtt_client_connection_get_stats(state_test_data->mqtt_connection, &operation_statistics));
1302 ASSERT_INT_EQUALS(0, operation_statistics.incomplete_operation_count);
1303 ASSERT_INT_EQUALS(0, operation_statistics.incomplete_operation_size);
1304 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_count);
1305 ASSERT_INT_EQUALS(0, operation_statistics.unacked_operation_size);
1306
1307 /* Disconnect */
1308 ASSERT_SUCCESS(aws_mqtt_client_connection_disconnect(
1309 state_test_data->mqtt_connection, s_operation_statistics_on_disconnect_fn, state_test_data));
1310 s_operation_statistics_wait_for_disconnect_to_complete(state_test_data);
1311
1312 return AWS_OP_SUCCESS;
1313 }
1314
1315 AWS_TEST_CASE_FIXTURE(
1316 mqtt_operation_statistics_simple_callback,
1317 s_operation_statistics_setup_mqtt_server_fn,
1318 s_test_mqtt_operation_statistics_simple_callback,
1319 s_operation_statistics_clean_up_mqtt_server_fn,
1320 &test_data)