mirror of
https://github.com/emqx/neuron.git
synced 2026-10-17 04:40:42 +08:00
test: rolling counter unit tests
This commit is contained in:
@@ -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)
|
||||
gtest_discover_tests(async_queue_test)
|
||||
gtest_discover_tests(rolling_counter_test)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
Reference in New Issue
Block a user