add a timeout function to promiseUtils for attemptConnection

pull/1306/head
Audric Ackermann 5 years ago
parent eb3ee8f6b0
commit cf3352d0af
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -103,7 +103,7 @@ export class SessionClosableOverlay extends React.Component<Props, State> {
});
}
// tslint:disable-next-line max-func-body-length */
// tslint:disable-next-line max-func-body-length cyclomatic-complexity */
public render(): JSX.Element {
const {
overlayMode,

@ -1,4 +1,5 @@
import { ConversationModel } from '../../../js/models/conversations';
import { PromiseUtils } from '../utils';
interface OpenGroupParams {
server: string;
@ -126,8 +127,14 @@ export class OpenGroup {
onLoading();
}
conversation = await window.attemptConnection(prefixedServer, channel);
conversationId = conversation?.cid;
conversation = await PromiseUtils.timeout(
window.attemptConnection(prefixedServer, channel),
5000
);
if (!conversation) {
throw new Error(window.i18n('connectToServerFail'));
}
conversationId = (conversation as any)?.cid;
} catch (e) {
throw new Error(e);
}

@ -14,13 +14,13 @@ async function toPromise<T>(value: Return<T>): Promise<T> {
*/
export async function waitForTask<T>(
task: (done: SimpleFunction<T>) => Return<void>,
timeout: number = 2000
timeoutMs: number = 2000
): Promise<T> {
const timeoutPromise = new Promise<T>((_, rej) => {
const wait = setTimeout(() => {
clearTimeout(wait);
rej(new Error('Task timed out.'));
}, timeout);
}, timeoutMs);
});
const taskPromise = new Promise(async (res, rej) => {
@ -35,7 +35,7 @@ export async function waitForTask<T>(
}
export interface PollOptions {
timeout: number;
timeoutMs: number;
interval: number;
}
@ -51,16 +51,16 @@ export async function poll(
options: Partial<PollOptions> = {}
): Promise<void> {
const defaults: PollOptions = {
timeout: 2000,
timeoutMs: 2000,
interval: 100,
};
const { timeout, interval } = {
const { timeoutMs, interval } = {
...defaults,
...options,
};
const endTime = Date.now() + timeout;
const endTime = Date.now() + timeoutMs;
let stop = false;
const finish = () => {
stop = true;
@ -101,7 +101,7 @@ export async function poll(
*/
export async function waitUntil(
check: () => Return<boolean>,
timeout: number = 2000
timeoutMs: number = 2000
) {
// This is causing unhandled promise rejection somewhere in MessageQueue tests
return poll(
@ -112,8 +112,22 @@ export async function waitUntil(
}
},
{
timeout,
interval: timeout / 20,
timeoutMs,
interval: timeoutMs / 20,
}
);
}
export async function timeout<T>(
promise: Promise<T>,
timeoutMs: number = 2000
): Promise<T> {
const timeoutPromise = new Promise<T>((_, rej) => {
const wait = setTimeout(() => {
clearTimeout(wait);
rej(new Error('Task timed out.'));
}, timeoutMs);
});
return Promise.race([timeoutPromise, promise]);
}

@ -60,7 +60,7 @@ describe('Promise Utils', () => {
const completionSpy = sandbox.spy();
const task = (_done: any) => undefined;
const promise = PromiseUtils.poll(task, { timeout: 1 });
const promise = PromiseUtils.poll(task, { timeoutMs: 1 });
await expect(promise).to.be.rejectedWith('Periodic check timeout');
expect(pollSpy.callCount).to.equal(1);
@ -82,7 +82,7 @@ describe('Promise Utils', () => {
}
};
const promise = PromiseUtils.poll(task, { timeout, interval });
const promise = PromiseUtils.poll(task, { timeoutMs: timeout, interval });
await expect(promise).to.be.fulfilled;
expect(pollSpy.callCount).to.equal(1);

Loading…
Cancel
Save