mirror of
https://github.com/microsoft/playwright.git
synced 2025-01-07 11:46:42 +03:00
fix(abort): abort waiting with error upon disconnect (#2204)
This commit is contained in:
parent
54b056bddb
commit
cb465bc698
@ -133,6 +133,8 @@ class Helper {
|
|||||||
resolveCallback = resolve;
|
resolveCallback = resolve;
|
||||||
rejectCallback = reject;
|
rejectCallback = reject;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Add listener.
|
||||||
const listener = Helper.addEventListener(emitter, eventName, event => {
|
const listener = Helper.addEventListener(emitter, eventName, event => {
|
||||||
try {
|
try {
|
||||||
if (!predicate(event))
|
if (!predicate(event))
|
||||||
@ -142,20 +144,21 @@ class Helper {
|
|||||||
rejectCallback(e);
|
rejectCallback(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Reject upon timeout.
|
||||||
const eventTimeout = setTimeout(() => {
|
const eventTimeout = setTimeout(() => {
|
||||||
rejectCallback(new TimeoutError(`Timeout exceeded while waiting for ${String(eventName)}`));
|
rejectCallback(new TimeoutError(`Timeout exceeded while waiting for ${String(eventName)}`));
|
||||||
}, helper.timeUntilDeadline(deadline));
|
}, helper.timeUntilDeadline(deadline));
|
||||||
function cleanup() {
|
|
||||||
|
// Reject upon abort.
|
||||||
|
abortPromise.then(rejectCallback);
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await promise;
|
||||||
|
} finally {
|
||||||
Helper.removeEventListeners([listener]);
|
Helper.removeEventListeners([listener]);
|
||||||
clearTimeout(eventTimeout);
|
clearTimeout(eventTimeout);
|
||||||
}
|
}
|
||||||
return await Promise.race([promise, abortPromise]).then(r => {
|
|
||||||
cleanup();
|
|
||||||
return r;
|
|
||||||
}, e => {
|
|
||||||
cleanup();
|
|
||||||
throw e;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async waitWithDeadline<T>(promise: Promise<T>, taskName: string, deadline: number, debugName: string): Promise<T> {
|
static async waitWithDeadline<T>(promise: Promise<T>, taskName: string, deadline: number, debugName: string): Promise<T> {
|
||||||
|
@ -345,6 +345,16 @@ describe('Page.waitForRequest', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Page.waitForEvent', function() {
|
||||||
|
it('should fail with error upon disconnect', async({page, server}) => {
|
||||||
|
let error;
|
||||||
|
const waitForPromise = page.waitForEvent('download').catch(e => error = e);
|
||||||
|
await page.close();
|
||||||
|
await waitForPromise;
|
||||||
|
expect(error.message).toContain('Target closed');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('Page.waitForResponse', function() {
|
describe('Page.waitForResponse', function() {
|
||||||
it('should work', async({page, server}) => {
|
it('should work', async({page, server}) => {
|
||||||
await page.goto(server.EMPTY_PAGE);
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
Loading…
Reference in New Issue
Block a user