Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(abort): abort waiting with error upon disconnect #2204

Merged
merged 1 commit into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class Helper {
resolveCallback = resolve;
rejectCallback = reject;
});

// Add listener.
const listener = Helper.addEventListener(emitter, eventName, event => {
try {
if (!predicate(event))
Expand All @@ -142,20 +144,21 @@ class Helper {
rejectCallback(e);
}
});

// Reject upon timeout.
const eventTimeout = setTimeout(() => {
rejectCallback(new TimeoutError(`Timeout exceeded while waiting for ${String(eventName)}`));
}, helper.timeUntilDeadline(deadline));
function cleanup() {

// Reject upon abort.
abortPromise.then(rejectCallback);

try {
return await promise;
} finally {
Helper.removeEventListeners([listener]);
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> {
Expand Down
10 changes: 10 additions & 0 deletions test/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
it('should work', async({page, server}) => {
await page.goto(server.EMPTY_PAGE);
Expand Down