Skip to content

Commit

Permalink
Make test suite setup and teardown functions static (#33330)
Browse files Browse the repository at this point in the history
* Make test suite setup and teardown functions static

* Restyled by clang-format

* Fixed bug in LoopbackTransport initialization

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
mbknust and restyled-commits authored May 14, 2024
1 parent 3219a5f commit d594086
Show file tree
Hide file tree
Showing 28 changed files with 191 additions and 197 deletions.
8 changes: 4 additions & 4 deletions src/app/tests/AppTestContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ class AppContext : public LoopbackMessagingContext
{
public:
// Performs shared setup for all tests in the test suite
void SetUpTestSuite() override;
static void SetUpTestSuite();
// Performs shared teardown for all tests in the test suite
void TearDownTestSuite() override;
static void TearDownTestSuite();
// Performs setup for each individual test in the test suite
void SetUp() override;
void SetUp();
// Performs teardown for each individual test in the test suite
void TearDown() override;
void TearDown();
};

} // namespace Test
Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestAclAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ const nlTest sTests[] = {
nlTestSuite sSuite = {
"TestAclAttribute",
&sTests[0],
TestAccessContext::nlTestSetUpTestSuite,
TestAccessContext::nlTestTearDownTestSuite,
TestAccessContext::nlTestSetUp,
TestAccessContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestAccessContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestAccessContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestAccessContext, SetUp),
NL_TEST_WRAP_METHOD(TestAccessContext, TearDown),
};

} // namespace
Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestAclEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ nlTestSuite sSuite =
{
"TestAclEvent",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};
// clang-format on

Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestBufferedReadCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,10 @@ nlTestSuite theSuite =
{
"TestBufferedReadCallback",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};

}
Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestClusterStateCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,10 +695,10 @@ nlTestSuite theSuite =
{
"TestClusterStateCache",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};

}
Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestCommandInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2120,10 +2120,10 @@ nlTestSuite sSuite =
{
"TestCommandInteraction",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};
// clang-format on

Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestEventLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,10 @@ const nlTest sTests[] = {
nlTestSuite sSuite = {
"EventLogging",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};

} // namespace
Expand Down
20 changes: 11 additions & 9 deletions src/app/tests/TestEventLoggingNoUTCTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ class TestContext : public chip::Test::AppContext
{
public:
// Performs shared setup for all tests in the test suite
void SetUpTestSuite() override
static void SetUpTestSuite()
{
chip::Test::AppContext::SetUpTestSuite();
mClock.Emplace(chip::System::SystemClock());
sClock.Emplace(chip::System::SystemClock());
}

// Performs shared teardown for all tests in the test suite
void TearDownTestSuite() override
static void TearDownTestSuite()
{
mClock.ClearValue();
sClock.ClearValue();
chip::Test::AppContext::TearDownTestSuite();
}

Expand Down Expand Up @@ -125,9 +125,11 @@ class TestContext : public chip::Test::AppContext

private:
chip::MonotonicallyIncreasingCounter<chip::EventNumber> mEventCounter;
chip::Optional<MockClock> mClock;
static chip::Optional<MockClock> sClock;
};

chip::Optional<MockClock> TestContext::sClock;

void ENFORCE_FORMAT(1, 2) SimpleDumpWriter(const char * aFormat, ...)
{
va_list args;
Expand Down Expand Up @@ -370,10 +372,10 @@ const nlTest sTests[] = {
nlTestSuite sSuite = {
"EventLogging",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};

} // namespace
Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestEventOverflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ nlTestSuite sSuite =
{
"TestEventOverflow",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};
// clang-format on

Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestFabricScopedEventLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ nlTestSuite sSuite =
{
"TestFabricScopedEventLogging",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};
// clang-format on

Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestInteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,10 @@ nlTestSuite sSuite =
{
"TestInteractionModelEngine",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};
// clang-format on

Expand Down
44 changes: 25 additions & 19 deletions src/app/tests/TestReadInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ class TestContext : public chip::Test::AppContext
{
public:
// Performs shared setup for all tests in the test suite
void SetUpTestSuite() override
static void SetUpTestSuite()
{
chip::Test::AppContext::SetUpTestSuite();
gRealClock = &chip::System::SystemClock();
chip::System::Clock::Internal::SetSystemClockForTesting(&gMockClock);

if (mSyncScheduler)
if (sSyncScheduler)
{
gReportScheduler = chip::app::reporting::GetSynchronizedReportScheduler();
sUsingSubSync = true;
Expand All @@ -96,14 +96,8 @@ class TestContext : public chip::Test::AppContext
}
}

static int nlTestSetUpTestSuite_Sync(void * context)
{
static_cast<TestContext *>(context)->mSyncScheduler = true;
return nlTestSetUpTestSuite(context);
}

// Performs shared teardown for all tests in the test suite
void TearDownTestSuite() override
static void TearDownTestSuite()
{
chip::System::Clock::Internal::SetSystemClockForTesting(gRealClock);
chip::Test::AppContext::TearDownTestSuite();
Expand Down Expand Up @@ -133,9 +127,21 @@ class TestContext : public chip::Test::AppContext
chip::Test::AppContext::TearDown();
}

private:
protected:
chip::MonotonicallyIncreasingCounter<chip::EventNumber> mEventCounter;
bool mSyncScheduler = false;
static bool sSyncScheduler;
};

bool TestContext::sSyncScheduler = false;

class TestSyncContext : public TestContext
{
public:
static void SetUpTestSuite()
{
sSyncScheduler = true;
TestContext::SetUpTestSuite();
}
};

class TestEventGenerator : public chip::app::EventLoggingDelegate
Expand Down Expand Up @@ -5146,19 +5152,19 @@ const nlTest sTests[] = {
nlTestSuite sSuite = {
"TestReadInteraction",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};

nlTestSuite sSyncSuite = {
"TestSyncReadInteraction",
&sTests[0],
TestContext::nlTestSetUpTestSuite_Sync,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestSyncContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestSyncContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestSyncContext, SetUp),
NL_TEST_WRAP_METHOD(TestSyncContext, TearDown),
};

} // namespace
Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestReportScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,10 +821,10 @@ static nlTest sTests[] = {
nlTestSuite sSuite = {
"TestReportScheduler",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};

} // namespace
Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestReportingEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,10 @@ nlTestSuite sSuite =
{
"TestReportingEngine",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};
// clang-format on

Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestTimedHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ nlTestSuite sSuite =
{
"TestTimedHandler",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};
// clang-format on

