Skip to content
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

in_dummy: Add interval settings. #8060

Merged
merged 5 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions plugins/in_dummy/in_dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,26 @@ static int configure(struct flb_dummy *ctx,
}

/* interval settings */
if (ctx->interval_sec < 0 || ctx->interval_nsec < 0) {
/* Illegal settings. Override them. */
ctx->interval_sec = atoi(DEFAULT_INTERVAL_SEC);
ctx->interval_nsec = atoi(DEFAULT_INTERVAL_NSEC);
}

/* default settings */
tm->tv_sec = 1;
tm->tv_nsec = 0;

if (ctx->rate > 1) {
tm->tv_sec = 0;
tm->tv_nsec = 1000000000 / ctx->rate;
if (ctx->interval_sec > 0 || ctx->interval_nsec > 0) {
/* Set using interval settings. */
tm->tv_sec = ctx->interval_sec;
tm->tv_nsec = ctx->interval_nsec;
} else {
if (ctx->rate > 1) {
/* Set using rate settings. */
tm->tv_sec = 0;
tm->tv_nsec = 1000000000 / ctx->rate;
}
}

/* dummy timestamp */
Expand Down Expand Up @@ -396,10 +410,20 @@ static struct flb_config_map config_map[] = {
"set the sample metadata to be generated. It should be a JSON object."
},
{
FLB_CONFIG_MAP_INT, "rate", "1",
FLB_CONFIG_MAP_INT, "rate", DEFAULT_RATE,
0, FLB_TRUE, offsetof(struct flb_dummy, rate),
"set a number of events per second."
},
{
FLB_CONFIG_MAP_INT, "interval_sec", DEFAULT_INTERVAL_SEC,
0, FLB_TRUE, offsetof(struct flb_dummy, interval_sec),
"set seconds of interval to generate events. overrides rate setting."
},
{
FLB_CONFIG_MAP_INT, "interval_nsec", DEFAULT_INTERVAL_NSEC,
0, FLB_TRUE, offsetof(struct flb_dummy, interval_nsec),
"set nanoseconds of interval to generate events. overrides rate setting."
},
{
FLB_CONFIG_MAP_INT, "copies", "1",
0, FLB_TRUE, offsetof(struct flb_dummy, copies),
Expand Down
5 changes: 5 additions & 0 deletions plugins/in_dummy/in_dummy.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

#define DEFAULT_DUMMY_MESSAGE "{\"message\":\"dummy\"}"
#define DEFAULT_DUMMY_METADATA "{}"
#define DEFAULT_RATE "1"
#define DEFAULT_INTERVAL_SEC "0"
#define DEFAULT_INTERVAL_NSEC "0"

struct flb_dummy {
int coll_fd;
Expand All @@ -34,6 +37,8 @@ struct flb_dummy {
int copies;
int samples;
int samples_count;
int interval_sec;
int interval_nsec;

int dummy_timestamp_set;
struct flb_time base_timestamp;
Expand Down
100 changes: 94 additions & 6 deletions tests/runtime/in_simple_systems.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,68 @@ 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;
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);

TEST_CHECK(flb_service_set(ctx, "Flush", "1",
"Grace", "1",
NULL) == 0);

/* Start test */
TEST_CHECK(flb_start(ctx) == 0);

/* Set wait_time plus 2 sec passed. It must have flushed */
sleep(wait_time + 2);

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",
Expand Down Expand Up @@ -471,6 +533,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_rate(struct callback_records *records)
{
TEST_CHECK(records->num_records >= 20);
}

void flb_test_dummy_records_message_interval_sec(struct callback_records *records)
{
TEST_CHECK(records->num_records >= 1);
}

void flb_test_dummy_records_message_interval_nsec(struct callback_records *records)
{
TEST_CHECK(records->num_records >= 1);
}

void flb_test_in_dummy_flush()
{
do_test("dummy", NULL);
Expand All @@ -493,14 +570,25 @@ 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", 1, flb_test_dummy_records_message_rate,
"rate", "20",
NULL);
do_test_records_wait_time("dummy", 2, flb_test_dummy_records_message_interval_sec,
"interval_sec", "2",
"interval_nsec", "0",
NULL);
do_test_records_wait_time("dummy", 1, flb_test_dummy_records_message_interval_nsec,
"interval_sec", "0",
"interval_nsec", "700000000",
NULL);
}

void flb_test_in_dummy_thread_flush()
Expand Down
Loading