Merge pull request #1314 from realm/tg/listener-test

Fix race conditions in testAddListener
This commit is contained in:
Thomas Goyne 2017-09-19 16:32:33 -07:00 committed by GitHub
commit 936dc15c2a
3 changed files with 60 additions and 33 deletions

View File

@ -54,13 +54,25 @@ function wait(t) {
return new Promise(resolve => setTimeout(resolve, t));
}
function repeatUntil(fn, predicate) {
let retries = 0
function check() {
if (retries > 3) {
return Promise.reject(new Error("operation timed out"));
}
++retries;
return fn().then(x => predicate(x) ? x : wait(100).then(check));
}
return check;
}
module.exports = {
testApplyAndGetGrantedPermissions() {
return createUsersWithTestRealms(1)
.then(([user]) => {
return user.applyPermissions({ userId: '*' }, `/${user.identity}/test`, 'read')
.then(wait(100))
.then(() => user.getGrantedPermissions('any'))
.then(repeatUntil(() => user.getGrantedPermissions('any'),
permissions => permissions.length > 1))
.then(permissions => {
TestCase.assertEqual(permissions[1].path, `/${user.identity}/test`);
TestCase.assertEqual(permissions[1].mayRead, true);
@ -77,17 +89,19 @@ module.exports = {
.then(token => user2.acceptPermissionOffer(token))
.then(realmUrl => {
TestCase.assertEqual(realmUrl, `/${user1.identity}/test`);
return user2.getGrantedPermissions('any')
.then(permissions => {
TestCase.assertEqual(permissions[1].path, `/${user1.identity}/test`);
TestCase.assertEqual(permissions[1].mayRead, true);
TestCase.assertEqual(permissions[1].mayWrite, false);
TestCase.assertEqual(permissions[1].mayManage, false);
});
return realmUrl;
})
.then(repeatUntil(() => user2.getGrantedPermissions('any'),
permissions => permissions.length > 1))
.then(permissions => {
TestCase.assertEqual(permissions[1].path, `/${user1.identity}/test`);
TestCase.assertEqual(permissions[1].mayRead, true);
TestCase.assertEqual(permissions[1].mayWrite, false);
TestCase.assertEqual(permissions[1].mayManage, false);
});
});
},
testInvalidatePermissionOffer() {
return createUsersWithTestRealms(2)
.then(([user1, user2]) => {

View File

@ -383,23 +383,23 @@ module.exports = {
TestCase.assertEqual(snapshot.length, 0);
});
},
testResultsFindIndexOfObject: function() {
var realm = new Realm({schema: [schemas.TestObject]});
var object1, object2, object3;
realm.write(function() {
object1 = realm.create('TestObject', {doubleCol: 1});
object2 = realm.create('TestObject', {doubleCol: 2});
object3 = realm.create('TestObject', {doubleCol: 2});
});
// Search in base table
const objects = realm.objects('TestObject');
TestCase.assertEqual(objects.indexOf(object1), 0);
TestCase.assertEqual(objects.indexOf(object2), 1);
TestCase.assertEqual(objects.indexOf(object3), 2);
// Search in filtered query
const results = objects.filtered("doubleCol == 2");
TestCase.assertEqual(results.indexOf(object1), -1);
@ -408,7 +408,7 @@ module.exports = {
const nonRealmObject = {test: "this is an object"};
TestCase.assertEqual(objects.indexOf(nonRealmObject), -1);
// Searching for object from the wrong realm
var realm2 = new Realm({path: '2.realm', schema: realm.schema});
var object4;
@ -421,29 +421,40 @@ module.exports = {
},
testAddListener: function() {
return new Promise((resolve, _reject) => {
var realm = new Realm({ schema: [schemas.TestObject] });
if (typeof navigator !== 'undefined' && /Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef
// FIXME: async callbacks do not work correctly in Chrome debugging mode
return;
}
realm.write(() => {
realm.create('TestObject', { doubleCol: 1 });
realm.create('TestObject', { doubleCol: 2 });
realm.create('TestObject', { doubleCol: 3 });
});
const realm = new Realm({ schema: [schemas.TestObject] });
realm.write(() => {
realm.create('TestObject', { doubleCol: 1 });
realm.create('TestObject', { doubleCol: 2 });
realm.create('TestObject', { doubleCol: 3 });
});
let resolve, first = true;
return new Promise((r, _reject) => {
resolve = r;
realm.objects('TestObject').addListener((testObjects, changes) => {
// TODO: First notification is empty, so perform these
// assertions on the second call. However, there is a race condition
// in React Native, so find a way to do this in a robust way.
//TestCase.assertEqual(testObjects.length, 4);
//TestCase.assertEqual(changes.insertions.length, 1);
if (first) {
TestCase.assertEqual(testObjects.length, 3);
TestCase.assertEqual(changes.insertions.length, 0);
}
else {
TestCase.assertEqual(testObjects.length, 4);
TestCase.assertEqual(changes.insertions.length, 1);
}
first = false;
resolve();
});
realm.write(() => {
realm.create('TestObject', { doubleCol: 1 });
}).then(() => {
return new Promise((r, _reject) => {
realm.write(() => {
realm.create('TestObject', { doubleCol: 1 });
});
resolve = r;
});
})
});
}
};

View File

@ -168,6 +168,7 @@ extern NSMutableArray *RCTGetModuleClasses(void);
+ (void)waitForCondition:(BOOL *)condition description:(NSString *)description {
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow:30.0];
RCTBridge *bridge = [self currentBridge];
while (!*condition) {
if ([timeout timeIntervalSinceNow] < 0) {
@ -180,6 +181,7 @@ extern NSMutableArray *RCTGetModuleClasses(void);
[runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
[runLoop runMode:NSRunLoopCommonModes beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
[NSThread sleepForTimeInterval:0.01]; // Bad things may happen without some sleep.
[bridge.eventDispatcher sendAppEventWithName:@"realm-dummy" body:nil]; // Ensure RN has an event loop running
}
}
}