Expand Down
8 changes: 4 additions & 4 deletions src/app/tests/TestWriteInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,10 +1063,10 @@ const nlTest sTests[] =
nlTestSuite sSuite = {
"TestWriteInteraction",
&sTests[0],
TestContext::nlTestSetUpTestSuite,
TestContext::nlTestTearDownTestSuite,
TestContext::nlTestSetUp,
TestContext::nlTestTearDown,
NL_TEST_WRAP_FUNCTION(TestContext::SetUpTestSuite),
NL_TEST_WRAP_FUNCTION(TestContext::TearDownTestSuite),
NL_TEST_WRAP_METHOD(TestContext, SetUp),
NL_TEST_WRAP_METHOD(TestContext, TearDown),
};

} // namespace
Expand Down
14 changes: 14 additions & 0 deletions src/lib/support/UnitTestRegistration.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@
VerifyOrDie(chip::RegisterUnitTests(&FUNCTION) == CHIP_NO_ERROR); \
}

// TODO: remove these once transition to pw_unit_test is completed
#define NL_TEST_WRAP_FUNCTION(FUNCTION) \
[](void * _context) -> int { \
FUNCTION(); \
return SUCCESS; \
}

#define NL_TEST_WRAP_METHOD(CLASS, METHOD) \
[](void * context) -> int { \
auto ctx = static_cast<CLASS *>(context); \
ctx->METHOD(); \
return SUCCESS; \
}

namespace chip {

typedef int (*UnitTestTriggerFunction)();
Expand Down
8 changes: 7 additions & 1 deletion src/messaging/tests/MessagingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ CHIP_ERROR MessagingContext::Init(TransportMgrBase * transport, IOContext * ioCo
ReturnErrorOnFailure(mExchangeManager.Init(&mSessionManager));
ReturnErrorOnFailure(mMessageCounterManager.Init(&mExchangeManager));

if (mInitializeNodes)
if (sInitializeNodes)
{
ReturnErrorOnFailure(CreateAliceFabric());
ReturnErrorOnFailure(CreateBobFabric());
Expand Down Expand Up @@ -112,6 +112,8 @@ using namespace System::Clock::Literals;
constexpr chip::System::Clock::Timeout MessagingContext::kResponsiveIdleRetransTimeout;
constexpr chip::System::Clock::Timeout MessagingContext::kResponsiveActiveRetransTimeout;

bool MessagingContext::sInitializeNodes = true;

void MessagingContext::SetMRPMode(MRPMode mode)
{
if (mode == MRPMode::kDefault)
Expand Down Expand Up @@ -302,6 +304,10 @@ Messaging::ExchangeContext * MessagingContext::NewExchangeToBob(Messaging::Excha
return mExchangeManager.NewContext(GetSessionAliceToBob(), delegate, isInitiator);
}

LoopbackTransportManager LoopbackMessagingContext::sLoopbackTransportManager;

UDPTransportManager UDPMessagingContext::sUDPTransportManager;

void MessageCapturer::OnMessageReceived(const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
const SessionHandle & session, DuplicateMessage isDuplicate,
System::PacketBufferHandle && msgBuf)
Expand Down
Loading

0 comments on commit d594086

Please sign in to comment.