Unverified Commit cd6c4c31 authored by Kyle Keating's avatar Kyle Keating Committed by GitHub

requestGroups RPC call should not timeout (#2159)

Change the client RPC request to optionally bypass the timeout. There could be a case where it's waiting for user input to proceed with authorization and could take any number of seconds.
parent ec84d3be
......@@ -160,8 +160,9 @@ async function fetchGroupsAsync(config, rpcCall) {
if (Array.isArray(config.services)) {
config.services.forEach((service, index) => {
if (service.groups === '$rpc:requestGroups') {
// The `groups` need to be fetched from a secondary RPC call
service.groups = rpcCall('requestGroups', [index], 5000).catch(() => {
// The `groups` need to be fetched from a secondary RPC call and
// there should be no timeout as it may be waiting for user input.
service.groups = rpcCall('requestGroups', [index], null).catch(() => {
throw new Error('Unable to fetch groups');
});
}
......
......@@ -21,7 +21,8 @@ function createTimeout(delay, message) {
* @param {string} origin - Origin filter for `window.postMessage` call
* @param {string} method - Name of the JSON-RPC method
* @param {any[]} params - Parameters of the JSON-RPC method
* @param [number] timeout - Maximum time to wait in ms
* @param {number|null} [number=2000] timeout - Maximum time to wait in
* ms or null or 0 to ignore timeout.
* @param [Window] window_ - Test seam.
* @param [id] id - Test seam.
* @return {Promise<any>} - A Promise for the response to the call
......@@ -79,14 +80,16 @@ export function call(
window_.addEventListener('message', listener);
});
const timeoutExpired = createTimeout(
timeout,
`Request to ${origin} timed out`
);
const responseOrTimeout = [response];
if (timeout) {
responseOrTimeout.push(
createTimeout(timeout, `Request to ${origin} timed out`)
);
}
// Cleanup and return.
// FIXME: If we added a `Promise.finally` polyfill we could simplify this.
return Promise.race([response, timeoutExpired])
return Promise.race(responseOrTimeout)
.then(result => {
window_.removeEventListener('message', listener);
return result;
......
......@@ -257,7 +257,7 @@ describe('sidebar.util.fetch-config', () => {
'https://embedder.com',
'requestGroups',
[0], // passes service index to requestGroups
5000
null // no timeout
)
);
});
......
......@@ -18,8 +18,7 @@ describe('sidebar.util.postmessage-json-rpc', () => {
let frame;
let fakeWindow;
function doCall() {
const timeout = 1;
function doCall(timeout = 1) {
return call(
frame,
origin,
......@@ -146,5 +145,21 @@ describe('sidebar.util.postmessage-json-rpc', () => {
'Request to https://embedder.com timed out'
);
});
it('if timeout is null, then it does not timeout', async () => {
const clock = sinon.useFakeTimers();
const result = doCall(null);
clock.tick(100000); // wait a long time
fakeWindow.emitter.emit('message', {
origin,
data: {
jsonrpc: '2.0',
id: messageId,
result: {},
},
});
await result;
clock.restore();
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment