diff --git a/server/core/test/CMakeLists.txt b/server/core/test/CMakeLists.txt index 049b9d8c8..c647a6f0b 100644 --- a/server/core/test/CMakeLists.txt +++ b/server/core/test/CMakeLists.txt @@ -7,6 +7,7 @@ add_executable(test_dcb test_dcb.cc) add_executable(test_event test_event.cc) add_executable(test_filter test_filter.cc) add_executable(test_hint test_hint.cc) +add_executable(test_housekeeper test_housekeeper.cc) add_executable(test_http test_http.cc) add_executable(test_json test_json.cc) add_executable(test_local_address test_local_address.cc) @@ -34,6 +35,7 @@ target_link_libraries(test_dcb maxscale-common) target_link_libraries(test_event maxscale-common) target_link_libraries(test_filter maxscale-common) target_link_libraries(test_hint maxscale-common) +target_link_libraries(test_housekeeper maxscale-common) target_link_libraries(test_http maxscale-common) target_link_libraries(test_json maxscale-common) target_link_libraries(test_local_address maxscale-common) @@ -60,6 +62,7 @@ add_test(test_dcb test_dcb) add_test(test_event test_event) add_test(test_filter test_filter) add_test(test_hint test_hint) +add_test(test_housekeeper test_housekeeper) add_test(test_http test_http) add_test(test_json test_json) add_test(test_log test_log) diff --git a/server/core/test/test_housekeeper.cc b/server/core/test/test_housekeeper.cc new file mode 100644 index 000000000..76b5c5275 --- /dev/null +++ b/server/core/test/test_housekeeper.cc @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2022-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include +#include +#include +#include +#include "test_utils.hh" + +using namespace std; + +namespace +{ + +std::atomic n_oneshot; +std::atomic n_repeating; + +const char* ZONESHOT_NAME = "OneShot"; +const char* ZREPEATING_NAME = "Repeating"; + +bool oneshot(void*) +{ + ++n_oneshot; + return false; // Remove from housekeeper. +} + +bool repeating(void*) +{ + ++n_repeating; + return true; // Continue calling. +} + +int test() +{ + int rc = EXIT_SUCCESS; + + hktask_add(ZONESHOT_NAME, oneshot, nullptr, 1); // Call oneshot, once per second. + hktask_add(ZREPEATING_NAME, repeating, nullptr, 1); // Call repeating, once per second. + + sleep(4); // Should get 1 oneshot call and ~4 repeating calls. + + hktask_remove(ZREPEATING_NAME); + + int n; + + n = n_oneshot.load(); + cout << "Oneshots: " << n << endl; + + if (n != 1) + { + cerr << "Expected 1 oneshots, got " << n << "." << endl; + rc = EXIT_FAILURE; + } + + n = n_repeating.load(); + cout << "Repeating: " << n << endl; + + // Let's check that the task removal really had an effect. + sleep(2); + + if (n != n_repeating.load()) + { + cerr << "Removed task was called." << endl; + rc = EXIT_FAILURE; + } + + // Timing involved, so we allow for some non-determinism. + if (n < 3 || n > 5) + { + cerr << "Expected ~4 repeating, got " << n << "." << endl; + rc = EXIT_FAILURE; + } + + return rc; +} + +} + +int main(int argc, char** argv) +{ + int rc = EXIT_FAILURE; + + init_test_env(); + + if (hkinit()) + { + if (hkstart()) + { + rc = test(); + } + else + { + cerr << "Could not start the housekeeper." << endl; + } + + hkfinish(); + } + else + { + cerr << "Could not initialize the housekeeper." << endl; + } + + return rc; +}