Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed refactoring of AppContext, MessagingContext, and its subclasses after PW test migration. #33497

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/app/tests/AppTestContext.cpp
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
* limitations under the License.
*/

#include <app/tests/AppTestContext.h>
#include "AppTestContext.h"

#include <access/AccessControl.h>
#include <access/examples/PermissiveAccessControlDelegate.h>
@@ -40,11 +40,10 @@ namespace Test {

void AppContext::SetUpTestSuite()
{
CHIP_ERROR err = CHIP_NO_ERROR;
LoopbackMessagingContext::SetUpTestSuite();
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = chip::DeviceLayer::PlatformMgr().InitChipStack()) == CHIP_NO_ERROR, AppServer,
"Init CHIP stack failed: %" CHIP_ERROR_FORMAT, err.Format());
VerifyOrReturn(!HasFailure()); // Stop if parent had a failure.

ASSERT_EQ(chip::DeviceLayer::PlatformMgr().InitChipStack(), CHIP_NO_ERROR);
}

void AppContext::TearDownTestSuite()
@@ -67,24 +66,23 @@ void AppContext::TearDownTestSuite()

void AppContext::SetUp()
{
CHIP_ERROR err = CHIP_NO_ERROR;
LoopbackMessagingContext::SetUp();
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = app::InteractionModelEngine::GetInstance()->Init(&GetExchangeManager(), &GetFabricTable(),
app::reporting::GetDefaultReportScheduler())) ==
CHIP_NO_ERROR,
AppServer, "Init InteractionModelEngine failed: %" CHIP_ERROR_FORMAT, err.Format());
VerifyOrReturn(!HasFailure()); // Stop if parent had a failure.

ASSERT_EQ(app::InteractionModelEngine::GetInstance()->Init(&GetExchangeManager(), &GetFabricTable(),
app::reporting::GetDefaultReportScheduler()),
CHIP_NO_ERROR);
Access::SetAccessControl(gPermissiveAccessControl);
VerifyOrDieWithMsg((err = Access::GetAccessControl().Init(chip::Access::Examples::GetPermissiveAccessControlDelegate(),
gDeviceTypeResolver)) == CHIP_NO_ERROR,
AppServer, "Init AccessControl failed: %" CHIP_ERROR_FORMAT, err.Format());
ASSERT_EQ(Access::GetAccessControl().Init(chip::Access::Examples::GetPermissiveAccessControlDelegate(), gDeviceTypeResolver),
CHIP_NO_ERROR);
}

void AppContext::TearDown()
{
Access::GetAccessControl().Finish();
Access::ResetAccessControlToDefault();
chip::app::InteractionModelEngine::GetInstance()->Shutdown();

LoopbackMessagingContext::TearDown();
}

4 changes: 2 additions & 2 deletions src/app/tests/AppTestContext.h
Original file line number Diff line number Diff line change
@@ -32,9 +32,9 @@ class AppContext : public LoopbackMessagingContext
// Performs shared teardown for all tests in the test suite
static void TearDownTestSuite();
// Performs setup for each individual test in the test suite
void SetUp();
virtual void SetUp();
// Performs teardown for each individual test in the test suite
void TearDown();
virtual void TearDown();
};

} // namespace Test
77 changes: 22 additions & 55 deletions src/controller/tests/TestEventCaching.cpp
Original file line number Diff line number Diff line change
@@ -16,8 +16,6 @@
* limitations under the License.
*/

#include <gtest/gtest.h>

#include "app-common/zap-generated/ids/Attributes.h"
#include "app-common/zap-generated/ids/Clusters.h"
#include "app/ClusterStateCache.h"
@@ -37,7 +35,6 @@
#include <lib/support/TimeUtils.h>
#include <lib/support/UnitTestUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <messaging/tests/MessagingContext.h>

using namespace chip;
using namespace chip::app;
@@ -50,42 +47,17 @@ static uint8_t gInfoEventBuffer[4096];
static uint8_t gCritEventBuffer[4096];
static chip::app::CircularEventBuffer gCircularEventBuffer[3];

using TestContext = chip::Test::AppContext;

//
// The generated endpoint_config for the controller app has Endpoint 1
// already used in the fixed endpoint set of size 1. Consequently, let's use the next
// number higher than that for our dynamic test endpoint.
//
constexpr EndpointId kTestEndpointId = 2;

class TestEventCaching : public ::testing::Test
class TestEventCaching : public Test::AppContext
{
public:
// Performs shared setup for all tests in the test suite
static void SetUpTestSuite()
{
if (mpContext == nullptr)
{
mpContext = new TestContext();
ASSERT_NE(mpContext, nullptr);
}
mpContext->SetUpTestSuite();
}

// Performs shared teardown for all tests in the test suite
static void TearDownTestSuite()
{
mpContext->TearDownTestSuite();
if (mpContext != nullptr)
{
delete mpContext;
mpContext = nullptr;
}
}

protected:
// Performs setup for each test in the suite
// Performs setup for each test in the suite. Run once for each test function.
void SetUp()
{
const chip::app::LogStorageResources logStorageResources[] = {
@@ -94,29 +66,24 @@ class TestEventCaching : public ::testing::Test
{ &gCritEventBuffer[0], sizeof(gCritEventBuffer), chip::app::PriorityLevel::Critical },
};

mpContext->SetUp();
AppContext::SetUp(); // Call parent.
VerifyOrReturn(!HasFailure()); // Stop if parent had a failure.

CHIP_ERROR err = CHIP_NO_ERROR;
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = mEventCounter.Init(0)) == CHIP_NO_ERROR, AppServer,
"Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format());
chip::app::EventManagement::CreateEventManagement(&mpContext->GetExchangeManager(), ArraySize(logStorageResources),
ASSERT_EQ(mEventCounter.Init(0), CHIP_NO_ERROR);
chip::app::EventManagement::CreateEventManagement(&GetExchangeManager(), ArraySize(logStorageResources),
gCircularEventBuffer, logStorageResources, &mEventCounter);
}

// Performs teardown for each test in the suite
// Performs teardown for each test in the suite. Run once for each test function.
void TearDown()
{
chip::app::EventManagement::DestroyEventManagement();
mpContext->TearDown();
AppContext::TearDown(); // Call parent.
}

static TestContext * mpContext;

private:
MonotonicallyIncreasingCounter<EventNumber> mEventCounter;
};
TestContext * TestEventCaching::mpContext = nullptr;

