diff --git a/plugins/in_dummy/in_dummy.c b/plugins/in_dummy/in_dummy.c index 193cec4165f..3894a410773 100644 --- a/plugins/in_dummy/in_dummy.c +++ b/plugins/in_dummy/in_dummy.c @@ -201,8 +201,6 @@ static int configure(struct flb_dummy *ctx, struct timespec *tm) { const char *msg; - double tm_interval_fractional; - int tm_interval_seconds; int root_type; int ret = -1; @@ -216,14 +214,26 @@ static int configure(struct flb_dummy *ctx, } /* interval settings */ - tm->tv_sec = ctx->time_interval; + 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_interval_fractional = (double) ctx->time_interval / ctx->rate; - tm_interval_seconds = (int) tm_interval_fractional; - tm->tv_sec = tm_interval_seconds; - tm->tv_nsec = (tm_interval_fractional - tm_interval_seconds) * 1000000000; + 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 */ @@ -400,14 +410,19 @@ 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 time interval." + "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, "time_interval", "1", - 0, FLB_TRUE, offsetof(struct flb_dummy, time_interval), - "set time interval to generate logs at the set rate." + 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", diff --git a/plugins/in_dummy/in_dummy.h b/plugins/in_dummy/in_dummy.h index fdf3b2de871..da6c578e743 100644 --- a/plugins/in_dummy/in_dummy.h +++ b/plugins/in_dummy/in_dummy.h @@ -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; @@ -34,7 +37,8 @@ struct flb_dummy { int copies; int samples; int samples_count; - int time_interval; + int interval_sec; + int interval_nsec; int dummy_timestamp_set; struct flb_time base_timestamp; diff --git a/tests/runtime/in_simple_systems.c b/tests/runtime/in_simple_systems.c index 32099a6e15a..9f1a9d2e49f 100644 --- a/tests/runtime/in_simple_systems.c +++ b/tests/runtime/in_simple_systems.c @@ -533,14 +533,19 @@ 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_2_rate_5(struct callback_records *records) +void flb_test_dummy_records_message_rate(struct callback_records *records) { - TEST_CHECK(records->num_records >= 5); + TEST_CHECK(records->num_records >= 20); } -void flb_test_dummy_records_message_time_interval_3_rate_100(struct callback_records *records) +void flb_test_dummy_records_message_interval_sec(struct callback_records *records) { - TEST_CHECK(records->num_records >= 100); + 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() @@ -573,13 +578,16 @@ void flb_test_in_dummy_flush() do_test_records_single("dummy", flb_test_dummy_records_message_copies_100, "copies", "100", NULL); - do_test_records_wait_time("dummy", 2, flb_test_dummy_records_message_time_interval_2_rate_5, - "time_interval", "2", - "rate", "5", + 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", 3, flb_test_dummy_records_message_time_interval_3_rate_100, - "time_interval", "3", - "rate", "100", + do_test_records_wait_time("dummy", 1, flb_test_dummy_records_message_interval_nsec, + "interval_sec", "0", + "interval_nsec", "700000000", NULL); }