Skip to content

Commit 9b05307

Browse files
authored
Improve C4 object leak check in tests (#3483)
Cherry-picked this from release/4.0 branch. * Created a utility function for interval waiting and checking using runloop spinning to avoid main thread block error warning. This is required when running with the host app on the actual iOS devices otherwise the tests could fail as blocking the main thread on the recent XCode. * Increased the timeout period from 2 seconds to 8 seconds. * Made a constant variable for disabling MultipeerReplicatorTest.
1 parent cdcb726 commit 9b05307

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

Objective-C/Tests/CBLTestCase.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ extern const NSTimeInterval kExpTimeout;
215215
*/
216216
- (XCTestExpectation*) allowOverfillExpectationWithDescription:(NSString *)description;
217217

218+
/** A utility function for waiting for the condition or timeout, spinning the run loop without blocking. */
219+
- (BOOL) waitWithTimeout: (NSTimeInterval)timeout
220+
interval: (NSTimeInterval)interval
221+
until: (BOOL (^)(void))condition;
222+
218223
@end
219224

220225
NS_ASSUME_NONNULL_END

Objective-C/Tests/CBLTestCase.m

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,12 @@ - (void) tearDown {
7979

8080
if (!_disableObjectLeakCheck) {
8181
// Wait a little while for objects to be cleaned up:
82-
int leaks = 0;
83-
for (int i = 0; i < 20; i++) {
84-
leaks = c4_getObjectCount() - _c4ObjectCount;
85-
if (leaks == 0)
86-
break;
87-
else
88-
[NSThread sleepForTimeInterval: 0.1];
89-
}
90-
if (leaks) {
82+
__block int leaks = 0;
83+
BOOL success = [self waitWithTimeout: 8 interval: 0.5 until: ^BOOL{
84+
leaks = c4_getObjectCount() - self->_c4ObjectCount;
85+
return leaks == 0;
86+
}];
87+
if (!success) {
9188
fprintf(stderr, "**** LITECORE OBJECTS STILL NOT FREED: ****\n");
9289
c4_dumpInstances();
9390
XCTFail("%d LiteCore objects have not been freed (see above)", leaks);
@@ -479,4 +476,18 @@ - (NSString*) getRickAndMortyJSON {
479476
return [self stringFromResource: @"rick_morty" ofType: @"json"];
480477
}
481478

479+
- (BOOL) waitWithTimeout: (NSTimeInterval)timeout
480+
interval: (NSTimeInterval)interval
481+
until: (BOOL (^)(void))condition {
482+
NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow: timeout];
483+
while ([deadline timeIntervalSinceNow] > 0) {
484+
if (condition()) {
485+
return YES;
486+
}
487+
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode
488+
beforeDate: [NSDate dateWithTimeIntervalSinceNow: interval]];
489+
}
490+
return condition();
491+
}
492+
482493
@end

Objective-C/Tests/MultipeerReplicatorTest.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#define kTestMaxIdentity 10
1717
#define kTestIdentityCommonName @"CBLMultipeerReplTest"
1818

19+
// Disable as cannot be run on the build VM (Got POSIX Error 50, Network is down)
20+
#define kDisableMultipeerTests YES
21+
1922
typedef NSMutableDictionary<CBLPeerID*, CBLPeerReplicatorStatus*> CBLPeerReplicatorStatusMap;
2023

2124
typedef CBLDocument * _Nullable (^MultipeerConflictResolverBlock)(CBLPeerID*, CBLConflict*);
@@ -67,6 +70,9 @@ - (void) setUp {
6770

6871
XCTSkipUnless(self.isExecutionAllowed);
6972

73+
// Disabled by testConfigurationValidation, ensure to re-enable the leak check:
74+
self.disableObjectLeakCheck = NO;
75+
7076
[self cleanupIdentities];
7177
[self openOtherDB];
7278

@@ -85,13 +91,12 @@ - (void) tearDown {
8591

8692
// TODO: Too flaky, enable after all LiteCore issues have been handled.
8793
- (BOOL) isExecutionAllowed {
88-
#if TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST || TARGET_OS_OSX
94+
#if TARGET_OS_SIMULATOR
8995
// Cannot be tested on the simulator as local network access permission is required.
9096
// See FAQ-12 : https://developer.apple.com/forums/thread/663858)
9197
return NO;
9298
#else
93-
// Disable as cannot be run on the build VM (Got POSIX Error 50, Network is down)
94-
return self.keyChainAccessAllowed;
99+
return kDisableMultipeerTests ? NO : self.keyChainAccessAllowed;
95100
#endif
96101
}
97102

Swift/Tests/MultipeerReplicatorTest.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class MultipeerReplicatorTest: CBLTestCase {
3434
}
3535
}
3636

37+
// Disable as cannot be run on the build VM (Got POSIX Error 50, Network is down)
38+
let kDisableMultipeerReplicatorTest = true
39+
3740
let kTestKeyUsages: KeyUsages = [.clientAuth, .serverAuth]
3841

3942
let kTestPeerGroupID = "CBLMultipeerReplTestGroup"
@@ -59,8 +62,7 @@ class MultipeerReplicatorTest: CBLTestCase {
5962
// See FAQ-12: https://developer.apple.com/forums/thread/663858
6063
return false
6164
#else
62-
// Disable as cannot be run on the build VM (Got POSIX Error 50, Network is down)
63-
return keyChainAccessAllowed && false
65+
return kDisableMultipeerReplicatorTest ? false : keyChainAccessAllowed;
6466
#endif
6567
}
6668

0 commit comments

Comments
 (0)