Skip to content

Commit 8b8dadd

Browse files
Fix some random Darwin test failures due to timing dependency. (#33761)
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 d4d9054 commit 8b8dadd

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
@@ -383,6 +383,7 @@ - (BOOL)unitTestPretendThreadEnabled:(MTRDevice *)device;
383383
- (void)unitTestSubscriptionPoolDequeue:(MTRDevice *)device;
384384
- (void)unitTestSubscriptionPoolWorkComplete:(MTRDevice *)device;
385385
- (void)unitTestClusterDataPersisted:(MTRDevice *)device;
386+
- (BOOL)unitTestSuppressTimeBasedReachabilityChanges:(MTRDevice *)device;
386387
@end
387388
#endif
388389

@@ -2026,15 +2027,24 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason
20262027

20272028
MTR_LOG("%@ setting up subscription with reason: %@", self, reason);
20282029

2029-
// Set up a timer to mark as not reachable if it takes too long to set up a subscription
2030-
MTRWeakReference<MTRDevice *> * weakSelf = [MTRWeakReference weakReferenceWithObject:self];
2031-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, static_cast<int64_t>(kSecondsToWaitBeforeMarkingUnreachableAfterSettingUpSubscription) * static_cast<int64_t>(NSEC_PER_SEC)), self.queue, ^{
2032-
MTRDevice * strongSelf = weakSelf.strongObject;
2033-
if (strongSelf != nil) {
2034-
std::lock_guard lock(strongSelf->_lock);
2035-
[strongSelf _markDeviceAsUnreachableIfNeverSubscribed];
2036-
}
2037-
});
2030+
bool markUnreachableAfterWait = true;
2031+
#ifdef DEBUG
2032+
if (delegate && [delegate respondsToSelector:@selector(unitTestSuppressTimeBasedReachabilityChanges:)]) {
2033+
markUnreachableAfterWait = ![delegate unitTestSuppressTimeBasedReachabilityChanges:self];
2034+
}
2035+
#endif
2036+
2037+
if (markUnreachableAfterWait) {
2038+
// Set up a timer to mark as not reachable if it takes too long to set up a subscription
2039+
MTRWeakReference<MTRDevice *> * weakSelf = [MTRWeakReference weakReferenceWithObject:self];
2040+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, static_cast<int64_t>(kSecondsToWaitBeforeMarkingUnreachableAfterSettingUpSubscription) * static_cast<int64_t>(NSEC_PER_SEC)), self.queue, ^{
2041+
MTRDevice * strongSelf = weakSelf.strongObject;
2042+
if (strongSelf != nil) {
2043+
std::lock_guard lock(strongSelf->_lock);
2044+
[strongSelf _markDeviceAsUnreachableIfNeverSubscribed];
2045+
}
2046+
});
2047+
}
20382048

20392049
[_deviceController
20402050
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)