From 4a8b6011f1288a3c29540699074ecb3ab3b12539 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 30 Aug 2024 15:45:26 +0800 Subject: [PATCH] enh: add assert CI test case --- include/util/tlockfree.h | 3 --- source/util/src/tcache.c | 3 --- source/util/src/tref.c | 4 +-- tests/ci/count_assert.py | 53 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 tests/ci/count_assert.py diff --git a/include/util/tlockfree.h b/include/util/tlockfree.h index 1a575f8ea1..24e83f23ca 100644 --- a/include/util/tlockfree.h +++ b/include/util/tlockfree.h @@ -39,7 +39,6 @@ typedef void (*_ref_fn_t)(const void *pObj); // set the initial reference count value #define T_REF_INIT_VAL(x, _v) \ do { \ - assert(_v >= 0); \ atomic_store_32(&((x)->_ref.val), (_v)); \ } while (0) @@ -64,8 +63,6 @@ typedef void (*_ref_fn_t)(const void *pObj); } \ } while (0) -#define T_REF_VAL_CHECK(x) assert((x)->_ref.val >= 0); - #define T_REF_VAL_GET(x) (x)->_ref.val // single writer multiple reader lock diff --git a/source/util/src/tcache.c b/source/util/src/tcache.c index c33e0ee501..3b5c46c2b3 100644 --- a/source/util/src/tcache.c +++ b/source/util/src/tcache.c @@ -787,9 +787,6 @@ void taosTrashcanEmpty(SCacheObj *pCacheObj, bool force) { STrashElem *pElem = pCacheObj->pTrash; while (pElem) { - T_REF_VAL_CHECK(pElem->pData); - // A S S E R T(pElem->next != pElem && pElem->prev != pElem); - if (force || (T_REF_VAL_GET(pElem->pData) == 0)) { uDebug("cache:%s, key:%p, %p removed from trashcan. numOfElem in trashcan:%d", pCacheObj->name, pElem->pData->key, pElem->pData->data, pCacheObj->numOfElemsInTrash - 1); diff --git a/source/util/src/tref.c b/source/util/src/tref.c index 4b1b477e06..0eac7b4427 100644 --- a/source/util/src/tref.c +++ b/source/util/src/tref.c @@ -455,9 +455,7 @@ static void taosLockList(int64_t *lockedBy) { static void taosUnlockList(int64_t *lockedBy) { int64_t tid = taosGetSelfPthreadId(); - if (atomic_val_compare_exchange_64(lockedBy, tid, 0) != tid) { - ASSERTS(false, "atomic_val_compare_exchange_64 tid failed"); - } + (void)atomic_val_compare_exchange_64(lockedBy, tid, 0); } static void taosInitRefModule(void) { (void)taosThreadMutexInit(&tsRefMutex, NULL); } diff --git a/tests/ci/count_assert.py b/tests/ci/count_assert.py new file mode 100644 index 0000000000..415c197b3f --- /dev/null +++ b/tests/ci/count_assert.py @@ -0,0 +1,53 @@ +import os +import re + +# List of source directories to search +source_dirs = [ + "community/source", + "community/include", + "enterprise/src/plugins/" +] + +# List of directories to exclude +exclude_dirs = [ + "community/source/client/jni" +] + +# List of files to exclude +exclude_source_files = [ + "community/source/libs/parser/src/sql.c", + "community/source/util/src/tlog.c", + "community/include/util/tlog.h" +] + +def grep_asserts_in_file(file_path, summary_list, detaild_list): + """Search for assert, ASSERTS, or ASSERT function calls in a file and print them.""" + match_count = 0 + with open(file_path, 'r') as file: + for line_number, line in enumerate(file, start=1): + if re.search(r'\bassert\(.*\)|\bASSERT\(.*\)|\bASSERTS\(.*\)|\bASSERT_CORE\(.*\)', line): + detaild_list.append(f"{file_path}:{line_number}: {line.strip()}") + match_count += 1 + if match_count > 0: + summary_list.append(f"Total matches in {file_path}: {match_count}") + +def traverse_and_grep(source_dirs, exclude_dirs, exclude_source_files): + """Traverse directories and grep for assert, ASSERTS, or ASSERT function calls in .h and .c files.""" + summary_list = [] + detaild_list = [] + for source_dir in source_dirs: + for root, _, files in os.walk(source_dir): + # Skip directories named 'test' or 'tests' and directories in exclude_dirs + if 'test' in root.split(os.sep) or 'tests' in root.split(os.sep) or any(excluded in root for excluded in exclude_dirs): + continue + for file in files: + if file.endswith((".h", ".c")): + file_path = os.path.join(root, file) + if file_path not in exclude_source_files: + grep_asserts_in_file(file_path, summary_list, detaild_list) + return summary_list, detaild_list + +if __name__ == "__main__": + summary_list, detaild_list = traverse_and_grep(source_dirs, exclude_dirs, exclude_source_files) + print("\n".join(summary_list)) + # print("\n".join(detaild_list)) \ No newline at end of file