Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 48d0a48

Browse files
[CDRIVER-6107] Add Test Case Tag Support (#2135)
* `mlib_check` supports an explanatory string with all assertions * Define a mutable string type * Add a "vector" template header * String functions for trimming whitespace * Use mlib strings and vec types in TestSuite files * Add support for test cases to declare tags (labels) This change allows for test cases to declare any number of associated "tags". The tags are specified after the test case name string as a list of bracketed tags, mimicking Catch2's syntax. The LoadTests.cmake script has been modified to apply test case's declared tags as CTest labels. This also gives us the ability to apply test fixture requirements granularly on only tests that declare their requirement (via a tag). * Tests can declare resource locks in tags * No -Wmissing-braces anywhere * Define a CTest fixture for our simple HTTP server * Add test tags and properties throughout * Fix OpenSSL test that requires an SSL context * Add "unlikely" annotations around direct overflow checks
1 parent 20d648b commit 48d0a48

File tree

62 files changed

+2522
-770
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2522
-770
lines changed

‎.clang-format‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ ForEachMacros:
149149
- mlib_foreach_urange
150150
- mlib_foreach
151151
- mlib_foreach_arr
152+
- mlib_vec_foreach
152153
IfMacros:
153154
- mlib_assert_aborts
154155
- KJ_IF_MAYBE

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ if(ENABLE_MAINTAINER_FLAGS)
292292
gnu-like:-Wuninitialized
293293
# Disabled, for now:
294294
gnu-like:-Wno-strict-aliasing
295+
gnu-like:-Wno-missing-braces
295296
# Sign-comparison-mismatch:
296297
gnu-like:-Wsign-compare
297298
msvc:/we4018

‎Earthfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ PREP_CMAKE:
137137
FUNCTION
138138
# Run all CMake commands using uvx:
139139
RUN __alias cmake uvx cmake
140+
RUN __alias ctest uvx --from=cmake ctest
140141
# Executing any CMake command will warm the cache:
141142
RUN cmake --version | head -n 1
142143

‎build/cmake/LoadTests.cmake‎

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,88 @@
33
# allowing CTest to control the execution, parallelization, and collection of
44
# test results.
55

6-
if(NOT EXISTS "${TEST_LIBMONGOC_EXE}")
6+
if(NOT EXISTS "${TEST_LIBMONGOC_EXE}")
77
# This will fail if 'test-libmongoc' is not compiled yet.
8-
message (WARNING "The test executable ${TEST_LIBMONGOC_EXE} is not present. "
9-
"Its tests will not be registered")
10-
add_test (mongoc/not-found NOT_FOUND)
11-
return ()
12-
endif ()
13-
14-
# Get the list of tests
15-
execute_process (
16-
COMMAND "${TEST_LIBMONGOC_EXE}" --list-tests --no-fork
17-
OUTPUT_VARIABLE tests_out
8+
message(WARNING "The test executable ${TEST_LIBMONGOC_EXE} is not present. "
9+
"Its tests will not be registered")
10+
add_test(mongoc/not-found NOT_FOUND)
11+
return()
12+
endif()
13+
14+
# Get the list of tests. This command emits CMake code that defines variables for
15+
# all test cases defined in the suite
16+
execute_process(
17+
COMMAND "${TEST_LIBMONGOC_EXE}" --tests-cmake --no-fork
18+
OUTPUT_VARIABLE tests_cmake
1819
WORKING_DIRECTORY "${SRC_ROOT}"
1920
RESULT_VARIABLE retc
2021
)
21-
if(retc)
22+
if(retc)
2223
# Failed to list the tests. That's bad.
23-
message(FATAL_ERROR "Failed to run test-libmongoc to discover tests [${retc}]:\n${tests_out}")
24-
endif()
24+
message(FATAL_ERROR "Failed to run test-libmongoc to discover tests [${retc}]:\n${tests_out}")
25+
endif()
2526

26-
# Split lines on newlines
27-
string (REPLACE "\n"";" lines "${tests_out}")
27+
# Execute the code that defines the test case information
28+
cmake_language(EVAL CODE "${tests_cmake}")
2829

29-
# TODO: Allow individual test cases to specify the fixtures they want.
30-
set (all_fixtures "mongoc/fixtures/fake_kms_provider_server")
31-
set (all_env
30+
# Define environment variables that are common to all test cases
31+
set(all_env
3232
TEST_KMS_PROVIDER_HOST=localhost:14987 # Refer: Fixtures.cmake
3333
)
3434

35-
# Generate the test definitions
36-
foreach (line IN LISTS lines)
37-
if (NOT line MATCHES "^/")
38-
# Only generate if the line begins with `/`, which all tests should.
39-
continue ()
40-
endif ()
41-
# The new test name is prefixed with 'mongoc'
42-
set (test "mongoc${line}")
43-
# Define the test. Use `--ctest-run` to tell it that CTest is in control.
44-
add_test ("${test}" "${TEST_LIBMONGOC_EXE}" --ctest-run "${line}")
45-
set_tests_properties ("${test}" PROPERTIES
35+
function(list_select list_var)
36+
cmake_parse_arguments(PARSE_ARGV 1 arg "" "SELECT;REPLACE;OUT" "")
37+
set(seq "${${list_var}}")
38+
list(FILTER seq INCLUDE REGEX "${arg_SELECT}")
39+
list(TRANSFORM seq REPLACE "${arg_SELECT}" "${arg_REPLACE}")
40+
set("${arg_OUT}" "${seq}" PARENT_SCOPE)
41+
endfunction()
42+
43+
# The emitted code defines a list MONGOC_TESTS with the name of every test case
44+
# in the suite.
45+
foreach(casename IN LISTS MONGOC_TESTS)
46+
set(name "mongoc${casename}")
47+
# Run the program with --ctest-run to select only this one test case
48+
add_test("${name}" "${TEST_LIBMONGOC_EXE}" --ctest-run "${casename}")
49+
# The emitted code defines a TAGS list for every test case that it emits. We use
50+
# these as the LABELS for the test case
51+
unset(labels)
52+
set(labels "${MONGOC_TEST_${casename}_TAGS}")
53+
54+
# Find what test fixtures the test wants by inspecting labels. Each "uses:"
55+
# label defines the names of the test fixtures that a particular case requires
56+
list_select(labels SELECT "^uses:(.*)$" REPLACE "mongoc/fixtures/\\1" OUT fixtures)
57+
58+
# For any "lock:..." labels, add a resource lock with the corresponding name
59+
list_select(labels SELECT "^lock:(.*)$" REPLACE "\\1" OUT locks)
60+
61+
# Tests can set a timeout with a tag:
62+
list_select(labels SELECT "^timeout:(.*)$" REPLACE "\\1" OUT timeout)
63+
if(NOT timeout)
64+
# Default timeout of 10 seconds. If a test takes longer than this, it either
65+
# has a bug or it needs to declare a longer timeout.
66+
set(timeout 10)
67+
endif()
68+
69+
# Add a label for all test cases generated via this script so that they
70+
# can be (de)selected separately:
71+
list(APPEND labels test-libmongoc-generated)
72+
# Set up the test:
73+
set_tests_properties("${name}" PROPERTIES
4674
# test-libmongoc expects to execute in the root of the source directory
4775
WORKING_DIRECTORY "${SRC_ROOT}"
4876
# If a test emits '@@ctest-skipped@@', this tells us that the test is
4977
# skipped.
5078
SKIP_REGULAR_EXPRESSION "@@ctest-skipped@@"
51-
# 45 seconds of timeout on each test.
52-
TIMEOUT 45
53-
FIXTURES_REQUIRED "${all_fixtures}"
79+
# Apply a timeout to each test, either the default or one from test tags
80+
TIMEOUT "${timeout}"
81+
# Common environment variables:
5482
ENVIRONMENT "${all_env}"
55-
# Mark all tests generated from the executable, so they can be (de)selected
56-
# for execution separately.
57-
LABELS "test-libmongoc-generated"
58-
)
59-
endforeach ()
83+
# Apply the labels
84+
LABELS "${labels}"
85+
# Fixture requirements:
86+
FIXTURES_REQUIRED "${fixtures}"
87+
# Test may lock resources:
88+
RESOURCE_LOCK "${locks}"
89+
)
90+
endforeach()

‎build/cmake/MongoC-Warnings.cmake‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,4 @@ mongoc_add_warning_options (
103103

104104
# Aside: Disable CRT insecurity warnings
105105
msvc:/D_CRT_SECURE_NO_WARNINGS
106-
)
106+
)

‎build/cmake/TestFixtures.cmake‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,11 @@ mongo_define_subprocess_fixture(
4747
"${_MONGOC_BUILD_SCRIPT_DIR}/bottle.py" fake_kms_provider_server:kms_provider
4848
--bind localhost:14987 # Port 14987 chosen arbitrarily
4949
)
50+
51+
# Run our very simple HTTP server in a fixture process
52+
mongo_define_subprocess_fixture(
53+
mongoc/fixtures/simple-http-server-18000
54+
SPAWN_WAIT 1
55+
COMMAND
56+
$<TARGET_FILE:Python3::Interpreter> -u "${mongo-c-driver_SOURCE_DIR}/.evergreen/scripts/simple_http_server.py"
57+
)

‎src/common/src/mlib/config.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,23 @@
397397
MLIB_IF_GNU_LIKE(mlib_gnu_warning_disable("-Wunused-parameter");) \
398398
MLIB_IF_MSVC(mlib_msvc_warning(disable : 4100);) mlib_static_assert(1, "")
399399

400+
#if mlib_is_clang()
401+
#define mlib_printf_attribute(f, v) __attribute__((format(printf, f, v)))
402+
#elif mlib_is_gcc()
403+
#define mlib_printf_attribute(f, v) __attribute__((format(gnu_printf, f, v)))
404+
#else
405+
#define mlib_printf_attribute(f, v)
406+
#endif
407+
408+
/**
409+
* @brief Annotate a boolean expression as "likely to be true" to guide the optimizer.
410+
* Use this very sparingly.
411+
*/
412+
#define mlib_likely(...) MLIB_IF_ELSE(mlib_is_gnu_like())(__builtin_expect(!!(__VA_ARGS__), 1))((__VA_ARGS__))
413+
/**
414+
* @brief Annotate a boolean expression as "likely to be untrue" to guide the optimizer.
415+
* Use this very sparingly.
416+
*/
417+
#define mlib_unlikely(...) MLIB_IF_ELSE(mlib_is_gnu_like())(__builtin_expect(!!(__VA_ARGS__), 0))((__VA_ARGS__))
418+
400419
#endif // MLIB_CONFIG_H_INCLUDED

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /