Skip to content

Commit

Permalink
Added unit test and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jtung-apple committed Mar 13, 2024
1 parent 6eb6869 commit e444ab9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.mm
Original file line number Diff line number Diff line change
Expand Up @@ -556,30 +556,28 @@ - (void)_pruneEmptyStoredAttributesBranches

if (attributeIndex.count != attributeIndexCopy.count) {
BOOL success;
if (!success) {
if (attributeIndexCopy.count) {
success = [self _storeAttributeIndex:attributeIndexCopy forNodeID:nodeID endpointID:endpointID clusterID:clusterID];
} else {
[clusterIndexCopy removeObject:clusterID];
success = [self _deleteAttributeIndexForNodeID:nodeID endpointID:endpointID clusterID:clusterID];
}
storeFailures++;
MTR_LOG_INFO("Store failed in _pruneEmptyStoredAttributesBranches for attributeIndex @ 0x%016llX:0x%04X:0x%08lX", nodeID.unsignedLongLongValue, endpointID.unsignedShortValue, clusterID.unsignedLongValue);
if (attributeIndexCopy.count) {
success = [self _storeAttributeIndex:attributeIndexCopy forNodeID:nodeID endpointID:endpointID clusterID:clusterID];
} else {
[clusterIndexCopy removeObject:clusterID];
success = [self _deleteAttributeIndexForNodeID:nodeID endpointID:endpointID clusterID:clusterID];
}
storeFailures++;
MTR_LOG_INFO("Store failed in _pruneEmptyStoredAttributesBranches for attributeIndex (%lu) @ 0x%016llX:0x%04X:0x%08lX", static_cast<unsigned long>(attributeIndexCopy.count), nodeID.unsignedLongLongValue, endpointID.unsignedShortValue, clusterID.unsignedLongValue);
}
}

if (clusterIndex.count != clusterIndexCopy.count) {
BOOL success;
if (clusterIndex.count) {
if (clusterIndexCopy.count) {
success = [self _storeClusterIndex:clusterIndexCopy forNodeID:nodeID endpointID:endpointID];
} else {
[endpointIndexCopy removeObject:endpointID];
success = [self _deleteClusterIndexForNodeID:nodeID endpointID:endpointID];
}
if (!success) {
storeFailures++;
MTR_LOG_INFO("Store failed in _pruneEmptyStoredAttributesBranches for clusterIndex @ 0x%016llX:0x%04X", nodeID.unsignedLongLongValue, endpointID.unsignedShortValue);
MTR_LOG_INFO("Store failed in _pruneEmptyStoredAttributesBranches for clusterIndex (%lu) @ 0x%016llX:0x%04X", static_cast<unsigned long>(clusterIndexCopy.count), nodeID.unsignedLongLongValue, endpointID.unsignedShortValue);
}
}
}
Expand All @@ -594,21 +592,21 @@ - (void)_pruneEmptyStoredAttributesBranches
}
if (!success) {
storeFailures++;
MTR_LOG_INFO("Store failed in _pruneEmptyStoredAttributesBranches for endpointIndex @ 0x%016llX", nodeID.unsignedLongLongValue);
MTR_LOG_INFO("Store failed in _pruneEmptyStoredAttributesBranches for endpointIndex (%lu) @ 0x%016llX", static_cast<unsigned long>(endpointIndexCopy.count), nodeID.unsignedLongLongValue);
}
}
}

