Skip to content

Commit b870e3a

Browse files
authored
Remove TestServerClusterContext::Create (project-chip#42000)
This encourages odd patterns of creating temporary variables, which are generally not needed for server cluster testing (only registry testing needs something like this and they can create temporaries)
1 parent 55930f3 commit b870e3a

File tree

9 files changed

+35
-54
lines changed

9 files changed

+35
-54
lines changed

src/app/clusters/basic-information/tests/TestBasicInformationReadWrite.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,13 @@ struct TestBasicInformationReadWrite : public ::testing::Test
175175
chip::Platform::MemoryShutdown();
176176
}
177177

178-
TestBasicInformationReadWrite() : testContext(), context(testContext.Create()) {}
178+
TestBasicInformationReadWrite() {}
179179

180-
void SetUp() override { ASSERT_EQ(basicInformationClusterInstance.Startup(context), CHIP_NO_ERROR); }
180+
void SetUp() override { ASSERT_EQ(basicInformationClusterInstance.Startup(testContext.Get()), CHIP_NO_ERROR); }
181181

182182
void TearDown() override { basicInformationClusterInstance.Shutdown(); }
183183

184184
chip::Test::TestServerClusterContext testContext;
185-
ServerClusterContext context;
186185
static BasicInformationCluster & basicInformationClusterInstance;
187186
static chip::Test::ClusterTester tester;
188187
};
@@ -196,7 +195,7 @@ TEST_F(TestBasicInformationReadWrite, TestNodeLabelLoadAndSave)
196195
CharSpan oldLabelSpan = "Old Label"_span;
197196

198197
// Initialize AttributePersistence with the provider from *this test's* context
199-
AttributePersistence persistence(context.attributeStorage);
198+
AttributePersistence persistence(testContext.AttributePersistenceProvider());
200199

201200
Storage::String<32> labelStorage;
202201
ASSERT_EQ(labelStorage.SetContent(oldLabelSpan), true); // ensure it fits
@@ -206,7 +205,7 @@ TEST_F(TestBasicInformationReadWrite, TestNodeLabelLoadAndSave)
206205
// 2. WHEN: The BasicInformationCluster starts up.
207206
// We must shut down the one from SetUp and re-start it to force a load.
208207
basicInformationClusterInstance.Shutdown();
209-
ASSERT_EQ(basicInformationClusterInstance.Startup(context), CHIP_NO_ERROR);
208+
ASSERT_EQ(basicInformationClusterInstance.Startup(testContext.Get()), CHIP_NO_ERROR);
210209

211210
// 3. THEN: The cluster should have loaded "Old Label" into its memory.
212211
char readBuffer[32];

src/app/clusters/boolean-state-server/tests/TestBooleanStateCluster.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ struct TestBooleanStateCluster : public ::testing::Test
4040

4141
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
4242

43-
void SetUp() override { ASSERT_EQ(booleanState.Startup(context), CHIP_NO_ERROR); }
43+
void SetUp() override { ASSERT_EQ(booleanState.Startup(testContext.Get()), CHIP_NO_ERROR); }
4444

4545
void TearDown() override { booleanState.Shutdown(); }
4646

47-
TestBooleanStateCluster() : context(testContext.Create()), booleanState(kRootEndpointId) {}
47+
TestBooleanStateCluster() : booleanState(kRootEndpointId) {}
4848

4949
chip::Test::TestServerClusterContext testContext;
50-
ServerClusterContext context;
5150
BooleanStateCluster booleanState;
5251
};
5352

src/app/clusters/fixed-label-server/tests/TestFixedLabelCluster.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ struct TestFixedLabelCluster : public ::testing::Test
4040

4141
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
4242

43-
void SetUp() override { ASSERT_EQ(fixedLabel.Startup(context), CHIP_NO_ERROR); }
43+
void SetUp() override { ASSERT_EQ(fixedLabel.Startup(testContext.Get()), CHIP_NO_ERROR); }
4444

