From 7490333616cc3b243bdf931efa1a77c8a4733a20 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Tue, 12 Jan 2021 11:40:23 +0800 Subject: [PATCH] [TD-2700]: fix incorrect generated time window. --- src/os/CMakeLists.txt | 2 ++ src/os/src/detail/osTime.c | 5 +++-- src/os/tests/CMakeLists.txt | 15 +++++++++++++++ src/os/tests/test.cpp | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/os/tests/CMakeLists.txt create mode 100644 src/os/tests/test.cpp diff --git a/src/os/CMakeLists.txt b/src/os/CMakeLists.txt index 4e44d29a02..ab8b0f7678 100644 --- a/src/os/CMakeLists.txt +++ b/src/os/CMakeLists.txt @@ -10,3 +10,5 @@ ELSEIF (TD_WINDOWS) ENDIF () ADD_SUBDIRECTORY(src/detail) +ADD_SUBDIRECTORY(tests) + diff --git a/src/os/src/detail/osTime.c b/src/os/src/detail/osTime.c index b78627f46f..87acae4a94 100644 --- a/src/os/src/detail/osTime.c +++ b/src/os/src/detail/osTime.c @@ -486,7 +486,7 @@ int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precisio start = (delta / pInterval->sliding + factor) * pInterval->sliding; if (pInterval->intervalUnit == 'd' || pInterval->intervalUnit == 'w') { - /* + /* * here we revised the start time of day according to the local time zone, * but in case of DST, the start time of one day need to be dynamically decided. */ @@ -502,8 +502,9 @@ int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precisio } int64_t end = start + pInterval->interval - 1; - if (end < t) { + while(end < t) { // move forward to the correct time window start += pInterval->sliding; + end = start + pInterval->interval - 1; } } diff --git a/src/os/tests/CMakeLists.txt b/src/os/tests/CMakeLists.txt new file mode 100644 index 0000000000..1a18a72b40 --- /dev/null +++ b/src/os/tests/CMakeLists.txt @@ -0,0 +1,15 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +PROJECT(TDengine) + +FIND_PATH(HEADER_GTEST_INCLUDE_DIR gtest.h /usr/include/gtest /usr/local/include/gtest) +FIND_LIBRARY(LIB_GTEST_STATIC_DIR libgtest.a /usr/lib/ /usr/local/lib) + +IF (HEADER_GTEST_INCLUDE_DIR AND LIB_GTEST_STATIC_DIR) + MESSAGE(STATUS "gTest library found, build unit test") + + INCLUDE_DIRECTORIES(${HEADER_GTEST_INCLUDE_DIR}) + AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} SOURCE_LIST) + + ADD_EXECUTABLE(osTest ${SOURCE_LIST}) + TARGET_LINK_LIBRARIES(osTest taos osdetail tutil common gtest pthread) +ENDIF() \ No newline at end of file diff --git a/src/os/tests/test.cpp b/src/os/tests/test.cpp new file mode 100644 index 0000000000..600e5d71a7 --- /dev/null +++ b/src/os/tests/test.cpp @@ -0,0 +1,34 @@ +#include "os.h" +#include +#include +#include + +#include "taos.h" +#include "tstoken.h" +#include "tutil.h" + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} + +// test function in os module +TEST(testCase, parse_time) { + taos_options(TSDB_OPTION_TIMEZONE, "GMT-8"); + deltaToUtcInitOnce(); + + // window: 1500000001000, 1500002000000 + // pQuery->interval: interval: 86400000, sliding:3600000 + int64_t key = 1500000001000; + SInterval interval = {0}; + interval.interval = 86400000; + interval.intervalUnit = 'd'; + interval.sliding = 3600000; + interval.slidingUnit = 'h'; + + int64_t s = taosTimeTruncate(key, &interval, TSDB_TIME_PRECISION_MILLI); + ASSERT_TRUE(s + interval.interval >= key); +} + + + -- GitLab