Skip to content

Commit 08bb265

Browse files
committed
Amended MTRPerControllerStorageTests/testSubscriptionPool to include BaseDevice testing
1 parent e479b8b commit 08bb265

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m

+47-1
Original file line numberDiff line numberDiff line change
@@ -2269,6 +2269,7 @@ - (void)doTestSubscriptionPoolWithSize:(NSInteger)subscriptionPoolSize deviceOnb
22692269
XCTAssertNotNil(factory);
22702270

22712271
__auto_type queue = dispatch_get_main_queue();
2272+
// __auto_type queue = dispatch_queue_create("test.subscription.pool", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
22722273

22732274
__auto_type * rootKeys = [[MTRTestKeys alloc] init];
22742275
XCTAssertNotNil(rootKeys);
@@ -2307,6 +2308,24 @@ - (void)doTestSubscriptionPoolWithSize:(NSInteger)subscriptionPoolSize deviceOnb
23072308
[self commissionWithController:controller newNodeID:deviceID onboardingPayload:deviceOnboardingPayloads[deviceID]];
23082309
}
23092310

2311+
// Shutdown and restart, to reset all existing sessions, so that the subscriptions and base device usage start after
2312+
[controller shutdown];
2313+
XCTAssertFalse([controller isRunning]);
2314+
2315+
controller = [self startControllerWithRootKeys:rootKeys
2316+
operationalKeys:operationalKeys
2317+
fabricID:fabricID
2318+
nodeID:nodeID
2319+
storage:storageDelegate
2320+
error:&error
2321+
certificateIssuer:&certificateIssuer
2322+
concurrentSubscriptionPoolSize:subscriptionPoolSize];
2323+
XCTAssertNil(error);
2324+
XCTAssertNotNil(controller);
2325+
XCTAssertTrue([controller isRunning]);
2326+
2327+
XCTAssertEqualObjects(controller.controllerNodeID, nodeID);
2328+
23102329
// Set up expectations and delegates
23112330

23122331
NSDictionary<NSNumber *, XCTestExpectation *> * subscriptionExpectations = @{
@@ -2329,6 +2348,7 @@ - (void)doTestSubscriptionPoolWithSize:(NSInteger)subscriptionPoolSize deviceOnb
23292348
__block os_unfair_lock counterLock = OS_UNFAIR_LOCK_INIT;
23302349
__block NSUInteger subscriptionRunningCount = 0;
23312350
__block NSUInteger subscriptionDequeueCount = 0;
2351+
__block BOOL baseDeviceReadCompleted = NO;
23322352

23332353
for (NSNumber * deviceID in orderedDeviceIDs) {
23342354
MTRDeviceTestDelegate * delegate = deviceDelegates[deviceID];
@@ -2347,6 +2367,14 @@ - (void)doTestSubscriptionPoolWithSize:(NSInteger)subscriptionPoolSize deviceOnb
23472367
// Stop counting subscribing right before calling work item completion
23482368
os_unfair_lock_lock(&counterLock);
23492369
subscriptionRunningCount--;
2370+
2371+
// Given the base device read is happening on the 5th device, at the completion
2372+
// time of the first [pool size] subscriptions, the BaseDevice's request to
2373+
// read can't have completed, as it should be gated on its call to the
2374+
// MTRDeviceController's getSessionForNode: call.
2375+
if (subscriptionDequeueCount <= (orderedDeviceIDs.count - subscriptionPoolSize)) {
2376+
XCTAssertFalse(baseDeviceReadCompleted);
2377+
}
23502378
os_unfair_lock_unlock(&counterLock);
23512379
};
23522380
__weak __auto_type weakDelegate = delegate;
@@ -2363,8 +2391,26 @@ - (void)doTestSubscriptionPoolWithSize:(NSInteger)subscriptionPoolSize deviceOnb
23632391
[device setDelegate:deviceDelegates[deviceID] queue:queue];
23642392
}
23652393

2394+
// Create the base device to attempt to read from the 5th device
2395+
__auto_type * baseDeviceReadExpectation = [self expectationWithDescription:@"BaseDevice read"];
2396+
// Dispatch async to get around XCTest, so that this runs after the above devices queue their subscriptions
2397+
dispatch_async(queue, ^{
2398+
__auto_type * baseDevice = [MTRBaseDevice deviceWithNodeID:@(105) controller:controller];
2399+
__auto_type * onOffCluster = [[MTRBaseClusterOnOff alloc] initWithDevice:baseDevice endpointID:@(1) queue:queue];
2400+
[onOffCluster readAttributeOnOffWithCompletion:^(NSNumber * value, NSError * _Nullable error) {
2401+
XCTAssertNil(error);
2402+
// We expect the device to be off.
2403+
XCTAssertEqualObjects(value, @(0));
2404+
[baseDeviceReadExpectation fulfill];
2405+
os_unfair_lock_lock(&counterLock);
2406+
baseDeviceReadCompleted = YES;
2407+
os_unfair_lock_unlock(&counterLock);
2408+
}];
2409+
});
2410+
23662411
// Make the wait time depend on pool size and device count (can expand number of devices in the future)
2367-
[self waitForExpectations:subscriptionExpectations.allValues timeout:(kSubscriptionPoolBaseTimeoutInSeconds * orderedDeviceIDs.count / subscriptionPoolSize)];
2412+
NSArray * expectationsToWait = [subscriptionExpectations.allValues arrayByAddingObject:baseDeviceReadExpectation];
2413+
[self waitForExpectations:expectationsToWait timeout:(kSubscriptionPoolBaseTimeoutInSeconds * orderedDeviceIDs.count / subscriptionPoolSize)];
23682414

23692415
XCTAssertEqual(subscriptionDequeueCount, orderedDeviceIDs.count);
23702416

0 commit comments

Comments
 (0)