diff --git a/plugins/in_dummy/in_dummy.c b/plugins/in_dummy/in_dummy.c index 75d1b73333f..3894a410773 100644 --- a/plugins/in_dummy/in_dummy.c +++ b/plugins/in_dummy/in_dummy.c @@ -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 */ @@ -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), diff --git a/plugins/in_dummy/in_dummy.h b/plugins/in_dummy/in_dummy.h index d351420cb27..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,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; diff --git a/tests/runtime/in_simple_systems.c b/tests/runtime/in_simple_systems.c index 7020c4f483e..9f1a9d2e49f 100644 --- a/tests/runtime/in_simple_systems.c +++ b/tests/runtime/in_simple_systems.c @@ -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", @@ -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); @@ -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()