4545
void TearDown() override { fixedLabel.Shutdown(); }
4646

47-
TestFixedLabelCluster() : context(testContext.Create()), fixedLabel(kRootEndpointId) {}
47+
TestFixedLabelCluster() : fixedLabel(kRootEndpointId) {}
4848

4949
chip::Test::TestServerClusterContext testContext;
50-
ServerClusterContext context;
5150
FixedLabelCluster fixedLabel;
5251
};
5352

src/app/clusters/soil-measurement-server/tests/TestSoilMeasurementCluster.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,13 @@ struct TestSoilMeasurementCluster : public ::testing::Test
6767

6868
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
6969

70-
void SetUp() override { ASSERT_EQ(soilMeasurement.Startup(context), CHIP_NO_ERROR); }
70+
void SetUp() override { ASSERT_EQ(soilMeasurement.Startup(testContext.Get()), CHIP_NO_ERROR); }
7171

7272
void TearDown() override { soilMeasurement.Shutdown(); }
7373

74-
TestSoilMeasurementCluster() :
75-
context(testContext.Create()), soilMeasurement(kEndpointWithSoilMeasurement, kDefaultSoilMoistureMeasurementLimits)
76-
{}
74+
TestSoilMeasurementCluster() : soilMeasurement(kEndpointWithSoilMeasurement, kDefaultSoilMoistureMeasurementLimits) {}
7775

7876
chip::Test::TestServerClusterContext testContext;
79-
ServerClusterContext context;
8077
SoilMeasurementClusterLocal soilMeasurement;
8178
};
8279

src/app/clusters/time-synchronization-server/tests/TestTimeSynchronizationCluster.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ struct TestTimeSynchronizationCluster : public ::testing::Test
4141

4242
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
4343

44-
TestTimeSynchronizationCluster() : context(testContext.Create()) {}
44+
TestTimeSynchronizationCluster() {}
4545

4646
chip::Test::TestServerClusterContext testContext;
47-
ServerClusterContext context;
4847
TimeSynchronization::DefaultTimeSyncDelegate delegate;
4948
};
5049

@@ -57,7 +56,7 @@ TEST_F(TestTimeSynchronizationCluster, AttributeTest)
5756
TimeSynchronizationCluster timeSynchronization(kRootEndpointId, features,
5857
TimeSynchronizationCluster::OptionalAttributeSet(),
5958
TimeSynchronizationCluster::StartupConfiguration{ .delegate = &delegate });
60-
ASSERT_EQ(timeSynchronization.Startup(context), CHIP_NO_ERROR);
59+
ASSERT_EQ(timeSynchronization.Startup(testContext.Get()), CHIP_NO_ERROR);
6160

