Skip to content

Commit

Permalink
Move unit tests context to platform heap instead of stack/static (#19931
Browse files Browse the repository at this point in the history
)

* Split out a separate testing support library for use in tests, prepare to have heap allocated unit test contexts

* Start moving some tests to executors with/without context

* move more tests to specific runners

* More tests updated to use heap context

* Final batch of test updates (I think)

* Restyled by whitespace

* Restyled by clang-format

* Restyled by gn

* Fix efr32 build

* Fix simulated builds

* Restyled by gn

* make qemu compile

* Fix CASE Session cleanup

* Fix memory leak in tlv tests (API misuse)

* Restyled by clang-format

* Fix memory leak in TestUdcMessages.cpp

* Fix memory leak on Crypto PAL OpenSSL object usage on VID/PID decoding

* Fix memory leak in TestReadInteraction

* Restyled by clang-format

* Fix fake platform tests

* Make darwin clang-tidy happy

* Restyled by clang-format

* Revert stack size increase since unit test contexts should be on the heap now

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Jan 10, 2024
1 parent 47e16bd commit 5281683
Show file tree
Hide file tree
Showing 76 changed files with 198 additions and 212 deletions.
6 changes: 1 addition & 5 deletions build/config/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,7 @@ config("warnings_common") {

if (current_os != "mac" && current_os != "ios" && current_os != "linux" &&
current_os != "win" && current_os != "tizen" && current_os != "webos") {
# TODO(#19951): Make this go back to 8192 once unit test contexts are no
# longer on the stack.
# cflags += [ "-Wstack-usage=8192" ]

cflags += [ "-Wstack-usage=8704" ] # 8192 + 512
cflags += [ "-Wstack-usage=8192" ]
}
}

Expand Down
2 changes: 2 additions & 0 deletions examples/placeholder/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ executable("chip-${chip_tests_zap_config}") {
"${chip_root}/src/app/tests/suites/credentials:dac_provider",
"${chip_root}/src/app/tests/suites/pics",
"${chip_root}/src/lib",
"${chip_root}/src/lib/support:testing", # For sleepMillis. TODO: this is
# odd and should be fixed
]

include_dirs = [ "include" ]
Expand Down
1 change: 1 addition & 0 deletions src/access/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ chip_test_suite("tests") {

public_deps = [
"${chip_root}/src/access",
"${chip_root}/src/lib/support:testing",
"${nlunit_test_root}:nlunit-test",
]
}
1 change: 1 addition & 0 deletions src/app/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ chip_test_suite("tests") {
"${chip_root}/src/app/tests:helpers",
"${chip_root}/src/app/util/mock:mock_ember",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/support:testing",
"${nlunit_test_root}:nlunit-test",
]

Expand Down
5 changes: 2 additions & 3 deletions src/app/tests/TestBufferedReadCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <app/data-model/DecodableList.h>
#include <app/data-model/Decode.h>
#include <app/tests/AppTestContext.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <nlunit-test.h>
#include <vector>
Expand Down Expand Up @@ -618,10 +619,8 @@ nlTestSuite theSuite =

int TestBufferedReadCallback()
{
TestContext gContext;
gSuite = &theSuite;
nlTestRunner(&theSuite, &gContext);
return (nlTestRunnerStats(&theSuite));
return chip::ExecuteTestsWithContext<TestContext>(&theSuite);
}

CHIP_REGISTER_TEST_SUITE(TestBufferedReadCallback)
5 changes: 2 additions & 3 deletions src/app/tests/TestClusterStateCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <app/data-model/DecodableList.h>
#include <app/data-model/Decode.h>
#include <app/tests/AppTestContext.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <nlunit-test.h>
#include <string.h>
Expand Down Expand Up @@ -636,10 +637,8 @@ nlTestSuite theSuite =

int TestClusterStateCache()
{
TestContext gContext;
gSuite = &theSuite;
nlTestRunner(&theSuite, &gContext);
return (nlTestRunnerStats(&theSuite));
return chip::ExecuteTestsWithContext<TestContext>(&theSuite);
}

