Skip to content

Commit 9278dc1

Browse files
authored
Improve C4 object leak check in tests (#3479)
* Created a utitlity function for interval wating and checking using runlopp spinning to avoid main thread block warning. * Increased the timeout period from 2 seconds to 8 seconds. * Made a constant variable for disabing MultipeerReplicatorTest.
1 parent 9580f51 commit 9278dc1

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

Objective-C/Tests/CBLTestCase.h

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

221+
/** A utility function for waiting for the condition or timeout, spinning the run loop without blocking. */
222+
- (BOOL) waitWithTimeout: (NSTimeInterval)timeout
223+
interval: (NSTimeInterval)interval
224+
until: (BOOL (^)(void))condition;
225+
221226
@end
222227

223228
NS_ASSUME_NONNULL_END

Objective-C/Tests/CBLTestCase.m

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ @implementation CBLTestCase
3636

3737
@synthesize db=_db, otherDB=_otherDB, disableObjectLeakCheck=_disableObjectLeakCheck;
3838

39-
// TODO: Remove https://issues.couchbase.com/browse/CBL-3206
40-
#pragma clang diagnostic push
41-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
42-
4339
- (XCTestExpectation*) allowOverfillExpectationWithDescription:(NSString *)description {
4440
XCTestExpectation* e = [super expectationWithDescription: description];
4541
e.assertForOverFulfill = false;
@@ -75,15 +71,12 @@ - (void) tearDown {
7571

7672
if (!_disableObjectLeakCheck) {
7773
// Wait a little while for objects to be cleaned up:
78-
int leaks = 0;
79-
for (int i = 0; i < 20; i++) {
80-
leaks = c4_getObjectCount() - _c4ObjectCount;
81-
if (leaks == 0)
82-
break;
83-
else
84-
[NSThread sleepForTimeInterval: 0.1];
85-
}
86-
if (leaks) {
74+
__block int leaks = 0;
75+
BOOL success = [self waitWithTimeout: 8 interval: 0.5 until: ^BOOL{
76+
leaks = c4_getObjectCount() - self->_c4ObjectCount;
77+
return leaks == 0;
78+
}];
79+
if (!success) {
8780
fprintf(stderr, "**** LITECORE OBJECTS STILL NOT FREED: ****\n");
8881
c4_dumpInstances();
8982
XCTFail("%d LiteCore objects have not been freed (see above)", leaks);
@@ -483,6 +476,18 @@ - (NSString*) getRickAndMortyJSON {
483476
return [self stringFromResource: @"rick_morty" ofType: @"json"];
484477
}
485478

486-
#pragma clang diagnostic pop
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+
}
487492

488493
@end

Objective-C/Tests/MultipeerReplicatorTest.m

Lines changed: 7 additions & 2 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

@@ -90,8 +96,7 @@ - (BOOL) isExecutionAllowed {
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 && false;
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)