Skip to content

Commit

Permalink
Check on_connection_termination for all mqtt3 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sfodagain committed Aug 18, 2023
1 parent 0fd1e13 commit 9c79955
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 128 deletions.
2 changes: 0 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ add_test_case(mqtt_operation_statistics_simple_callback)

# Connection termination tests
add_test_case(mqtt_connection_termination_callback_simple)
add_test_case(mqtt_connection_termination_callback_connect_disconnect)
add_test_case(mqtt_connection_termination_callback_connect_failed)

# MQTT5 tests

Expand Down
161 changes: 35 additions & 126 deletions tests/v3/connection_state_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,29 @@ static void s_wait_for_connection_to_fail(struct mqtt_connection_state_test *sta
aws_mutex_unlock(&state_test_data->lock);
}

static bool s_is_termination_completed(void *arg) {
struct mqtt_connection_state_test *state_test_data = arg;
return state_test_data->connection_terminated;
}

static void s_wait_for_termination_to_complete(struct mqtt_connection_state_test *state_test_data) {
aws_mutex_lock(&state_test_data->lock);
aws_condition_variable_wait_pred(
&state_test_data->cvar, &state_test_data->lock, s_is_termination_completed, state_test_data);
state_test_data->connection_terminated = false;
aws_mutex_unlock(&state_test_data->lock);
}

static void s_on_connection_termination_fn(void *userdata) {
struct mqtt_connection_state_test *state_test_data = (struct mqtt_connection_state_test *)userdata;

aws_mutex_lock(&state_test_data->lock);
state_test_data->connection_termination_calls += 1;
aws_mutex_unlock(&state_test_data->lock);

state_test_data->connection_terminated = true;
}

/** sets up a unix domain socket server and socket options. Creates an mqtt connection configured to use
* the domain socket.
*/
Expand Down Expand Up @@ -353,6 +376,10 @@ static int s_setup_mqtt_server_fn(struct aws_allocator *allocator, void *ctx) {
ASSERT_SUCCESS(aws_array_list_init_dynamic(
&state_test_data->any_published_messages, allocator, 4, sizeof(struct received_publish_packet)));
ASSERT_SUCCESS(aws_array_list_init_dynamic(&state_test_data->qos_returned, allocator, 2, sizeof(uint8_t)));

ASSERT_SUCCESS(aws_mqtt_client_connection_set_connection_termination_handler(
state_test_data->mqtt_connection, s_on_connection_termination_fn, state_test_data));

return AWS_OP_SUCCESS;
}

Expand All @@ -376,6 +403,10 @@ static int s_clean_up_mqtt_server_fn(struct aws_allocator *allocator, int setup_
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);

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);
Expand Down Expand Up @@ -3686,66 +3717,16 @@ AWS_TEST_CASE_FIXTURE(
s_clean_up_mqtt_server_fn,
&test_data)

static bool s_is_termination_completed(void *arg) {
struct mqtt_connection_state_test *state_test_data = arg;
return state_test_data->connection_terminated;
}

static void s_wait_for_termination_to_complete(struct mqtt_connection_state_test *state_test_data) {
aws_mutex_lock(&state_test_data->lock);
aws_condition_variable_wait_pred(
&state_test_data->cvar, &state_test_data->lock, s_is_termination_completed, state_test_data);
state_test_data->connection_terminated = false;
aws_mutex_unlock(&state_test_data->lock);
}

static void s_on_connection_termination_fn(void *userdata) {
struct mqtt_connection_state_test *state_test_data = (struct mqtt_connection_state_test *)userdata;

aws_mutex_lock(&state_test_data->lock);
state_test_data->connection_termination_calls += 1;
aws_mutex_unlock(&state_test_data->lock);

state_test_data->connection_terminated = true;
}

static int s_clean_up_mqtt_server_with_termination_fn(struct aws_allocator *allocator, int setup_result, void *ctx) {
(void)allocator;

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);

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);
aws_server_bootstrap_destroy_socket_listener(state_test_data->server_bootstrap, state_test_data->listener);
s_wait_on_listener_cleanup(state_test_data);
aws_server_bootstrap_release(state_test_data->server_bootstrap);
aws_event_loop_group_release(state_test_data->el_group);
destroy_mqtt_mock_server(state_test_data->mock_server);
}

aws_mqtt_library_clean_up();
return AWS_OP_SUCCESS;
}

/**
* Test that the connection termination callback is fired for the connection that was not actually connected ever.
* \note Other tests use on_connection_termination callback as well, so one simple dedicated case is enough.
*/
static int s_test_mqtt_connection_termination_callback_simple_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;

aws_mqtt_client_connection_set_connection_termination_handler(
state_test_data->mqtt_connection, s_on_connection_termination_fn, state_test_data);
ASSERT_SUCCESS(aws_mqtt_client_connection_set_connection_termination_handler(
state_test_data->mqtt_connection, s_on_connection_termination_fn, state_test_data));

return AWS_OP_SUCCESS;
}
Expand All @@ -3754,77 +3735,5 @@ AWS_TEST_CASE_FIXTURE(
mqtt_connection_termination_callback_simple,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_termination_callback_simple_fn,
s_clean_up_mqtt_server_with_termination_fn,
&test_data)

/*
* Test that the connection termination callback is fired after connect and disconnect.
*/
static int s_test_mqtt_connection_termination_callback_connect_disconnect_fn(
struct aws_allocator *allocator,
void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;

aws_mqtt_client_connection_set_connection_termination_handler(
state_test_data->mqtt_connection, s_on_connection_termination_fn, state_test_data);

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,
};

ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);
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);

return AWS_OP_SUCCESS;
}

AWS_TEST_CASE_FIXTURE(
mqtt_connection_termination_callback_connect_disconnect,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_termination_callback_connect_disconnect_fn,
s_clean_up_mqtt_server_with_termination_fn,
&test_data)

/*
* Test that the connection termination callback is fired after connection failure.
*/
static int s_test_mqtt_connection_termination_callback_connect_failed_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
struct mqtt_connection_state_test *state_test_data = ctx;

aws_mqtt_client_connection_set_connection_termination_handler(
state_test_data->mqtt_connection, s_on_connection_termination_fn, state_test_data);

struct aws_mqtt_connection_options connection_options = {
.user_data = state_test_data,
.clean_session = true,
.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,
.keep_alive_time_secs = DEFAULT_TEST_KEEP_ALIVE_S,
.ping_timeout_ms = DEFAULT_TEST_PING_TIMEOUT_MS,
};

mqtt_mock_server_set_max_connack(state_test_data->mock_server, 0);
ASSERT_SUCCESS(aws_mqtt_client_connection_connect(state_test_data->mqtt_connection, &connection_options));
s_wait_for_connection_to_complete(state_test_data);

return AWS_OP_SUCCESS;
}

AWS_TEST_CASE_FIXTURE(
mqtt_connection_termination_callback_connect_failed,
s_setup_mqtt_server_fn,
s_test_mqtt_connection_termination_callback_connect_failed_fn,
s_clean_up_mqtt_server_with_termination_fn,
s_clean_up_mqtt_server_fn,
&test_data)

0 comments on commit 9c79955

Please sign in to comment.