Blame tests/0025-timers.c

Packit 2997f0
/*
Packit 2997f0
 * librdkafka - Apache Kafka C library
Packit 2997f0
 *
Packit 2997f0
 * Copyright (c) 2012-2015, Magnus Edenhill
Packit 2997f0
 * All rights reserved.
Packit 2997f0
 *
Packit 2997f0
 * Redistribution and use in source and binary forms, with or without
Packit 2997f0
 * modification, are permitted provided that the following conditions are met:
Packit 2997f0
 *
Packit 2997f0
 * 1. Redistributions of source code must retain the above copyright notice,
Packit 2997f0
 *    this list of conditions and the following disclaimer.
Packit 2997f0
 * 2. Redistributions in binary form must reproduce the above copyright notice,
Packit 2997f0
 *    this list of conditions and the following disclaimer in the documentation
Packit 2997f0
 *    and/or other materials provided with the distribution.
Packit 2997f0
 *
Packit 2997f0
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 2997f0
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 2997f0
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 2997f0
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit 2997f0
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit 2997f0
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit 2997f0
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 2997f0
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit 2997f0
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit 2997f0
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit 2997f0
 * POSSIBILITY OF SUCH DAMAGE.
Packit 2997f0
 */
Packit 2997f0
Packit 2997f0
#include "test.h"
Packit 2997f0
#include "rdkafka.h"
Packit 2997f0
Packit 2997f0
Packit 2997f0
/**
Packit 2997f0
 * Tests that rdkafka's internal timers behave.
Packit 2997f0
 */
Packit 2997f0
Packit 2997f0
Packit 2997f0
Packit 2997f0
struct state {
Packit 2997f0
        int calls;
Packit 2997f0
        int64_t ts_last;
Packit 2997f0
        int interval;
Packit 2997f0
        int fails;
Packit 2997f0
};
Packit 2997f0
Packit 2997f0
struct state state;
Packit 2997f0
Packit 2997f0
Packit 2997f0
static int stats_cb (rd_kafka_t *rk, char *json, size_t json_len,
Packit 2997f0
                      void *opaque) {
Packit 2997f0
        const int64_t now = test_clock();
Packit 2997f0
        /* Fake the first elapsed time since we dont really know how
Packit 2997f0
         * long rd_kafka_new() takes and at what time the timer is started. */
Packit 2997f0
        const int64_t elapsed = state.ts_last ?
Packit 2997f0
                now - state.ts_last : state.interval;
Packit 2997f0
        const int64_t overshoot = elapsed - state.interval;
Packit 2997f0
        const int wiggleroom_up = (int)((double)state.interval *
Packit 2997f0
					(!strcmp(test_mode, "bare") ? 0.2 : 1.0));
Packit 2997f0
	const int wiggleroom_down = (int)((double)state.interval * 0.1);
Packit 2997f0
Packit 2997f0
        TEST_SAY("Call #%d: after %"PRId64"ms, %.0f%% outside "
Packit 2997f0
                 "interval %"PRId64"  >-%d <+%d\n",
Packit 2997f0
                 state.calls, elapsed / 1000,
Packit 2997f0
                 ((double)overshoot / state.interval) * 100.0,
Packit 2997f0
                 (int64_t)state.interval / 1000,
Packit 2997f0
		 wiggleroom_down / 1000, wiggleroom_up / 1000);
Packit 2997f0
Packit 2997f0
        if (overshoot < -wiggleroom_down || overshoot > wiggleroom_up) {
Packit 2997f0
                TEST_WARN("^ outside range\n");
Packit 2997f0
                state.fails++;
Packit 2997f0
        }
Packit 2997f0
Packit 2997f0
        state.ts_last = now;
Packit 2997f0
        state.calls++;
Packit 2997f0
Packit 2997f0
        return 0;
Packit 2997f0
}
Packit 2997f0
Packit 2997f0
Packit 2997f0
/**
Packit 2997f0
 * Enable statistics with a set interval, make sure the stats callbacks are
Packit 2997f0
 * called within reasonable intervals.
Packit 2997f0
 */
Packit 2997f0
static void do_test_stats_timer (void) {
Packit 2997f0
        rd_kafka_t *rk;
Packit 2997f0
        rd_kafka_conf_t *conf;
Packit 2997f0
        const int exp_calls = 10;
Packit 2997f0
        test_timing_t t_new;
Packit 2997f0
Packit 2997f0
        memset(&state, 0, sizeof(state));
Packit 2997f0
Packit 2997f0
        state.interval = 600*1000;
Packit 2997f0
Packit 2997f0
        test_conf_init(&conf, NULL, 200);
Packit 2997f0
Packit 2997f0
        test_conf_set(conf, "statistics.interval.ms", "600");
Packit 2997f0
        test_conf_set(conf, "bootstrap.servers", NULL); /*no need for brokers*/
Packit 2997f0
        rd_kafka_conf_set_stats_cb(conf, stats_cb);
Packit 2997f0
Packit 2997f0
        TIMING_START(&t_new, "rd_kafka_new()");
Packit 2997f0
        rk = test_create_handle(RD_KAFKA_CONSUMER, conf);
Packit 2997f0
        TIMING_STOP(&t_new);
Packit 2997f0
Packit 2997f0
        TEST_SAY("Starting wait loop for %d expected stats_cb calls "
Packit 2997f0
                 "with an interval of %dms\n",
Packit 2997f0
                 exp_calls, state.interval/1000);
Packit 2997f0
Packit 2997f0
Packit 2997f0
        while (state.calls < exp_calls) {
Packit 2997f0
                test_timing_t t_poll;
Packit 2997f0
                TIMING_START(&t_poll, "rd_kafka_poll()");
Packit 2997f0
                rd_kafka_poll(rk, 100);
Packit 2997f0
                TIMING_STOP(&t_poll);
Packit 2997f0
Packit 2997f0
                if (TIMING_DURATION(&t_poll) > 150*1000)
Packit 2997f0
                        TEST_WARN("rd_kafka_poll(rk,100) "
Packit 2997f0
                                  "took more than 50%% extra\n");
Packit 2997f0
        }
Packit 2997f0
Packit 2997f0
        rd_kafka_destroy(rk);
Packit 2997f0
Packit 2997f0
        if (state.calls > exp_calls)
Packit 2997f0
                TEST_SAY("Got more calls than expected: %d > %d\n",
Packit 2997f0
                         state.calls, exp_calls);
Packit 2997f0
Packit 2997f0
        if (state.fails) {
Packit 2997f0
                /* We can't rely on CIs giving our test job enough CPU to finish
Packit 2997f0
                 * in time, so don't error out even if the time is outside
Packit 2997f0
                 * the window */
Packit 2997f0
                if (test_on_ci)
Packit 2997f0
                        TEST_WARN("%d/%d intervals failed\n",
Packit 2997f0
                                  state.fails, state.calls);
Packit 2997f0
                else
Packit 2997f0
                        TEST_FAIL("%d/%d intervals failed\n",
Packit 2997f0
                                  state.fails, state.calls);
Packit 2997f0
        } else
Packit 2997f0
                TEST_SAY("All %d intervals okay\n", state.calls);
Packit 2997f0
}
Packit 2997f0
Packit 2997f0
Packit 2997f0
int main_0025_timers (int argc, char **argv) {
Packit 2997f0
        do_test_stats_timer();
Packit 2997f0
        return 0;
Packit 2997f0
}