bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#include "test-lib.h"
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#include "ioloop.h"
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#include "log-throttle.h"
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenstatic unsigned int test_log_throttle_new_events_count;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenstatic void test_log_throttle_callback(unsigned int new_events_count,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen struct ioloop *ioloop)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen{
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_log_throttle_new_events_count = new_events_count;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen io_loop_stop(ioloop);
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen}
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenvoid test_log_throttle(void)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen{
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen const struct log_throttle_settings set = {
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen .throttle_at_max_per_interval = 10,
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen .unthrottle_at_max_per_interval = 5,
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen .interval_msecs = 10,
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen };
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen struct log_throttle *throttle;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen struct ioloop *ioloop;
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen unsigned int i;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_begin("log throttle");
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen ioloop = io_loop_create();
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen throttle = log_throttle_init(&set, test_log_throttle_callback, ioloop);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* throttle once and drop out just below */
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen for (i = 0; i < 10; i++)
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen test_assert_idx(log_throttle_accept(throttle), i);
25ec868bd8b5375e1c1c4c3331d761667ddfe26cTimo Sirainen for (i = 0; i < 4; i++)
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen test_assert_idx(!log_throttle_accept(throttle), i);
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen io_loop_run(ioloop);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(test_log_throttle_new_events_count == 4);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* throttle and continue just above */
7b032348d7bbb93ff96188289d3dfc1899b9abb3Josef 'Jeff' Sipek for (i = 0; i < 10; i++)
6307d76096764e66bddc63d4a3e5a1aa19cc528fJosef 'Jeff' Sipek test_assert_idx(log_throttle_accept(throttle), i);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen for (i = 0; i < 5; i++)
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen test_assert_idx(!log_throttle_accept(throttle), i);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen io_loop_run(ioloop);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(test_log_throttle_new_events_count == 5);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* we should be still throttled */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_assert(!log_throttle_accept(throttle));
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen log_throttle_deinit(&throttle);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen io_loop_destroy(&ioloop);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen test_end();
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen}
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen