tests: runtime: filter_kubernetes: Wait for enough time with timeout
#define _GNU_SOURCE /* for accept4 */
#include <fluent-bit.h>
+#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_info.h>
#include "flb_tests_runtime.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
+#ifdef _WIN32
+ #define TIME_EPSILON_MS 30
+#else
+ #define TIME_EPSILON_MS 10
+#endif
struct kube_test {
flb_ctx_t *flb;
@@ -20,6 +26,34 @@ struct kube_test_result {
int nMatched;
};
+void wait_with_timeout(uint32_t timeout_ms, struct kube_test_result *result, int nExpected)
+{
+ struct flb_time start_time;
+ struct flb_time end_time;
+ struct flb_time diff_time;
+ uint64_t elapsed_time_flb = 0;
+ int64_t ret = 0;
+
+ flb_time_get(&start_time);
+
+ while (true) {
+ if (result->nMatched == nExpected) {
+ break;
+ }
+
+ flb_time_msleep(100);
+ flb_time_get(&end_time);
+ flb_time_diff(&end_time, &start_time, &diff_time);
+ elapsed_time_flb = flb_time_to_nanosec(&diff_time) / 1000000;
+
+ if (elapsed_time_flb > timeout_ms - TIME_EPSILON_MS) {
+ flb_warn("[timeout] elapsed_time: %ld", elapsed_time_flb);
+ // Reached timeout.
+ break;
+ }
+ }
+}
+
/* Test target mode */
#define KUBE_TAIL 0
#define KUBE_SYSTEMD 1
@@ -327,6 +361,10 @@ static void kube_test(const char *target, int type, const char *suffix, int nExp
for (ret = 0; ret < 2000 && result.nMatched == 0; ret++) {
usleep(1000);
}
+
+ /* Wait until matching nExpected results */
+ wait_with_timeout(5000, &result, nExpected);
+
TEST_CHECK(result.nMatched == nExpected);
TEST_MSG("result.nMatched: %i\nnExpected: %i", result.nMatched, nExpected);