if (nodeIndex.count != nodeIndexCopy.count) {
BOOL success;
if (!nodeIndex.count) {
if (nodeIndexCopy.count) {
success = [self _storeNodeIndex:nodeIndexCopy];
} else {
success = [self _deleteNodeIndex];
}
if (!success) {
storeFailures++;
MTR_LOG_INFO("Store failed in _pruneEmptyStoredAttributesBranches for nodeIndex");
MTR_LOG_INFO("Store failed in _pruneEmptyStoredAttributesBranches for nodeIndex (%lu)", static_cast<unsigned long>(nodeIndexCopy.count));
}
}

Expand Down Expand Up @@ -766,6 +764,20 @@ - (void)clearStoredAttributesForNodeID:(NSNumber *)nodeID
{
dispatch_async(_storageDelegateQueue, ^{
[self _clearStoredAttributesForNodeID:nodeID];
NSArray<NSNumber *> * nodeIndex = [self _fetchNodeIndex];
NSMutableArray<NSNumber *> * nodeIndexCopy = nodeIndex.mutableCopy;
[nodeIndexCopy removeObject:nodeID];
if (nodeIndex.count != nodeIndexCopy.count) {
BOOL success;
if (nodeIndexCopy.count) {
success = [self _storeNodeIndex:nodeIndexCopy];
} else {
success = [self _deleteNodeIndex];
}
if (!success) {
MTR_LOG_INFO("Store failed in clearStoredAttributesForNodeID for nodeIndex (%lu)", static_cast<unsigned long>(nodeIndexCopy.count));
}
}
});
}

Expand Down
28 changes: 28 additions & 0 deletions src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,34 @@ - (void)test008_TestDataStoreDirect
dataStoreValues = [controller.controllerDataStore getStoredAttributesForNodeID:@(1003)];
XCTAssertEqual(dataStoreValues.count, 0);

// Test MTRDeviceControllerDataStore _pruneEmptyStoredAttributesBranches
// - Clear cache
// - Store an attribute
// - Manually delete it from the user defaults
// - Call _pruneEmptyStoredAttributesBranches
[controller.controllerDataStore clearAllStoredAttributes];

NSArray * testAttribute = @[
@{ MTRAttributePathKey : [MTRAttributePath attributePathWithEndpointID:@(1) clusterID:@(1) attributeID:@(1)], MTRDataKey : @ { MTRTypeKey : MTRUnsignedIntegerValueType, MTRValueKey : @(111) } },
];
[controller.controllerDataStore storeAttributeValues:testAttribute forNodeID:@(2001)];
NSString * testAttributeValueKey = [controller.controllerDataStore _attributeValueKeyForNodeID:@(2001) endpointID:@(1) clusterID:@(1) attributeID:@(1)];
[storageDelegate controller:controller removeValueForKey:testAttributeValueKey securityLevel:MTRStorageSecurityLevelSecure sharingType:MTRStorageSharingTypeNotShared];
[controller.controllerDataStore _pruneEmptyStoredAttributesBranches];

// Now check the indexes are pruned
NSString * testAttributeIndexKey = [controller.controllerDataStore _attributeIndexKeyForNodeID:@(2001) endpointID:@(1) clusterID:@(1)];
id testAttributeIndex = [storageDelegate controller:controller valueForKey:testAttributeIndexKey securityLevel:MTRStorageSecurityLevelSecure sharingType:MTRStorageSharingTypeNotShared];
XCTAssertNil(testAttributeIndex);
NSString * testClusterIndexKey = [controller.controllerDataStore _clusterIndexKeyForNodeID:@(2001) endpointID:@(1)];
id testClusterIndex = [storageDelegate controller:controller valueForKey:testClusterIndexKey securityLevel:MTRStorageSecurityLevelSecure sharingType:MTRStorageSharingTypeNotShared];
XCTAssertNil(testClusterIndex);
NSString * testEndpointIndexKey = [controller.controllerDataStore _endpointIndexKeyForNodeID:@(2001)];
id testEndpointIndex = [storageDelegate controller:controller valueForKey:testEndpointIndexKey securityLevel:MTRStorageSecurityLevelSecure sharingType:MTRStorageSharingTypeNotShared];
XCTAssertNil(testEndpointIndex);
id testNodeIndex = [storageDelegate controller:controller valueForKey:@"attrCacheNodeIndex" securityLevel:MTRStorageSecurityLevelSecure sharingType:MTRStorageSharingTypeNotShared];
XCTAssertNil(testNodeIndex);

[controller shutdown];
XCTAssertFalse([controller isRunning]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ NS_ASSUME_NONNULL_BEGIN
- (void)storeAttributeValues:(NSArray<NSDictionary *> *)dataValues forNodeID:(NSNumber *)nodeID;
- (void)clearStoredAttributesForNodeID:(NSNumber *)nodeID;
- (void)clearAllStoredAttributes;
- (void)_pruneEmptyStoredAttributesBranches;
- (NSString *)_endpointIndexKeyForNodeID:(NSNumber *)nodeID;
- (NSString *)_clusterIndexKeyForNodeID:(NSNumber *)nodeID endpointID:(NSNumber *)endpointID;
- (NSString *)_attributeIndexKeyForNodeID:(NSNumber *)nodeID endpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID;
- (NSString *)_attributeValueKeyForNodeID:(NSNumber *)nodeID endpointID:(NSNumber *)endpointID clusterID:(NSNumber *)clusterID attributeID:(NSNumber *)attributeID;
@end

// Declare internal methods for testing
Expand Down

0 comments on commit e444ab9

Please sign in to comment.