CHIP_REGISTER_TEST_SUITE(TestClusterStateCache)
5 changes: 2 additions & 3 deletions src/app/tests/TestCommandInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <lib/core/CHIPTLVDebug.hpp>
#include <lib/core/CHIPTLVUtilities.hpp>
#include <lib/support/ErrorStr.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/logging/CHIPLogging.h>
#include <messaging/ExchangeContext.h>
Expand Down Expand Up @@ -933,9 +934,7 @@ nlTestSuite sSuite =

int TestCommandInteraction()
{
TestContext gContext;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestCommandInteraction)
6 changes: 2 additions & 4 deletions src/app/tests/TestEventLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <lib/support/CHIPCounter.h>
#include <lib/support/EnforceFormat.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/ScopedBuffer.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/logging/Constants.h>
#include <messaging/ExchangeContext.h>
Expand Down Expand Up @@ -332,9 +332,7 @@ nlTestSuite sSuite =

int TestEventLogging()
{
TestContext gContext;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestEventLogging)
5 changes: 2 additions & 3 deletions src/app/tests/TestEventOverflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <lib/support/CHIPCounter.h>
#include <lib/support/EnforceFormat.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/logging/Constants.h>
#include <messaging/ExchangeContext.h>
Expand Down Expand Up @@ -185,9 +186,7 @@ nlTestSuite sSuite =

int TestEventOverflow()
{
TestContext gContext;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestEventOverflow)
5 changes: 2 additions & 3 deletions src/app/tests/TestFabricScopedEventLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <lib/support/CHIPCounter.h>
#include <lib/support/EnforceFormat.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/logging/Constants.h>
#include <messaging/ExchangeContext.h>
Expand Down Expand Up @@ -294,9 +295,7 @@ nlTestSuite sSuite =

int TestFabricScopedEventLogging()
{
TestContext gContext;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestFabricScopedEventLogging)
5 changes: 2 additions & 3 deletions src/app/tests/TestInteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <lib/core/CHIPTLVDebug.hpp>
#include <lib/core/CHIPTLVUtilities.hpp>
#include <lib/support/ErrorStr.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <messaging/ExchangeContext.h>
#include <messaging/Flags.h>
Expand Down Expand Up @@ -250,9 +251,7 @@ nlTestSuite sSuite =

int TestInteractionModelEngine()
{
TestContext gContext;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestInteractionModelEngine)
5 changes: 2 additions & 3 deletions src/app/tests/TestReadInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <lib/core/CHIPTLVUtilities.hpp>
#include <lib/support/CHIPCounter.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <messaging/ExchangeContext.h>
#include <messaging/Flags.h>
Expand Down Expand Up @@ -2788,9 +2789,7 @@ nlTestSuite sSuite =

