From e8e23bbee7824323939e9657c4007a12bef72556 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Tue, 19 Sep 2017 11:53:49 -0700 Subject: [PATCH] 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. --- tests/js/permission-tests.js | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/tests/js/permission-tests.js b/tests/js/permission-tests.js index c1bf831e..5f33e95d 100644 --- a/tests/js/permission-tests.js +++ b/tests/js/permission-tests.js @@ -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]) => {