Skip to content

Commit 1a18a07

Browse files
Fix some random Darwin test failures due to timing dependency.
Some Darwin tests failed randomly if a subscription took longer than 10s to establish. Add test-only conditionals around the relevant code so we suppress that behavior in tests.
1 parent 24bbdf4 commit 1a18a07

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/darwin/Framework/CHIP/MTRDevice.mm

+19-9
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ - (BOOL)unitTestPretendThreadEnabled:(MTRDevice *)device;
376376
- (void)unitTestSubscriptionPoolDequeue:(MTRDevice *)device;
377377
- (void)unitTestSubscriptionPoolWorkComplete:(MTRDevice *)device;
378378
- (void)unitTestClusterDataPersisted:(MTRDevice *)device;
379+
- (BOOL)unitTestSuppressTimeBasedReachabilityChanges:(MTRDevice *)device;
379380
@end
380381
#endif
381382

@@ -2014,15 +2015,24 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason
20142015

20152016
MTR_LOG("%@ setting up subscription with reason: %@", self, reason);
20162017

2017-
// Set up a timer to mark as not reachable if it takes too long to set up a subscription
2018-
MTRWeakReference<MTRDevice *> * weakSelf = [MTRWeakReference weakReferenceWithObject:self];
2019-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, static_cast<int64_t>(kSecondsToWaitBeforeMarkingUnreachableAfterSettingUpSubscription) * static_cast<int64_t>(NSEC_PER_SEC)), self.queue, ^{
2020-
MTRDevice * strongSelf = weakSelf.strongObject;
2021-
if (strongSelf != nil) {
2022-
std::lock_guard lock(strongSelf->_lock);
2023-
[strongSelf _markDeviceAsUnreachableIfNeverSubscribed];
2024-
}
2025-
});
2018+
bool markUnreachableAfterWait = true;
2019+
#ifdef DEBUG
2020+
if (delegate && [delegate respondsToSelector:@selector(unitTestSuppressTimeBasedReachabilityChanges:)]) {
2021+
markUnreachableAfterWait = ![delegate unitTestSuppressTimeBasedReachabilityChanges:self];
2022+
}
2023+
#endif
2024+
2025+
if (markUnreachableAfterWait) {
2026+
// Set up a timer to mark as not reachable if it takes too long to set up a subscription
2027+
MTRWeakReference<MTRDevice *> * weakSelf = [MTRWeakReference weakReferenceWithObject:self];
2028+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, static_cast<int64_t>(kSecondsToWaitBeforeMarkingUnreachableAfterSettingUpSubscription) * static_cast<int64_t>(NSEC_PER_SEC)), self.queue, ^{
2029+
MTRDevice * strongSelf = weakSelf.strongObject;
2030+
if (strongSelf != nil) {
2031+
std::lock_guard lock(strongSelf->_lock);
2032+
[strongSelf _markDeviceAsUnreachableIfNeverSubscribed];
2033+
}
2034+
});
2035+
}
20262036

20272037
[_deviceController
20282038
getSessionForNode:_nodeID.unsignedLongLongValue

src/darwin/Framework/CHIPTests/TestHelpers/MTRDeviceTestDelegate.m

+9
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ - (void)unitTestClusterDataPersisted:(MTRDevice *)device
103103
}
104104
}
105105

106+
- (BOOL)unitTestSuppressTimeBasedReachabilityChanges:(MTRDevice *)device
107+
{
108+
// Allowing time-based reachability changes just makes the tests
109+
// non-deterministic and can lead to random failures. Suppress them
110+
// unconditionally for now. If we ever add tests that try to exercise that
111+
// codepath, we can make this configurable.
112+
return YES;
113+
}
114+
106115
@end
107116

108117
@implementation MTRDeviceTestDelegateWithSubscriptionSetupOverride

0 commit comments

Comments
 (0)