From 5ef9abfa5d4740d57966d056850c86bf31a6fab0 Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Thu, 1 Feb 2024 23:53:32 +0800 Subject: [PATCH] test: rolling counter unit tests --- tests/ut/CMakeLists.txt | 10 ++++++- tests/ut/rolling_counter_test.cc | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/ut/rolling_counter_test.cc diff --git a/tests/ut/CMakeLists.txt b/tests/ut/CMakeLists.txt index 21b3a743..f4feb6a2 100644 --- a/tests/ut/CMakeLists.txt +++ b/tests/ut/CMakeLists.txt @@ -57,6 +57,13 @@ target_include_directories(async_queue_test PRIVATE ${CMAKE_SOURCE_DIR}/include) target_link_libraries(async_queue_test neuron-base gtest_main gtest) +add_executable(rolling_counter_test rolling_counter_test.cc) +target_include_directories(rolling_counter_test PRIVATE + ${CMAKE_SOURCE_DIR}/src + ${CMAKE_SOURCE_DIR}/include +) +target_link_libraries(rolling_counter_test neuron-base gtest_main gtest) + include(GoogleTest) gtest_discover_tests(json_test) gtest_discover_tests(http_test) @@ -64,4 +71,5 @@ gtest_discover_tests(jwt_test) gtest_discover_tests(base64_test) gtest_discover_tests(tag_sort_test) gtest_discover_tests(modbus_test) -gtest_discover_tests(async_queue_test) \ No newline at end of file +gtest_discover_tests(async_queue_test) +gtest_discover_tests(rolling_counter_test) diff --git a/tests/ut/rolling_counter_test.cc b/tests/ut/rolling_counter_test.cc new file mode 100644 index 00000000..fb72c458 --- /dev/null +++ b/tests/ut/rolling_counter_test.cc @@ -0,0 +1,45 @@ +#include + +#include "utils/log.h" +#include "utils/rolling_counter.h" + +zlog_category_t *neuron = NULL; +TEST(RollingCounterTest, neu_rolling_counter_inc) +{ + uint64_t ts = 0; + neu_rolling_counter_t *counter = neu_rolling_counter_new(4000); + EXPECT_NE(nullptr, counter); + + for (int i = 1; i <= 4; ++i) { + ts += 1000; + EXPECT_EQ(i, neu_rolling_counter_inc(counter, ts, 1)); + } + + // wrap around + for (int i = 1; i <= 4; ++i) { + ts += 1000; + EXPECT_EQ(4, neu_rolling_counter_inc(counter, ts, 1)); + } + + // increment without time stamp update + EXPECT_EQ(4, neu_rolling_counter_value(counter)); + for (int i = 1; i <= 10; ++i) { + EXPECT_EQ(4 + i, neu_rolling_counter_inc(counter, ts, 1)); + } + + EXPECT_EQ(4 + 10, neu_rolling_counter_value(counter)); + + // stale value will be clean + ts += 4000; + EXPECT_EQ(0, neu_rolling_counter_inc(counter, ts, 0)); + + neu_rolling_counter_free(counter); +} + +int main(int argc, char **argv) +{ + zlog_init("./config/dev.conf"); + neuron = zlog_get_category("neuron"); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}