|
| 1 | +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | + |
| 3 | +/* Fluent Bit |
| 4 | + * ========== |
| 5 | + * Copyright (C) 2015-2016 Treasure Data Inc. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <gtest/gtest.h> |
| 21 | +#include <fluent-bit.h> |
| 22 | +#include <pthread.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <unistd.h> |
| 25 | + |
| 26 | +pthread_mutex_t result_mutex; |
| 27 | +int result; |
| 28 | + |
| 29 | +int callback_test(void* data, size_t size) |
| 30 | +{ |
| 31 | + if (size > 0) { |
| 32 | + free(data); |
| 33 | + pthread_mutex_lock(&result_mutex); |
| 34 | + result = 1;/* success */ |
| 35 | + pthread_mutex_unlock(&result_mutex); |
| 36 | + } |
| 37 | + return 0; |
| 38 | +} |
| 39 | + |
| 40 | +TEST(Inputs, flush_5s) |
| 41 | +{ |
| 42 | + int ret = 0; |
| 43 | + flb_ctx_t *ctx = NULL; |
| 44 | + flb_input_t *input = NULL; |
| 45 | + flb_output_t *output = NULL; |
| 46 | + |
| 47 | + /* initialize */ |
| 48 | + ret = pthread_mutex_init(&result_mutex, NULL); |
| 49 | + result = 0; |
| 50 | + EXPECT_EQ(ret, 0); |
| 51 | + |
| 52 | + ctx = flb_create(); |
| 53 | + |
| 54 | + input = flb_input(ctx, (char *) "cpu", NULL); |
| 55 | + EXPECT_TRUE(input != NULL); |
| 56 | + flb_input_set(input, "tag", "test", NULL); |
| 57 | + |
| 58 | + output = flb_output(ctx, (char *) "lib", (void*)callback_test); |
| 59 | + EXPECT_TRUE(output != NULL); |
| 60 | + flb_output_set(output, "match", "test", NULL); |
| 61 | + |
| 62 | + ret = flb_start(ctx); |
| 63 | + EXPECT_EQ(ret, 0); |
| 64 | + |
| 65 | + /* start test */ |
| 66 | + sleep(2); |
| 67 | + pthread_mutex_lock(&result_mutex); |
| 68 | + ret = result; /* 2sec passed, no data should be flushed */ |
| 69 | + pthread_mutex_unlock(&result_mutex); |
| 70 | + EXPECT_EQ(ret, 0); |
| 71 | + |
| 72 | + sleep(3); |
| 73 | + pthread_mutex_lock(&result_mutex); |
| 74 | + ret = result; /* 5sec passed, data should be flushed */ |
| 75 | + pthread_mutex_unlock(&result_mutex); |
| 76 | + EXPECT_EQ(ret, 1); |
| 77 | + |
| 78 | + |
| 79 | + /* finalize */ |
| 80 | + flb_stop(ctx); |
| 81 | + flb_destroy(ctx); |
| 82 | + |
| 83 | + ret = pthread_mutex_destroy(&result_mutex); |
| 84 | + EXPECT_EQ(ret, 0); |
| 85 | +} |
0 commit comments