Skip to content

Commit 06c31d7

Browse files
cosmo0920edsiper
authored andcommitted
filter_lua: tests: Wait proper time with timeout
In macOS kevent system, we sometimes experienced to ought to be waiting for relatively log time. So, we have to wait for the such systems but also we need to terminate early not to be needed to wait systems. Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 7d7e57b commit 06c31d7

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

tests/runtime/filter_lua.c

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,35 @@ static int get_output_num()
8686
return ret;
8787
}
8888

89+
void wait_for_output_num(uint32_t timeout_ms, int *output_num)
90+
{
91+
struct flb_time start_time;
92+
struct flb_time end_time;
93+
struct flb_time diff_time;
94+
uint64_t elapsed_time_flb = 0;
95+
96+
flb_time_get(&start_time);
97+
98+
while (true) {
99+
*output_num = get_output_num();
100+
101+
if (*output_num > 0) {
102+
break;
103+
}
104+
105+
flb_time_msleep(100);
106+
flb_time_get(&end_time);
107+
flb_time_diff(&end_time, &start_time, &diff_time);
108+
elapsed_time_flb = flb_time_to_nanosec(&diff_time) / 1000000;
109+
110+
if (elapsed_time_flb > timeout_ms) {
111+
flb_warn("[timeout] elapsed_time: %ld", elapsed_time_flb);
112+
/* Reached timeout. */
113+
break;
114+
}
115+
}
116+
}
117+
89118
static int cb_count_msgpack_events(void *record, size_t size, void *data)
90119
{
91120
msgpack_unpacked result;
@@ -1125,6 +1154,7 @@ void flb_test_invalid_metatable()
11251154
void flb_test_metadata_single_record()
11261155
{
11271156
int ret;
1157+
int num;
11281158
flb_ctx_t *ctx;
11291159
int in_ffd;
11301160
int out_ffd;
@@ -1169,10 +1199,11 @@ void flb_test_metadata_single_record()
11691199
ret = flb_start(ctx);
11701200
TEST_CHECK(ret == 0);
11711201

1172-
flb_time_msleep(2000);
1173-
1174-
ret = get_output_num();
1175-
TEST_CHECK(ret > 0);
1202+
/* waiting to flush */
1203+
wait_for_output_num(3000, &num);
1204+
if (!TEST_CHECK(num > 0)) {
1205+
TEST_MSG("no outputs");
1206+
}
11761207

11771208
flb_stop(ctx);
11781209
flb_destroy(ctx);
@@ -1181,6 +1212,7 @@ void flb_test_metadata_single_record()
11811212
void flb_test_metadata_array(void)
11821213
{
11831214
int ret;
1215+
int num;
11841216
flb_ctx_t *ctx;
11851217
int in_ffd;
11861218
int out_ffd;
@@ -1222,10 +1254,11 @@ void flb_test_metadata_array(void)
12221254
ret = flb_start(ctx);
12231255
TEST_CHECK(ret == 0);
12241256

1225-
flb_time_msleep(2000);
1226-
1227-
ret = get_output_num();
1228-
TEST_CHECK(ret == 2);
1257+
/* waiting to flush */
1258+
wait_for_output_num(3000, &num);
1259+
if (!TEST_CHECK(num == 2)) {
1260+
TEST_MSG("no intended outputs. Expected 2 actual %d", num);
1261+
}
12291262

12301263
flb_stop(ctx);
12311264
flb_destroy(ctx);

0 commit comments

Comments
 (0)