6261
ReadOnlyBufferBuilder<DataModel::AttributeEntry> attributes;
6362
ASSERT_EQ(timeSynchronization.Attributes(ConcreteClusterPath(kRootEndpointId, TimeSynchronization::Id), attributes),
@@ -78,7 +77,7 @@ TEST_F(TestTimeSynchronizationCluster, AttributeTest)
7877
const BitFlags<Feature> features{ 0U };
7978
TimeSynchronizationCluster timeSynchronization(kRootEndpointId, features, optionalAttributeSet,
8079
TimeSynchronizationCluster::StartupConfiguration{ .delegate = &delegate });
81-
ASSERT_EQ(timeSynchronization.Startup(context), CHIP_NO_ERROR);
80+
ASSERT_EQ(timeSynchronization.Startup(testContext.Get()), CHIP_NO_ERROR);
8281

8382
ReadOnlyBufferBuilder<DataModel::AttributeEntry> attributes;
8483
ASSERT_EQ(timeSynchronization.Attributes(ConcreteClusterPath(kRootEndpointId, TimeSynchronization::Id), attributes),
@@ -97,7 +96,7 @@ TEST_F(TestTimeSynchronizationCluster, AttributeTest)
9796
TimeSynchronizationCluster timeSynchronization(kRootEndpointId, features,
9897
TimeSynchronizationCluster::OptionalAttributeSet(),
9998
TimeSynchronizationCluster::StartupConfiguration{ .delegate = &delegate });
100-
ASSERT_EQ(timeSynchronization.Startup(context), CHIP_NO_ERROR);
99+
ASSERT_EQ(timeSynchronization.Startup(testContext.Get()), CHIP_NO_ERROR);
101100

102101
ReadOnlyBufferBuilder<DataModel::AttributeEntry> attributes;
103102
ASSERT_EQ(timeSynchronization.Attributes(ConcreteClusterPath(kRootEndpointId, TimeSynchronization::Id), attributes),
@@ -120,7 +119,7 @@ TEST_F(TestTimeSynchronizationCluster, AttributeTest)
120119
TimeSynchronizationCluster timeSynchronization(kRootEndpointId, features,
121120
TimeSynchronizationCluster::OptionalAttributeSet(),
122121
TimeSynchronizationCluster::StartupConfiguration{ .delegate = &delegate });
123-
ASSERT_EQ(timeSynchronization.Startup(context), CHIP_NO_ERROR);
122+
ASSERT_EQ(timeSynchronization.Startup(testContext.Get()), CHIP_NO_ERROR);
124123

125124
ReadOnlyBufferBuilder<DataModel::AttributeEntry> attributes;
126125
ASSERT_EQ(timeSynchronization.Attributes(ConcreteClusterPath(kRootEndpointId, TimeSynchronization::Id), attributes),
@@ -144,7 +143,7 @@ TEST_F(TestTimeSynchronizationCluster, AttributeTest)
144143
TimeSynchronizationCluster timeSynchronization(kRootEndpointId, features,
145144
TimeSynchronizationCluster::OptionalAttributeSet(),
146145
TimeSynchronizationCluster::StartupConfiguration{ .delegate = &delegate });
147-
ASSERT_EQ(timeSynchronization.Startup(context), CHIP_NO_ERROR);
146+
ASSERT_EQ(timeSynchronization.Startup(testContext.Get()), CHIP_NO_ERROR);
148147

149148
ReadOnlyBufferBuilder<DataModel::AttributeEntry> attributes;
150149
ASSERT_EQ(timeSynchronization.Attributes(ConcreteClusterPath(kRootEndpointId, TimeSynchronization::Id), attributes),
@@ -167,7 +166,7 @@ TEST_F(TestTimeSynchronizationCluster, AttributeTest)
167166
TimeSynchronizationCluster timeSynchronization(kRootEndpointId, features,
168167
TimeSynchronizationCluster::OptionalAttributeSet(),
169168
TimeSynchronizationCluster::StartupConfiguration{ .delegate = &delegate });
170-
ASSERT_EQ(timeSynchronization.Startup(context), CHIP_NO_ERROR);
169+
ASSERT_EQ(timeSynchronization.Startup(testContext.Get()), CHIP_NO_ERROR);
171170

172171
ReadOnlyBufferBuilder<DataModel::AttributeEntry> attributes;
173172
ASSERT_EQ(timeSynchronization.Attributes(ConcreteClusterPath(kRootEndpointId, TimeSynchronization::Id), attributes),
@@ -203,7 +202,7 @@ TEST_F(TestTimeSynchronizationCluster, ReadAttributeTest)
203202
TimeSynchronizationCluster timeSynchronization(kRootEndpointId, features,
204203
TimeSynchronizationCluster::OptionalAttributeSet(),
205204
TimeSynchronizationCluster::StartupConfiguration{ .delegate = &delegate });
206-
ASSERT_EQ(timeSynchronization.Startup(context), CHIP_NO_ERROR);
205+
ASSERT_EQ(timeSynchronization.Startup(testContext.Get()), CHIP_NO_ERROR);
207206

208207
ClusterTester tester(timeSynchronization);
209208

@@ -228,7 +227,7 @@ TEST_F(TestTimeSynchronizationCluster, ReadAttributeTest)
228227
const BitFlags<Feature> features{ 0U };
229228
TimeSynchronizationCluster timeSynchronization(kRootEndpointId, features, optionalAttributeSet,
230229
TimeSynchronizationCluster::StartupConfiguration{ .delegate = &delegate });
231-
ASSERT_EQ(timeSynchronization.Startup(context), CHIP_NO_ERROR);
230+
ASSERT_EQ(timeSynchronization.Startup(testContext.Get()), CHIP_NO_ERROR);
232231

233232
ClusterTester tester(timeSynchronization);
234233

@@ -255,7 +254,7 @@ TEST_F(TestTimeSynchronizationCluster, ReadAttributeTest)
255254
TimeSynchronizationCluster timeSynchronization(kRootEndpointId, features,
256255
TimeSynchronizationCluster::OptionalAttributeSet(),
257256
TimeSynchronizationCluster::StartupConfiguration{ .delegate = &delegate });
258-
ASSERT_EQ(timeSynchronization.Startup(context), CHIP_NO_ERROR);
257+
ASSERT_EQ(timeSynchronization.Startup(testContext.Get()), CHIP_NO_ERROR);
259258

260259
ClusterTester tester(timeSynchronization);
261260

@@ -282,7 +281,7 @@ TEST_F(TestTimeSynchronizationCluster, ReadAttributeTest)
282281
TimeSynchronizationCluster timeSynchronization(kRootEndpointId, features,
283282
TimeSynchronizationCluster::OptionalAttributeSet(),
284283
TimeSynchronizationCluster::StartupConfiguration{ .delegate = &delegate });
285-
ASSERT_EQ(timeSynchronization.Startup(context), CHIP_NO_ERROR);
284+
ASSERT_EQ(timeSynchronization.Startup(testContext.Get()), CHIP_NO_ERROR);
286285

287286
ClusterTester tester(timeSynchronization);
288287

@@ -312,7 +311,7 @@ TEST_F(TestTimeSynchronizationCluster, ReadAttributeTest)
312311
TimeSynchronizationCluster timeSynchronization(kRootEndpointId, features,
313312
TimeSynchronizationCluster::OptionalAttributeSet(),
314313
TimeSynchronizationCluster::StartupConfiguration{ .delegate = &delegate });
315-
ASSERT_EQ(timeSynchronization.Startup(context), CHIP_NO_ERROR);
314+
ASSERT_EQ(timeSynchronization.Startup(testContext.Get()), CHIP_NO_ERROR);
316315

317316
ClusterTester tester(timeSynchronization);
318317

@@ -339,7 +338,7 @@ TEST_F(TestTimeSynchronizationCluster, ReadAttributeTest)
339338
TimeSynchronizationCluster timeSynchronization(kRootEndpointId, features,
340339
TimeSynchronizationCluster::OptionalAttributeSet(),
341340
TimeSynchronizationCluster::StartupConfiguration{ .delegate = &delegate });
342-
ASSERT_EQ(timeSynchronization.Startup(context), CHIP_NO_ERROR);
341+
ASSERT_EQ(timeSynchronization.Startup(testContext.Get()), CHIP_NO_ERROR);
343342

344343
ClusterTester tester(timeSynchronization);
345344

src/app/clusters/user-label-server/tests/TestUserLabelCluster.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct TestUserLabelCluster : public ::testing::Test
7070
void SetUp() override
7171
{
7272
DeviceLayer::SetDeviceInfoProvider(&mDeviceInfoProvider);
73-
ASSERT_EQ(userLabel.Startup(context), CHIP_NO_ERROR);
73+
ASSERT_EQ(userLabel.Startup(testContext.Get()), CHIP_NO_ERROR);
7474
}
7575

7676
void TearDown() override
@@ -79,10 +79,9 @@ struct TestUserLabelCluster : public ::testing::Test
7979
DeviceLayer::SetDeviceInfoProvider(nullptr);
8080
}
8181

82-
TestUserLabelCluster() : context(testContext.Create()), userLabel(kRootEndpointId) {}
82+
TestUserLabelCluster() : userLabel(kRootEndpointId) {}
8383

8484
chip::Test::TestServerClusterContext testContext;
85-
ServerClusterContext context;
8685
UserLabelCluster userLabel;
8786
MockDeviceInfoProvider mDeviceInfoProvider;
8887
};

src/app/server-cluster/testing/TestServerClusterContext.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,6 @@ class TestServerClusterContext
6666
/// Get a stable pointer to the underlying context
6767
app::ServerClusterContext & Get() { return mContext; }
6868

69-
/// Create a new context bound to this test context
70-
app::ServerClusterContext Create()
71-
{
72-
return {
73-
.provider = mTestProvider,
74-
.storage = mTestStorage,
75-
.attributeStorage = mDefaultAttributePersistenceProvider,
76-
.interactionContext = mTestContext,
77-
};
78-
};
79-
8069
LogOnlyEvents & EventsGenerator() { return mTestEventsGenerator; }
8170
TestProviderChangeListener & ChangeListener() { return mTestDataModelChangeListener; }
8271
TestPersistentStorageDelegate & StorageDelegate() { return mTestStorage; }

src/app/server-cluster/tests/TestServerClusterInterfaceRegistry.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ TEST_F(TestServerClusterInterfaceRegistry, Context)
239239

240240
// set up the registry
241241
TestServerClusterContext context;
242-
EXPECT_EQ(registry.SetContext(context.Create()), CHIP_NO_ERROR);
242+
EXPECT_EQ(registry.SetContext(ServerClusterContext{ context.Get() }), CHIP_NO_ERROR);
243243

244244
EXPECT_TRUE(cluster1.HasContext());
245245
EXPECT_FALSE(cluster2.HasContext());
@@ -255,7 +255,7 @@ TEST_F(TestServerClusterInterfaceRegistry, Context)
255255
EXPECT_FALSE(cluster2.HasContext());
256256
EXPECT_FALSE(cluster3.HasContext());
257257

258-
EXPECT_EQ(registry.SetContext(context.Create()), CHIP_NO_ERROR);
258+
EXPECT_EQ(registry.SetContext(ServerClusterContext{ context.Get() }), CHIP_NO_ERROR);
259259
EXPECT_TRUE(cluster1.HasContext());
260260
EXPECT_TRUE(cluster2.HasContext());
261261
EXPECT_FALSE(cluster3.HasContext());
@@ -270,15 +270,15 @@ TEST_F(TestServerClusterInterfaceRegistry, Context)
270270
EXPECT_TRUE(cluster3.HasContext());
271271

272272
// re-setting context works
273-
EXPECT_EQ(registry.SetContext(context.Create()), CHIP_NO_ERROR);
273+
EXPECT_EQ(registry.SetContext(ServerClusterContext{ context.Get() }), CHIP_NO_ERROR);
274274
EXPECT_TRUE(cluster1.HasContext());
275275
EXPECT_FALSE(cluster2.HasContext());
276276
EXPECT_TRUE(cluster3.HasContext());
277277

278278
// also not valid, but different
279279
TestServerClusterContext otherContext;
280280

281-
EXPECT_EQ(registry.SetContext(otherContext.Create()), CHIP_NO_ERROR);
281+
EXPECT_EQ(registry.SetContext(ServerClusterContext{ otherContext.Get() }), CHIP_NO_ERROR);
282282
EXPECT_TRUE(cluster1.HasContext());
283283
EXPECT_FALSE(cluster2.HasContext());
284284
EXPECT_TRUE(cluster3.HasContext());
@@ -373,7 +373,7 @@ TEST_F(TestServerClusterInterfaceRegistry, StartupShutdownWithoutContext)
373373

374374
// the clusters are explicitly set to fail startup, so SetContext returns an error.
375375
// TODO: is this sane? Register() with a startup failure does NOT return a failure.
376-
EXPECT_EQ(registry.SetContext(context.Create()), CHIP_ERROR_HAD_FAILURES);
376+
EXPECT_EQ(registry.SetContext(ServerClusterContext{ context.Get() }), CHIP_ERROR_HAD_FAILURES);
377377

378378
// Startup called after registration, with failure that is NOT reported (only logged)
379379
EXPECT_EQ(cluster3.Cluster().GetStartupCallCount(), 0u);

src/app/server-cluster/tests/TestSingleEndpointServerClusterRegistry.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ TEST_F(TestSingleEndpointServerClusterRegistry, Context)
373373

374374
// set up the registry
375375
TestServerClusterContext context;
376-
EXPECT_EQ(registry.SetContext(context.Create()), CHIP_NO_ERROR);
376+
EXPECT_EQ(registry.SetContext(ServerClusterContext{ context.Get() }), CHIP_NO_ERROR);
377377

378378
EXPECT_TRUE(cluster1.HasContext());
379379
EXPECT_FALSE(cluster2.HasContext());
@@ -389,7 +389,7 @@ TEST_F(TestSingleEndpointServerClusterRegistry, Context)
389389
EXPECT_FALSE(cluster2.HasContext());
390390
EXPECT_FALSE(cluster3.HasContext());
391391

392-
EXPECT_EQ(registry.SetContext(context.Create()), CHIP_NO_ERROR);
392+
EXPECT_EQ(registry.SetContext(ServerClusterContext{ context.Get() }), CHIP_NO_ERROR);
393393
EXPECT_TRUE(cluster1.HasContext());
394394
EXPECT_TRUE(cluster2.HasContext());
395395
EXPECT_FALSE(cluster3.HasContext());
@@ -404,15 +404,15 @@ TEST_F(TestSingleEndpointServerClusterRegistry, Context)
404404
EXPECT_TRUE(cluster3.HasContext());
405405

406406
// re-setting context works
407-
EXPECT_EQ(registry.SetContext(context.Create()), CHIP_NO_ERROR);
407+
EXPECT_EQ(registry.SetContext(ServerClusterContext{ context.Get() }), CHIP_NO_ERROR);
408408
EXPECT_TRUE(cluster1.HasContext());
409409
EXPECT_FALSE(cluster2.HasContext());
410410
EXPECT_TRUE(cluster3.HasContext());
411411

412412
// also not valid, but different
413413
TestServerClusterContext otherContext;
414414

415-
EXPECT_EQ(registry.SetContext(otherContext.Create()), CHIP_NO_ERROR);
415+
EXPECT_EQ(registry.SetContext(ServerClusterContext{ otherContext.Get() }), CHIP_NO_ERROR);
416416
EXPECT_TRUE(cluster1.HasContext());
417417
EXPECT_FALSE(cluster2.HasContext());
418418
EXPECT_TRUE(cluster3.HasContext());
@@ -542,7 +542,7 @@ TEST_F(TestSingleEndpointServerClusterRegistry, StartupErrors)
542542
EXPECT_FALSE(cluster2.HasContext());
543543

544544
TestServerClusterContext context;
545-
EXPECT_EQ(registry.SetContext(context.Create()), CHIP_ERROR_HAD_FAILURES);
545+
EXPECT_EQ(registry.SetContext(ServerClusterContext{ context.Get() }), CHIP_ERROR_HAD_FAILURES);
546546
EXPECT_TRUE(cluster1.HasContext());
547547
EXPECT_FALSE(cluster2.HasContext());
548548

0 commit comments

Comments
 (0)