Make permission tests better handle server delays

Retry a few times if the request hasn't been processed yet rather than hoping
that a 100ms sleep will suffice.
This commit is contained in:
Thomas Goyne 2017-09-19 11:53:49 -07:00
parent 8ad5c36cca
commit e8e23bbee7
1 changed files with 24 additions and 10 deletions

View File

@ -54,13 +54,25 @@ function wait(t) {
return new Promise(resolve => setTimeout(resolve, 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 = { module.exports = {
testApplyAndGetGrantedPermissions() { testApplyAndGetGrantedPermissions() {
return createUsersWithTestRealms(1) return createUsersWithTestRealms(1)
.then(([user]) => { .then(([user]) => {
return user.applyPermissions({ userId: '*' }, `/${user.identity}/test`, 'read') return user.applyPermissions({ userId: '*' }, `/${user.identity}/test`, 'read')
.then(wait(100)) .then(repeatUntil(() => user.getGrantedPermissions('any'),
.then(() => user.getGrantedPermissions('any')) permissions => permissions.length > 1))
.then(permissions => { .then(permissions => {
TestCase.assertEqual(permissions[1].path, `/${user.identity}/test`); TestCase.assertEqual(permissions[1].path, `/${user.identity}/test`);
TestCase.assertEqual(permissions[1].mayRead, true); TestCase.assertEqual(permissions[1].mayRead, true);
@ -77,13 +89,15 @@ module.exports = {
.then(token => user2.acceptPermissionOffer(token)) .then(token => user2.acceptPermissionOffer(token))
.then(realmUrl => { .then(realmUrl => {
TestCase.assertEqual(realmUrl, `/${user1.identity}/test`); TestCase.assertEqual(realmUrl, `/${user1.identity}/test`);
return user2.getGrantedPermissions('any') return realmUrl;
.then(permissions => { })
TestCase.assertEqual(permissions[1].path, `/${user1.identity}/test`); .then(repeatUntil(() => user2.getGrantedPermissions('any'),
TestCase.assertEqual(permissions[1].mayRead, true); permissions => permissions.length > 1))
TestCase.assertEqual(permissions[1].mayWrite, false); .then(permissions => {
TestCase.assertEqual(permissions[1].mayManage, false); 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);
}); });
}); });
}, },