//clang-format off
DECLARE_DYNAMIC_ATTRIBUTE_LIST_BEGIN(testClusterAttrs)
@@ -179,7 +146,7 @@ void GenerateEvents(chip::EventNumber & firstEventNumber, chip::EventNumber & la
*/
TEST_F(TestEventCaching, TestBasicCaching)
{
auto sessionHandle = mpContext->GetSessionBobToAlice();
auto sessionHandle = GetSessionBobToAlice();
app::InteractionModelEngine * engine = app::InteractionModelEngine::GetInstance();

// Initialize the ember side server logic
@@ -207,12 +174,12 @@ TEST_F(TestEventCaching, TestBasicCaching)
TestReadCallback readCallback;

{
app::ReadClient readClient(engine, &mpContext->GetExchangeManager(),
readCallback.mClusterCacheAdapter.GetBufferedCallback(), app::ReadClient::InteractionType::Read);
app::ReadClient readClient(engine, &GetExchangeManager(), readCallback.mClusterCacheAdapter.GetBufferedCallback(),
app::ReadClient::InteractionType::Read);

EXPECT_EQ(readClient.SendRequest(readParams), CHIP_NO_ERROR);

mpContext->DrainAndServiceIO();
DrainAndServiceIO();

uint8_t generationCount = 0;
readCallback.mClusterCacheAdapter.ForEachEventData(
@@ -340,12 +307,12 @@ TEST_F(TestEventCaching, TestBasicCaching)
GenerateEvents(firstEventNumber, lastEventNumber);

{
app::ReadClient readClient(engine, &mpContext->GetExchangeManager(),
readCallback.mClusterCacheAdapter.GetBufferedCallback(), app::ReadClient::InteractionType::Read);
app::ReadClient readClient(engine, &GetExchangeManager(), readCallback.mClusterCacheAdapter.GetBufferedCallback(),
app::ReadClient::InteractionType::Read);

EXPECT_EQ(readClient.SendRequest(readParams), CHIP_NO_ERROR);

mpContext->DrainAndServiceIO();
DrainAndServiceIO();

//
// Validate that we still have all 5 of the old events we received, as well as the new ones that just got generated.
@@ -392,8 +359,8 @@ TEST_F(TestEventCaching, TestBasicCaching)
// we don't receive events lower than that value.
//
{
app::ReadClient readClient(engine, &mpContext->GetExchangeManager(),
readCallback.mClusterCacheAdapter.GetBufferedCallback(), app::ReadClient::InteractionType::Read);
app::ReadClient readClient(engine, &GetExchangeManager(), readCallback.mClusterCacheAdapter.GetBufferedCallback(),
app::ReadClient::InteractionType::Read);

readCallback.mClusterCacheAdapter.ClearEventCache();
constexpr EventNumber kLastSeenEventNumber = 3;
@@ -405,7 +372,7 @@ TEST_F(TestEventCaching, TestBasicCaching)

EXPECT_EQ(readClient.SendRequest(readParams), CHIP_NO_ERROR);

mpContext->DrainAndServiceIO();
DrainAndServiceIO();

// We should only get events with event numbers larger than kHighestEventNumberSeen.
EXPECT_EQ(readCallback.mEventsSeen, lastEventNumber - kLastSeenEventNumber);
@@ -441,12 +408,12 @@ TEST_F(TestEventCaching, TestBasicCaching)

{
readParams.mEventNumber.SetValue(5);
app::ReadClient readClient(engine, &mpContext->GetExchangeManager(),
readCallback.mClusterCacheAdapter.GetBufferedCallback(), app::ReadClient::InteractionType::Read);
app::ReadClient readClient(engine, &GetExchangeManager(), readCallback.mClusterCacheAdapter.GetBufferedCallback(),
app::ReadClient::InteractionType::Read);
readCallback.mClusterCacheAdapter.ClearEventCache(true);
EXPECT_EQ(readClient.SendRequest(readParams), CHIP_NO_ERROR);

mpContext->DrainAndServiceIO();
DrainAndServiceIO();

//
// Validate that we would receive 5 events
@@ -474,7 +441,7 @@ TEST_F(TestEventCaching, TestBasicCaching)
EXPECT_TRUE(highestEventNumber.HasValue() && highestEventNumber.Value() == 9);
}

EXPECT_EQ(mpContext->GetExchangeManager().GetNumActiveExchanges(), 0u);
EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 0u);

emberAfClearDynamicEndpoint(0);
}
Loading