-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix mqtt3 suback returned qos behavior #340
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10de2ea
fix mqtt3 suback return qos
xiazhvera 607ad6f
clang format
xiazhvera 4ecee83
add test for failed sub
xiazhvera 0ef6d3f
add test for multi subscribe
xiazhvera 326eecd
update cr
xiazhvera f609a22
Merge branch 'main' into error_suback
xiazhvera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -292,6 +292,22 @@ static void s_on_connection_termination_fn(void *userdata) { | |
aws_condition_variable_notify_one(&state_test_data->cvar); | ||
} | ||
|
||
/** | ||
* Release/terminate the connection, the `mqtt_connection` would be set to NULL after the function call. | ||
* The function would set the `connection_terminated` flag so the termination step would be skipped in cleanup. | ||
*/ | ||
static void s_terminate_connection(void *arg) { | ||
struct mqtt_connection_state_test *state_test_data = arg; | ||
aws_mqtt_client_connection_release(state_test_data->mqtt_connection); | ||
|
||
s_wait_for_termination_to_complete(state_test_data); | ||
// Set `connection_terminated` flag to skip the termination in `s_clean_up_mqtt_server_fn` | ||
aws_mutex_lock(&state_test_data->lock); | ||
state_test_data->mqtt_connection = NULL; | ||
state_test_data->connection_terminated = true; | ||
aws_mutex_unlock(&state_test_data->lock); | ||
} | ||
|
||
/** sets up a unix domain socket server and socket options. Creates an mqtt connection configured to use | ||
* the domain socket. | ||
*/ | ||
|
@@ -399,14 +415,16 @@ static int s_clean_up_mqtt_server_fn(struct aws_allocator *allocator, int setup_ | |
if (!setup_result) { | ||
struct mqtt_connection_state_test *state_test_data = ctx; | ||
|
||
s_received_publish_packet_list_clean_up(&state_test_data->published_messages); | ||
s_received_publish_packet_list_clean_up(&state_test_data->any_published_messages); | ||
aws_array_list_clean_up(&state_test_data->qos_returned); | ||
aws_mqtt_client_connection_release(state_test_data->mqtt_connection); | ||
|
||
s_wait_for_termination_to_complete(state_test_data); | ||
ASSERT_UINT_EQUALS(1, state_test_data->connection_termination_calls); | ||
|
||
// Clean up the state_test_data after the client is terminated. | ||
s_received_publish_packet_list_clean_up(&state_test_data->published_messages); | ||
s_received_publish_packet_list_clean_up(&state_test_data->any_published_messages); | ||
aws_array_list_clean_up(&state_test_data->qos_returned); | ||
|
||
aws_mqtt_client_release(state_test_data->mqtt_client); | ||
aws_client_bootstrap_release(state_test_data->client_bootstrap); | ||
aws_host_resolver_release(state_test_data->host_resolver); | ||
|
@@ -582,9 +600,8 @@ static void s_on_suback( | |
struct mqtt_connection_state_test *state_test_data = userdata; | ||
|
||
aws_mutex_lock(&state_test_data->lock); | ||
if (!error_code) { | ||
aws_array_list_push_back(&state_test_data->qos_returned, &qos); | ||
} | ||
aws_array_list_push_back(&state_test_data->qos_returned, &qos); | ||
|
||
state_test_data->subscribe_completed = true; | ||
state_test_data->subscribe_complete_error = error_code; | ||
aws_mutex_unlock(&state_test_data->lock); | ||
|
@@ -619,14 +636,15 @@ static void s_on_multi_suback( | |
|
||
aws_mutex_lock(&state_test_data->lock); | ||
state_test_data->subscribe_completed = true; | ||
if (!error_code) { | ||
size_t length = aws_array_list_length(topic_subacks); | ||
for (size_t i = 0; i < length; ++i) { | ||
struct aws_mqtt_topic_subscription *subscription = NULL; | ||
aws_array_list_get_at(topic_subacks, &subscription, i); | ||
aws_array_list_push_back(&state_test_data->qos_returned, &subscription->qos); | ||
} | ||
|
||
size_t length = aws_array_list_length(topic_subacks); | ||
for (size_t i = 0; i < length; ++i) { | ||
struct aws_mqtt_topic_subscription *subscription = NULL; | ||
aws_array_list_get_at(topic_subacks, &subscription, i); | ||
aws_array_list_push_back(&state_test_data->qos_returned, &subscription->qos); | ||
} | ||
|
||
state_test_data->subscribe_complete_error = error_code; | ||
aws_mutex_unlock(&state_test_data->lock); | ||
aws_condition_variable_notify_one(&state_test_data->cvar); | ||
} | ||
|
@@ -1464,6 +1482,68 @@ AWS_TEST_CASE_FIXTURE( | |
s_clean_up_mqtt_server_fn, | ||
&test_data) | ||
|
||
/* Subscribe to a topic and release the client, the subscribe should fail */ | ||
static int s_test_mqtt_connect_subscribe_fail_after_client_shutdown_fn(struct aws_allocator *allocator, void *ctx) { | ||
(void)allocator; | ||
struct mqtt_connection_state_test *state_test_data = ctx; | ||
|
||
struct aws_mqtt_connection_options connection_options = { | ||
.user_data = state_test_data, | ||
.clean_session = false, | ||
.client_id = aws_byte_cursor_from_c_str("client1234"), | ||
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address), | ||
.socket_options = &state_test_data->socket_options, | ||
.on_connection_complete = s_on_connection_complete_fn, | ||
.ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS, | ||
.protocol_operation_timeout_ms = 3000, | ||
.keep_alive_time_secs = 16960, /* basically stop automatically sending PINGREQ */ | ||
}; | ||
|
||
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options)); | ||
s_wait_for_connection_to_complete(state_test_data); | ||
|
||
/* Disable the auto ACK packets sent by the server, which blocks the requests to complete */ | ||
mqtt_mock_server_disable_auto_ack(state_test_data->mock_server); | ||
|
||
struct aws_byte_cursor sub_topic = aws_byte_cursor_from_c_str("/test/topic"); | ||
|
||
uint16_t packet_id = aws_mqtt_client_connection_subscribe( | ||
state_test_data->mqtt_connection, | ||
&sub_topic, | ||
AWS_MQTT_QOS_AT_LEAST_ONCE, | ||
s_on_publish_received, | ||
state_test_data, | ||
NULL, | ||
s_on_suback, | ||
state_test_data); | ||
ASSERT_TRUE(packet_id > 0); | ||
|
||
ASSERT_SUCCESS( | ||
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data)); | ||
s_wait_for_disconnect_to_complete(state_test_data); | ||
|
||
s_terminate_connection(state_test_data); | ||
|
||
/* Check the subscribe has been completed after shutdown */ | ||
s_wait_for_subscribe_to_complete(state_test_data); | ||
|
||
size_t length = aws_array_list_length(&state_test_data->qos_returned); | ||
ASSERT_UINT_EQUALS(1, length); | ||
uint8_t qos = 0; | ||
ASSERT_SUCCESS(aws_array_list_get_at(&state_test_data->qos_returned, &qos, 0)); | ||
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_FAILURE, qos); | ||
ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_MQTT_CONNECTION_DESTROYED); | ||
|
||
return AWS_OP_SUCCESS; | ||
} | ||
|
||
AWS_TEST_CASE_FIXTURE( | ||
mqtt_connect_subscribe_fail_after_client_shutdown, | ||
s_setup_mqtt_server_fn, | ||
s_test_mqtt_connect_subscribe_fail_after_client_shutdown_fn, | ||
s_clean_up_mqtt_server_fn, | ||
&test_data) | ||
|
||
/* Subscribe to multiple topics prior to connection, make a CONNECT, have the server send PUBLISH messages, | ||
* make sure they're received, then send a DISCONNECT. */ | ||
static int s_test_mqtt_subscribe_multi_fn(struct aws_allocator *allocator, void *ctx) { | ||
|
@@ -1614,6 +1694,86 @@ AWS_TEST_CASE_FIXTURE( | |
s_clean_up_mqtt_server_fn, | ||
&test_data) | ||
|
||
/* Subscribe to multiple topics prior to connection, make a CONNECT, have the server send PUBLISH messages, | ||
* make sure they're received, then send a DISCONNECT. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trivial: The test description seems to be incorrect |
||
static int s_test_mqtt_subscribe_multi_fail_after_client_shutdown_fn(struct aws_allocator *allocator, void *ctx) { | ||
(void)allocator; | ||
struct mqtt_connection_state_test *state_test_data = ctx; | ||
|
||
struct aws_mqtt_connection_options connection_options = { | ||
.user_data = state_test_data, | ||
.clean_session = false, | ||
.client_id = aws_byte_cursor_from_c_str("client1234"), | ||
.host_name = aws_byte_cursor_from_c_str(state_test_data->endpoint.address), | ||
.socket_options = &state_test_data->socket_options, | ||
.on_connection_complete = s_on_connection_complete_fn, | ||
}; | ||
|
||
struct aws_byte_cursor sub_topic_1 = aws_byte_cursor_from_c_str("/test/topic1"); | ||
struct aws_byte_cursor sub_topic_2 = aws_byte_cursor_from_c_str("/test/topic2"); | ||
|
||
struct aws_mqtt_topic_subscription sub1 = { | ||
.topic = sub_topic_1, | ||
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, | ||
.on_publish = s_on_publish_received, | ||
.on_cleanup = NULL, | ||
.on_publish_ud = state_test_data, | ||
}; | ||
struct aws_mqtt_topic_subscription sub2 = { | ||
.topic = sub_topic_2, | ||
.qos = AWS_MQTT_QOS_AT_LEAST_ONCE, | ||
.on_publish = s_on_publish_received, | ||
.on_cleanup = NULL, | ||
.on_publish_ud = state_test_data, | ||
}; | ||
|
||
struct aws_array_list topic_filters; | ||
size_t list_len = 2; | ||
AWS_VARIABLE_LENGTH_ARRAY(uint8_t, static_buf, list_len * sizeof(struct aws_mqtt_topic_subscription)); | ||
aws_array_list_init_static(&topic_filters, static_buf, list_len, sizeof(struct aws_mqtt_topic_subscription)); | ||
|
||
aws_array_list_push_back(&topic_filters, &sub1); | ||
aws_array_list_push_back(&topic_filters, &sub2); | ||
|
||
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options)); | ||
s_wait_for_connection_to_complete(state_test_data); | ||
|
||
/* Disable the auto ACK packets sent by the server, which blocks the requests to complete */ | ||
mqtt_mock_server_disable_auto_ack(state_test_data->mock_server); | ||
|
||
uint16_t packet_id = aws_mqtt_client_connection_subscribe_multiple( | ||
state_test_data->mqtt_connection, &topic_filters, s_on_multi_suback, state_test_data); | ||
ASSERT_TRUE(packet_id > 0); | ||
|
||
ASSERT_SUCCESS( | ||
aws_mqtt_client_connection_disconnect(state_test_data->mqtt_connection, s_on_disconnect_fn, state_test_data)); | ||
s_wait_for_disconnect_to_complete(state_test_data); | ||
|
||
s_terminate_connection(state_test_data); | ||
|
||
/* Check the subscribe has been completed after shutdown */ | ||
s_wait_for_subscribe_to_complete(state_test_data); | ||
|
||
/* Check the subscribe returned QoS is expected */ | ||
size_t length = aws_array_list_length(&state_test_data->qos_returned); | ||
ASSERT_UINT_EQUALS(2, length); | ||
uint8_t qos = 0; | ||
ASSERT_SUCCESS(aws_array_list_get_at(&state_test_data->qos_returned, &qos, 0)); | ||
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_FAILURE, qos); | ||
ASSERT_SUCCESS(aws_array_list_get_at(&state_test_data->qos_returned, &qos, 1)); | ||
ASSERT_UINT_EQUALS(AWS_MQTT_QOS_FAILURE, qos); | ||
ASSERT_UINT_EQUALS(state_test_data->subscribe_complete_error, AWS_ERROR_MQTT_CONNECTION_DESTROYED); | ||
|
||
return AWS_OP_SUCCESS; | ||
} | ||
|
||
AWS_TEST_CASE_FIXTURE( | ||
mqtt_connect_subscribe_multi_fail_after_client_shutdown, | ||
s_setup_mqtt_server_fn, | ||
s_test_mqtt_subscribe_multi_fail_after_client_shutdown_fn, | ||
s_clean_up_mqtt_server_fn, | ||
&test_data) | ||
|
||
/* Subscribe to multiple topics prior to connection, make a CONNECT, have the server send PUBLISH messages, unsubscribe | ||
* to a topic, have the server send PUBLISH messages again, make sure the unsubscribed topic callback will not be fired | ||
*/ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trivial: Here and below in
s_subscribe_single_complete
we could probably change the topic->request-qos directly instead of creating a separatereturned_qos
. I don't believe the topic->request is used after the point of invoking theon_suback
callbacks in either case.