int TestReadInteraction()
{
TestContext sContext;
nlTestRunner(&sSuite, &sContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestReadInteraction)
5 changes: 2 additions & 3 deletions src/app/tests/TestReportingEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <lib/core/CHIPTLVDebug.hpp>
#include <lib/core/CHIPTLVUtilities.hpp>
#include <lib/support/ErrorStr.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <messaging/ExchangeContext.h>
#include <messaging/Flags.h>
Expand Down Expand Up @@ -349,9 +350,7 @@ nlTestSuite sSuite =

int TestReportingEngine()
{
TestContext gContext;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestReportingEngine)
5 changes: 2 additions & 3 deletions src/app/tests/TestTimedHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <app/tests/AppTestContext.h>
#include <lib/core/CHIPCore.h>
#include <lib/core/CHIPTLV.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/UnitTestUtils.h>
#include <protocols/interaction_model/Constants.h>
Expand Down Expand Up @@ -267,9 +268,7 @@ nlTestSuite sSuite =

int TestTimedHandler()
{
TestContext gContext;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestTimedHandler)
5 changes: 2 additions & 3 deletions src/app/tests/TestWriteInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <lib/support/ErrorStr.h>
#include <lib/support/TestGroupData.h>
#include <lib/support/TestPersistentStorageDelegate.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <messaging/ExchangeContext.h>
#include <messaging/Flags.h>
Expand Down Expand Up @@ -631,9 +632,7 @@ nlTestSuite sSuite =

int TestWriteInteraction()
{
TestContext gContext;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestWriteInteraction)
1 change: 1 addition & 0 deletions src/app/tests/suites/commands/interaction_model/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ static_library("interaction_model") {
public_deps = [
"${chip_root}/src/app",
"${chip_root}/src/lib/support",
"${chip_root}/src/lib/support:testing",
]
}
1 change: 1 addition & 0 deletions src/app/util/mock/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ source_set("mock_ember") {
"${chip_root}/src/app",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/support",
"${chip_root}/src/lib/support:testing",
]

public_configs = [ ":mock_include" ]
Expand Down
1 change: 1 addition & 0 deletions src/ble/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ chip_test_suite("tests") {

public_deps = [
"${chip_root}/src/ble",
"${chip_root}/src/lib/support:testing",
"${nlunit_test_root}:nlunit-test",
]
}
1 change: 1 addition & 0 deletions src/controller/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ chip_test_suite("tests") {
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/src/app/tests:helpers",
"${chip_root}/src/controller",
"${chip_root}/src/lib/support:testing",
"${chip_root}/src/messaging/tests:helpers",
"${chip_root}/src/transport/raw/tests:helpers",
"${nlunit_test_root}:nlunit-test",
Expand Down
5 changes: 2 additions & 3 deletions src/controller/tests/TestEventCaching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <controller/InvokeInteraction.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/TimeUtils.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/UnitTestUtils.h>
#include <lib/support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -463,10 +464,8 @@ nlTestSuite sSuite =

int TestEventCaching()
{
TestContext gContext;
gSuite = &sSuite;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestEventCaching)
5 changes: 2 additions & 3 deletions src/controller/tests/TestEventChunking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <lib/support/CHIPCounter.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/TimeUtils.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/UnitTestUtils.h>
#include <lib/support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -545,10 +546,8 @@ nlTestSuite sSuite =

int TestReadChunkingTests()
{
TestContext gContext;
gSuite = &sSuite;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestReadChunkingTests)
5 changes: 2 additions & 3 deletions src/controller/tests/TestReadChunking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <functional>
#include <lib/support/ErrorStr.h>
#include <lib/support/TimeUtils.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/UnitTestUtils.h>
#include <lib/support/logging/CHIPLogging.h>
Expand Down Expand Up @@ -921,10 +922,8 @@ nlTestSuite sSuite =

int TestReadChunkingTests()
{
TestContext gContext;
gSuite = &sSuite;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestReadChunkingTests)
5 changes: 2 additions & 3 deletions src/controller/tests/TestServerCommandDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <controller/InvokeInteraction.h>
#include <controller/ReadInteraction.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/logging/CHIPLogging.h>
#include <messaging/tests/MessagingContext.h>
Expand Down Expand Up @@ -412,9 +413,7 @@ nlTestSuite sSuite =

int TestCommandInteractionTest()
{
TestContext gContext;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestCommandInteractionTest)
5 changes: 2 additions & 3 deletions src/controller/tests/TestWriteChunking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <app/util/attribute-storage.h>
#include <controller/InvokeInteraction.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/logging/CHIPLogging.h>
#include <messaging/tests/MessagingContext.h>
Expand Down Expand Up @@ -737,10 +738,8 @@ nlTestSuite sSuite =

int TestWriteChunkingTests()
{
TestContext gContext;
gSuite = &sSuite;
nlTestRunner(&sSuite, &gContext);
return (nlTestRunnerStats(&sSuite));
return chip::ExecuteTestsWithContext<TestContext>(&sSuite);
}

CHIP_REGISTER_TEST_SUITE(TestWriteChunkingTests)
1 change: 1 addition & 0 deletions src/controller/tests/data_model/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ chip_test_suite("data_model") {
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/src/app/tests:helpers",
"${chip_root}/src/app/util/mock:mock_ember",
"${chip_root}/src/lib/support:testing",
"${chip_root}/src/messaging/tests:helpers",
"${chip_root}/src/transport/raw/tests:helpers",
"${nlunit_test_root}:nlunit-test",
Expand Down
Loading

0 comments on commit 5281683

Please sign in to comment.