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

New failing test demonstrating a race condition #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@ describe('makeTrashable()', () => {
});
});

test('cancels the promise without a race condition', () => {
const timeoutPromise = delay => {
return new Promise(resolve => {
setTimeout(resolve, delay);
});
};
const original = timeoutPromise(2);
const trashablePromise = makeTrashable(original);
let trashed = false;
let last = false;
original.then(() => {
expect(last).toBe(false); // (A)
trashed = true;
trashablePromise.trash();
});
trashablePromise.then(() => {
last = true; // (B)
});
return timeoutPromise(100).then(() => {
expect(trashed).toBe(true); // (C)
// Right now, this 'expect' fails. This means that the `then` on
// trashablePromise, (B), executed. However:
// - 'expect' (C) does not fail, which means that the promise actually
// got trashed.
// - Thus 'expect' (A) executed, and it did not fail, which means that
// we called trash() *before* (B) executed.
expect(last).toBe(false);
});
});

test.requireGC('makes handler garbage collectable', () => {
class Foo {
constructor(promise) {
Expand Down