Skip to content

Commit 302bf20

Browse files
committed
Made some helper functions into class methods.
1 parent a02c9d7 commit 302bf20

File tree

2 files changed

+62
-51
lines changed

2 files changed

+62
-51
lines changed

src/controller/tests/TestReadChunking.cpp

+51-44
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ constexpr AttributeId kTestBadAttribute =
7070

7171
constexpr int kListAttributeItems = 5;
7272

73-
using TestReadChunking = chip::Test::AppContext;
74-
7573
//clang-format off
7674
DECLARE_DYNAMIC_ATTRIBUTE_LIST_BEGIN(testClusterAttrs)
7775
DECLARE_DYNAMIC_ATTRIBUTE(0x00000001, INT8U, 1, 0), DECLARE_DYNAMIC_ATTRIBUTE(0x00000002, INT8U, 1, 0),
@@ -456,6 +454,15 @@ void TestMutableReadCallback::OnAttributeData(const app::ConcreteDataAttributePa
456454
// Ignore all other attributes, we don't care above the global attributes.
457455
}
458456

457+
class TestReadChunking : public chip::Test::AppContext
458+
{
459+
protected:
460+
struct Instruction;
461+
void DoTest(TestMutableReadCallback * callback, Instruction instruction);
462+
void DriveIOUntilSubscriptionEstablished(TestMutableReadCallback * callback);
463+
void DriveIOUntilEndOfReport(TestMutableReadCallback * callback);
464+
};
465+
459466
/*
460467
* This validates all the various corner cases encountered during chunking by
461468
* artificially reducing the size of a packet buffer used to encode attribute data
@@ -809,57 +816,59 @@ enum AttrIds
809816
using AttributeWithValue = std::pair<AttributeIdWithEndpointId, uint8_t>;
810817
using AttributesList = std::vector<AttributeIdWithEndpointId>;
811818

812-
struct Instruction
819+
void CheckValues(TestMutableReadCallback * callback, std::vector<AttributeWithValue> expectedValues = {})
820+
{
821+
for (const auto & vals : expectedValues)
822+
{
823+
EXPECT_EQ(callback->mValues[vals.first], vals.second);
824+
}
825+
}
826+
827+
void ExpectSameDataVersions(TestMutableReadCallback * callback, AttributesList attrList)
828+
{
829+
if (attrList.size() == 0)
830+
{
831+
return;
832+
}
833+
DataVersion expectedVersion = callback->mDataVersions[attrList[0]];
834+
for (const auto & attr : attrList)
835+
{
836+
EXPECT_EQ(callback->mDataVersions[attr], expectedVersion);
837+
}
838+
}
839+
840+
}; // namespace TestSetDirtyBetweenChunksUtil
841+
842+
struct TestReadChunking::Instruction
813843
{
814844
// The maximum number of attributes should be iterated in a single report chunk.
815845
uint32_t chunksize;
816846
// A list of functions that will be executed before driving the main loop.
817847
std::vector<std::function<void()>> preworks;
818848
// A list of pair for attributes and their expected values in the report.
819-
std::vector<AttributeWithValue> expectedValues;
849+
std::vector<TestSetDirtyBetweenChunksUtil::AttributeWithValue> expectedValues;
820850
// A list of list of various attributes which should have the same data version in the report.
821-
std::vector<AttributesList> attributesWithSameDataVersion;
851+
std::vector<TestSetDirtyBetweenChunksUtil::AttributesList> attributesWithSameDataVersion;
822852
};
823853

824-
void DriveIOUntilSubscriptionEstablished(TestReadChunking * pContext, TestMutableReadCallback * callback)
854+
void TestReadChunking::DriveIOUntilSubscriptionEstablished(TestMutableReadCallback * callback)
825855
{
826856
callback->mOnReportEnd = false;
827-
pContext->GetIOContext().DriveIOUntil(System::Clock::Seconds16(5), [&]() { return callback->mOnSubscriptionEstablished; });
857+
GetIOContext().DriveIOUntil(System::Clock::Seconds16(5), [&]() { return callback->mOnSubscriptionEstablished; });
828858
EXPECT_TRUE(callback->mOnReportEnd);
829859
EXPECT_TRUE(callback->mOnSubscriptionEstablished);
830860
callback->mActionOn.clear();
831861
}
832862

833-
void DriveIOUntilEndOfReport(TestReadChunking * pContext, TestMutableReadCallback * callback)
863+
void TestReadChunking::DriveIOUntilEndOfReport(TestMutableReadCallback * callback)
834864
{
835865
callback->mOnReportEnd = false;
836-
pContext->GetIOContext().DriveIOUntil(System::Clock::Seconds16(5), [&]() { return callback->mOnReportEnd; });
866+
GetIOContext().DriveIOUntil(System::Clock::Seconds16(5), [&]() { return callback->mOnReportEnd; });
837867
EXPECT_TRUE(callback->mOnReportEnd);
838868
callback->mActionOn.clear();
839869
}
840870

841-
void CheckValues(TestMutableReadCallback * callback, std::vector<AttributeWithValue> expectedValues = {})
842-
{
843-
for (const auto & vals : expectedValues)
844-
{
845-
EXPECT_EQ(callback->mValues[vals.first], vals.second);
846-
}
847-
}
848-
849-
void ExpectSameDataVersions(TestMutableReadCallback * callback, AttributesList attrList)
850-
{
851-
if (attrList.size() == 0)
852-
{
853-
return;
854-
}
855-
DataVersion expectedVersion = callback->mDataVersions[attrList[0]];
856-
for (const auto & attr : attrList)
857-
{
858-
EXPECT_EQ(callback->mDataVersions[attr], expectedVersion);
859-
}
860-
}
861-
862-
void DoTest(TestReadChunking * pContext, TestMutableReadCallback * callback, Instruction instruction)
871+
void TestReadChunking::DoTest(TestMutableReadCallback * callback, Instruction instruction)
863872
{
864873
app::InteractionModelEngine::GetInstance()->GetReportingEngine().SetMaxAttributesPerChunk(instruction.chunksize);
865874

@@ -868,18 +877,16 @@ void DoTest(TestReadChunking * pContext, TestMutableReadCallback * callback, Ins
868877
act();
869878
}
870879

871-
DriveIOUntilEndOfReport(pContext, callback);
880+
DriveIOUntilEndOfReport(callback);
872881

873-
CheckValues(callback, instruction.expectedValues);
882+
TestSetDirtyBetweenChunksUtil::CheckValues(callback, instruction.expectedValues);
874883

875884
for (const auto & attrList : instruction.attributesWithSameDataVersion)
876885
{
877-
ExpectSameDataVersions(callback, attrList);
886+
TestSetDirtyBetweenChunksUtil::ExpectSameDataVersions(callback, attrList);
878887
}
879888
}
880889

881-
}; // namespace TestSetDirtyBetweenChunksUtil
882-
883890
TEST_F(TestReadChunking, TestSetDirtyBetweenChunks)
884891
{
885892
using namespace TestSetDirtyBetweenChunksUtil;
@@ -929,19 +936,19 @@ TEST_F(TestReadChunking, TestSetDirtyBetweenChunks)
929936
// We are expected to miss attributes on kTestEndpointId during initial reports.
930937
ChipLogProgress(DataManagement, "Case 1-1: Set dirty during priming report.");
931938
readCallback.mActionOn[AttrOnEp5<Attr1>] = TouchAttrOp(AttrOnEp1<Attr1>);
932-
DriveIOUntilSubscriptionEstablished(this, &readCallback);
939+
DriveIOUntilSubscriptionEstablished(&readCallback);
933940
CheckValues(&readCallback, { { AttrOnEp1<Attr1>, 1 } });
934941

935942
ChipLogProgress(DataManagement, "Case 1-2: Check for attributes missed last report.");
936-
DoTest(this, &readCallback, Instruction{ .chunksize = 2, .expectedValues = { { AttrOnEp1<Attr1>, 2 } } });
943+
DoTest(&readCallback, Instruction{ .chunksize = 2, .expectedValues = { { AttrOnEp1<Attr1>, 2 } } });
937944
}
938945

939946
// CASE 2 -- Set dirty during chunked report, the attribute is already dirty.
940947
{
941948
ChipLogProgress(DataManagement, "Case 2: Set dirty during chunked report by wildcard path.");
942949
readCallback.mActionOn[AttrOnEp5<Attr2>] = WriteAttrOp(AttrOnEp5<Attr3>, 3);
943950
DoTest(
944-
this, &readCallback,
951+
&readCallback,
945952
Instruction{ .chunksize = 2,
946953
.preworks = { WriteAttrOp(AttrOnEp5<Attr1>, 2), WriteAttrOp(AttrOnEp5<Attr2>, 2),
947954
WriteAttrOp(AttrOnEp5<Attr3>, 2) },
@@ -955,7 +962,7 @@ TEST_F(TestReadChunking, TestSetDirtyBetweenChunks)
955962
"Case 3-1: Set dirty during chunked report by wildcard path -- new dirty attribute.");
956963
readCallback.mActionOn[AttrOnEp5<Attr2>] = WriteAttrOp(AttrOnEp5<Attr3>, 4);
957964
DoTest(
958-
this, &readCallback,
965+
&readCallback,
959966
Instruction{ .chunksize = 1,
960967
.preworks = { WriteAttrOp(AttrOnEp5<Attr1>, 4), WriteAttrOp(AttrOnEp5<Attr2>, 4) },
961968
.expectedValues = { { AttrOnEp5<Attr1>, 4 }, { AttrOnEp5<Attr2>, 4 }, { AttrOnEp5<Attr3>, 4 } },
@@ -966,7 +973,7 @@ TEST_F(TestReadChunking, TestSetDirtyBetweenChunks)
966973
app::InteractionModelEngine::GetInstance()->GetReportingEngine().SetMaxAttributesPerChunk(1);
967974
readCallback.mActionOn[AttrOnEp5<Attr2>] = WriteAttrOp(AttrOnEp5<Attr1>, 5);
968975
DoTest(
969-
this, &readCallback,
976+
&readCallback,
970977
Instruction{ .chunksize = 1,
971978
.preworks = { WriteAttrOp(AttrOnEp5<Attr2>, 5), WriteAttrOp(AttrOnEp5<Attr3>, 5) },
972979
.expectedValues = { { AttrOnEp5<Attr1>, 5 }, { AttrOnEp5<Attr2>, 5 }, { AttrOnEp5<Attr3>, 5 } },
@@ -1001,21 +1008,21 @@ TEST_F(TestReadChunking, TestSetDirtyBetweenChunks)
10011008

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

1004-
DriveIOUntilSubscriptionEstablished(this, &readCallback);
1011+
DriveIOUntilSubscriptionEstablished(&readCallback);
10051012

10061013
// Note, although the two attributes comes from the same cluster, they are generated by different interested paths.
10071014
// In this case, we won't reset the path iterator.
10081015
ChipLogProgress(DataManagement, "Case 1-1: Test set dirty during reports generated by concrete paths.");
10091016
readCallback.mActionOn[AttrOnEp5<Attr2>] = WriteAttrOp(AttrOnEp5<Attr3>, 4);
1010-
DoTest(this, &readCallback,
1017+
DoTest(&readCallback,
10111018
Instruction{ .chunksize = 1,
10121019
.preworks = { WriteAttrOp(AttrOnEp5<Attr1>, 3), WriteAttrOp(AttrOnEp5<Attr2>, 3),
10131020
WriteAttrOp(AttrOnEp5<Attr3>, 3) },
10141021
.expectedValues = { { AttrOnEp5<Attr1>, 3 }, { AttrOnEp5<Attr2>, 3 }, { AttrOnEp5<Attr3>, 3 } } });
10151022

10161023
// The attribute failed to catch last report will be picked by this report.
10171024
ChipLogProgress(DataManagement, "Case 1-2: Check for attributes missed last report.");
1018-
DoTest(this, &readCallback, { .chunksize = 1, .expectedValues = { { AttrOnEp5<Attr3>, 4 } } });
1025+
DoTest(&readCallback, { .chunksize = 1, .expectedValues = { { AttrOnEp5<Attr3>, 4 } } });
10191026
}
10201027
}
10211028

src/controller/tests/data_model/TestRead.cpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ class TestRead : public chip::Test::AppContext, public app::ReadHandler::Applica
294294
bool mEmitSubscriptionError = false;
295295
int32_t mNumActiveSubscriptions = 0;
296296
bool mAlterSubscriptionIntervals = false;
297+
298+
protected:
299+
struct TestReadHandler_ParallelReads_TestCase_Parameters;
300+
void TestReadHandler_ParallelReads_TestCase(const TestReadHandler_ParallelReads_TestCase_Parameters & params,
301+
std::function<void()> body);
297302
};
298303

299304
uint16_t TestRead::mMaxInterval = 66;
@@ -3629,16 +3634,15 @@ TEST_F(TestRead, TestReadHandler_KillOldestSubscriptions)
36293634
app::InteractionModelEngine::GetInstance()->SetPathPoolCapacityForSubscriptions(-1);
36303635
}
36313636

3632-
struct TestReadHandler_ParallelReads_TestCase_Parameters
3637+
struct TestRead::TestReadHandler_ParallelReads_TestCase_Parameters
36333638
{
36343639
int ReadHandlerCapacity = -1;
36353640
int PathPoolCapacity = -1;
36363641
int MaxFabrics = -1;
36373642
};
36383643

3639-
static void TestReadHandler_ParallelReads_TestCase(TestRead * apContext,
3640-
const TestReadHandler_ParallelReads_TestCase_Parameters & params,
3641-
std::function<void()> body)
3644+
void TestRead::TestReadHandler_ParallelReads_TestCase(const TestReadHandler_ParallelReads_TestCase_Parameters & params,
3645+
std::function<void()> body)
36423646
{
36433647
app::InteractionModelEngine::GetInstance()->SetForceHandlerQuota(true);
36443648
app::InteractionModelEngine::GetInstance()->SetHandlerCapacityForReads(params.ReadHandlerCapacity);
@@ -3649,10 +3653,10 @@ static void TestReadHandler_ParallelReads_TestCase(TestRead * apContext,
36493653

36503654
// Clean up
36513655
app::InteractionModelEngine::GetInstance()->ShutdownActiveReads();
3652-
apContext->DrainAndServiceIO();
3656+
DrainAndServiceIO();
36533657

36543658
// Sanity check
3655-
EXPECT_EQ(apContext->GetExchangeManager().GetNumActiveExchanges(), 0u);
3659+
EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 0u);
36563660

36573661
app::InteractionModelEngine::GetInstance()->SetForceHandlerQuota(false);
36583662
app::InteractionModelEngine::GetInstance()->SetHandlerCapacityForReads(-1);
@@ -3673,7 +3677,7 @@ TEST_F(TestRead, TestReadHandler_ParallelReads)
36733677
app::InteractionModelEngine::GetInstance()->RegisterReadHandlerAppCallback(this);
36743678

36753679
auto TestCase = [&](const TestReadHandler_ParallelReads_TestCase_Parameters & params, std::function<void()> body) {
3676-
TestReadHandler_ParallelReads_TestCase(this, params, body);
3680+
TestReadHandler_ParallelReads_TestCase(params, body);
36773681
};
36783682

36793683
// Case 1.1: 2 reads used up the path pool (but not the ReadHandler pool), and one incoming oversized read request =>

0 commit comments

Comments
 (0)