From 125e941522df4d4f501e3d770a1670e1f149f4d6 Mon Sep 17 00:00:00 2001 From: Francisco Valente <1435136+franciscovalentecastro@users.noreply.github.com> Date: Thu, 19 Oct 2023 15:42:42 +0000 Subject: [PATCH] in_dummy: add unit tests for rate and time_interval. Signed-off-by: Francisco Valente <1435136+franciscovalentecastro@users.noreply.github.com> --- tests/runtime/in_simple_systems.c | 105 ++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 6 deletions(-) diff --git a/tests/runtime/in_simple_systems.c b/tests/runtime/in_simple_systems.c index 7020c4f483e..2e537654e51 100644 --- a/tests/runtime/in_simple_systems.c +++ b/tests/runtime/in_simple_systems.c @@ -294,6 +294,72 @@ void do_test_records_single(char *system, void (*records_cb)(struct callback_rec flb_destroy(ctx); } +void do_test_records_wait_time(char *system, int wait_time, void (*records_cb)(struct callback_records *), ...) +{ + flb_ctx_t *ctx = NULL; + int in_ffd; + int out_ffd; + int exit_ffd; + va_list va; + char *key; + char *value; + int i; + struct flb_lib_out_cb cb; + struct callback_records *records; + + records = flb_calloc(1, sizeof(struct callback_records)); + records->num_records = 0; + records->records = NULL; + cb.cb = callback_add_record; + cb.data = (void *)records; + + /* initialize */ + set_result(0); + + ctx = flb_create(); + + in_ffd = flb_input(ctx, (char *) system, NULL); + TEST_CHECK(in_ffd >= 0); + TEST_CHECK(flb_input_set(ctx, in_ffd, "tag", "test", NULL) == 0); + + va_start(va, records_cb); + while ((key = va_arg(va, char *))) { + value = va_arg(va, char *); + TEST_CHECK(value != NULL); + TEST_CHECK(flb_input_set(ctx, in_ffd, key, value, NULL) == 0); + } + va_end(va); + + out_ffd = flb_output(ctx, (char *) "lib", &cb); + TEST_CHECK(out_ffd >= 0); + TEST_CHECK(flb_output_set(ctx, out_ffd, "match", "test", NULL) == 0); + + exit_ffd = flb_output(ctx, (char *)"exit", &cb); + TEST_CHECK(flb_output_set(ctx, exit_ffd, "match", "test", NULL) == 0); + + TEST_CHECK(flb_service_set(ctx, "Flush", "1", + "Grace", "1", + NULL) == 0); + + /* Start test */ + TEST_CHECK(flb_start(ctx) == 0); + + /* Set wait_time plus 4 sec passed. It must have flushed */ + sleep(wait_time + 4); + + records_cb(records); + + flb_stop(ctx); + + for (i = 0; i < records->num_records; i++) { + flb_lib_free(records->records[i].data); + } + flb_free(records->records); + flb_free(records); + + flb_destroy(ctx); +} + void flb_test_in_disk_flush() { do_test("disk", @@ -471,6 +537,21 @@ void flb_test_dummy_records_message_copies_100(struct callback_records *records) TEST_CHECK(records->num_records >= 100); } +void flb_test_dummy_records_message_time_interval_3_rate_1(struct callback_records *records) +{ + TEST_CHECK(records->num_records >= 1); +} + +void flb_test_dummy_records_message_time_interval_3_rate_10(struct callback_records *records) +{ + TEST_CHECK(records->num_records >= 10); +} + +void flb_test_dummy_records_message_time_interval_6_rate_100(struct callback_records *records) +{ + TEST_CHECK(records->num_records >= 100); +} + void flb_test_in_dummy_flush() { do_test("dummy", NULL); @@ -493,14 +574,26 @@ void flb_test_in_dummy_flush() "fixed_timestamp", "on", NULL); do_test_records_single("dummy", flb_test_dummy_records_message_copies_1, - "copies", "1", - NULL); + "copies", "1", + NULL); do_test_records_single("dummy", flb_test_dummy_records_message_copies_5, - "copies", "5", - NULL); + "copies", "5", + NULL); do_test_records_single("dummy", flb_test_dummy_records_message_copies_100, - "copies", "100", - NULL); + "copies", "100", + NULL); + do_test_records_wait_time("dummy", 3, flb_test_dummy_records_message_time_interval_3_rate_1, + "time_interval", "3", + "rate", "1", + NULL); + do_test_records_wait_time("dummy", 3, flb_test_dummy_records_message_time_interval_3_rate_10, + "time_interval", "3", + "rate", "10", + NULL); + do_test_records_wait_time("dummy", 6, flb_test_dummy_records_message_time_interval_6_rate_100, + "time_interval", "6", + "rate", "100", + NULL); } void flb_test_in_dummy_thread_flush()