Skip to content

Commit

Permalink
Fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jtung-apple committed May 23, 2024
1 parent 9ad4922 commit 9a4f3e4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/darwin/Framework/CHIPTests/MTRDeviceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ - (void)tearDown
// existing subscriptions and then fail out due to requesting a subscribe to
// a nonexistent cluster.
if (mConnectedDevice != nil) {
NSLog(@"JEFFTEST: teardown - unsubscribe");
dispatch_queue_t queue = dispatch_get_main_queue();

MTRSubscribeParams * params = [[MTRSubscribeParams alloc] initWithMinInterval:@(0) maxInterval:@(10)];
Expand Down Expand Up @@ -3679,7 +3680,7 @@ - (void)test036_TestStorageBehaviorConfiguration

__auto_type * device = [MTRDevice deviceWithNodeID:kDeviceId deviceController:sController];

__auto_type * delegate = [[MTRDeviceTestDelegate alloc] init];
__auto_type * delegate = [[MTRDeviceTestDelegateWithSubscriptionSetupOverride alloc] init];
__block os_unfair_lock lock = OS_UNFAIR_LOCK_INIT;
__block NSDate * reportEndTime = nil;
__block NSDate * dataPersistedTime = nil;
Expand Down Expand Up @@ -3832,9 +3833,10 @@ - (void)test036_TestStorageBehaviorConfiguration
os_unfair_lock_lock(&lock);
reportToPersistenceDelay = [dataPersistedTime timeIntervalSinceDate:reportEndTime];
os_unfair_lock_unlock(&lock);
// Check delay exists and approximately base delay times 3
// Check delay exists and at least base delay times 3
XCTAssertGreaterThan(reportToPersistenceDelay, baseTestDelayTime * 3 * 0.9);
XCTAssertLessThan(reportToPersistenceDelay, baseTestDelayTime * 3 * 1.3); // larger upper limit in case machine is slow
// upper limit at most max delay times full multiplier + extra in case machine is slow
XCTAssertLessThan(reportToPersistenceDelay, baseTestDelayTime * 2 * 5 * 1.3);

// Test 4: test reporting excessively, and see that persistence does not happen until
// reporting frequency goes back above the threshold
Expand Down Expand Up @@ -3888,6 +3890,9 @@ - (void)test036_TestStorageBehaviorConfiguration
[device unitTestInjectAttributeReport:attributeReport fromSubscription:YES];

[self waitForExpectations:@[ dataPersisted4 ] timeout:60];

delegate.onReportEnd = nil;
delegate.onClusterDataPersisted = nil;
}

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ typedef void (^MTRDeviceTestDelegateDataHandler)(NSArray<NSDictionary<NSString *
@property (nonatomic) BOOL pretendThreadEnabled;
@property (nonatomic, nullable) dispatch_block_t onSubscriptionPoolDequeue;
@property (nonatomic, nullable) dispatch_block_t onSubscriptionPoolWorkComplete;
@property (nonatomic) BOOL skipSetupSubscription;
@property (nonatomic, nullable) dispatch_block_t onClusterDataPersisted;
@end

@interface MTRDeviceTestDelegateWithSubscriptionSetupOverride : MTRDeviceTestDelegate
@property (nonatomic) BOOL skipSetupSubscription;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ - (void)unitTestSubscriptionPoolWorkComplete:(MTRDevice *)device
}
}

- (BOOL)unitTestShouldSetUpSubscriptionForDevice:(MTRDevice *)device
{
return !_skipSetupSubscription;
}

- (void)unitTestClusterDataPersisted:(MTRDevice *)device
{
if (self.onClusterDataPersisted != nil) {
Expand All @@ -109,3 +104,12 @@ - (void)unitTestClusterDataPersisted:(MTRDevice *)device
}

@end

@implementation MTRDeviceTestDelegateWithSubscriptionSetupOverride

- (BOOL)unitTestShouldSetUpSubscriptionForDevice:(MTRDevice *)device
{
return !_skipSetupSubscription;
}

@end

0 comments on commit 9a4f3e4

Please sign in to comment.