Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Bret Ambrose committed Mar 26, 2024
1 parent 49bfe58 commit c8bb2d7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ add_test_case(mqtt5_client_sub_pub_unsub_qos1)
add_test_case(mqtt5_client_ping_sequence)
add_test_case(mqtt5_client_ping_timeout)
add_test_case(mqtt5_client_ping_timeout_with_keep_alive_conflict)
add_test_case(mqtt5_client_disabled_keep_alive)
add_test_case(mqtt5_client_reconnect_failure_backoff)
add_test_case(mqtt5_client_reconnect_backoff_insufficient_reset)
add_test_case(mqtt5_client_reconnect_backoff_sufficient_reset)
Expand Down
62 changes: 58 additions & 4 deletions tests/v5/mqtt5_client_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -986,10 +986,7 @@ struct aws_mqtt5_client_test_wait_for_n_context {
struct aws_mqtt5_client_mock_test_fixture *test_fixture;
};

static bool s_received_at_least_n_pingreqs(void *arg) {
struct aws_mqtt5_client_test_wait_for_n_context *ping_context = arg;
struct aws_mqtt5_client_mock_test_fixture *test_fixture = ping_context->test_fixture;

static size_t s_count_pingreqs(struct aws_mqtt5_client_mock_test_fixture *test_fixture) {
size_t ping_count = 0;
size_t packet_count = aws_array_list_length(&test_fixture->server_received_packets);
for (size_t i = 0; i < packet_count; ++i) {
Expand All @@ -1001,6 +998,15 @@ static bool s_received_at_least_n_pingreqs(void *arg) {
}
}

return ping_count;
}

static bool s_received_at_least_n_pingreqs(void *arg) {
struct aws_mqtt5_client_test_wait_for_n_context *ping_context = arg;
struct aws_mqtt5_client_mock_test_fixture *test_fixture = ping_context->test_fixture;

size_t ping_count = s_count_pingreqs(test_fixture);

return ping_count >= ping_context->required_event_count;
}

Expand Down Expand Up @@ -1413,6 +1419,54 @@ AWS_TEST_CASE(
mqtt5_client_ping_timeout_with_keep_alive_conflict,
s_mqtt5_client_ping_timeout_with_keep_alive_conflict_fn)

/*
* Set up a zero keep alive and verify no pings get sent over an interval of time.
*/
static int s_mqtt5_client_disabled_keep_alive_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;

aws_mqtt_library_init(allocator);

struct mqtt5_client_test_options test_options;
aws_mqtt5_client_test_init_default_options(&test_options);

/* no keep alive at all */
test_options.connect_options.keep_alive_interval_seconds = 0;

struct aws_mqtt5_client_mqtt5_mock_test_fixture_options test_fixture_options = {
.client_options = &test_options.client_options,
.server_function_table = &test_options.server_function_table,
};

struct aws_mqtt5_client_mock_test_fixture test_context;
ASSERT_SUCCESS(aws_mqtt5_client_mock_test_fixture_init(&test_context, allocator, &test_fixture_options));

struct aws_mqtt5_client *client = test_context.client;
ASSERT_SUCCESS(aws_mqtt5_client_start(client));

aws_wait_for_connected_lifecycle_event(&test_context);

// zzz
aws_thread_current_sleep(aws_timestamp_convert(5, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));

ASSERT_SUCCESS(aws_mqtt5_client_stop(client, NULL, NULL));

aws_wait_for_stopped_lifecycle_event(&test_context);

// verify the mock server did not get any PINGREQs
aws_mutex_lock(&test_context.lock);
size_t pingreq_count = s_count_pingreqs(&test_context);
aws_mutex_unlock(&test_context.lock);
ASSERT_INT_EQUALS(0, pingreq_count);

aws_mqtt5_client_mock_test_fixture_clean_up(&test_context);
aws_mqtt_library_clean_up();

return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt5_client_disabled_keep_alive, s_mqtt5_client_disabled_keep_alive_fn)

struct aws_lifecycle_event_wait_context {
enum aws_mqtt5_client_lifecycle_event_type type;
size_t count;
Expand Down

0 comments on commit c8bb2d7

Please sign in to comment.