Promises: whenever evaluating checkSequence(sequence), also assert length of sequence (#2672)

This commit is contained in:
Rick Waldron 2020-06-24 15:18:35 -04:00 committed by GitHub
parent 20a1345bbe
commit 040eb5393a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 115 additions and 27 deletions

View File

@ -20,6 +20,7 @@ Promise.resolve().then(function() {
sequence.push(3);
}).then(function() {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "Promises resolved in unexpected sequence");
}).then($DONE, $DONE);

View File

@ -22,14 +22,17 @@ sequence.push(1);
Promise.all([p1]).then(function(resolved) {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "Expected Promise.all().then to queue second");
}).catch($DONE);
p1.then(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Expected p1.then to queue first");
}).then(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "Expected final then to queue last");
}).then($DONE, $DONE);

View File

@ -22,16 +22,19 @@ sequence.push(1);
p1.then(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Expected to be called first.");
}).catch($DONE);
Promise.all([p1, p2]).then(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "Expected to be called third.");
}).then($DONE, $DONE);
p2.then(function() {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "Expected to be called second.");
}).catch($DONE);

View File

@ -19,7 +19,8 @@ Promise.resolve().then(function() {
sequence.push(3);
}).then(function() {
sequence.push(4);
checkSequence(sequence, 'Promises resolved in unexpected sequence');
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Promises resolved in unexpected sequence');
}).then($DONE, $DONE);
sequence.push(1);

View File

@ -30,14 +30,17 @@ sequence.push(1);
Promise.allSettled([p1]).then(function(resolved) {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected Promise.allSettled().then to queue second');
}).catch($DONE);
p1.then(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected p1.then to queue first');
}).then(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected final then to queue last');
}).then($DONE, $DONE);

View File

@ -37,22 +37,26 @@ sequence.push(1);
p1.catch(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected to be called first.');
}).catch($DONE);
});
Promise.allSettled([p1, p2, p3]).then(function() {
sequence.push(6);
assert.sameValue(sequence.length, 6);
checkSequence(sequence, 'Expected to be called fourth.');
}).then($DONE, $DONE);
p2.then(function() {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected to be called second.');
}).catch($DONE);
});
sequence.push(2);
p3.catch(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected to be called third.');
}).catch($DONE);
});

View File

@ -33,16 +33,19 @@ sequence.push(1);
p1.catch(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected to be called first.');
}).catch($DONE);
Promise.allSettled([p1, p2]).then(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected to be called third.');
}).then($DONE, $DONE);
p2.catch(function() {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected to be called second.');
}).catch($DONE);

View File

@ -33,16 +33,19 @@ sequence.push(1);
p1.then(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected to be called first.');
}).catch($DONE);
Promise.allSettled([p1, p2]).then(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected to be called third.');
}).then($DONE, $DONE);
p2.then(function() {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected to be called second.');
}).catch($DONE);

View File

@ -37,14 +37,17 @@ sequence.push(1);
Promise.any([p1]).then((resolved) => {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected Promise.any().then to queue second');
}).catch($DONE);
p1.then(() => {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected p1.then to queue first');
}).then(() => {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected final then to queue last');
}).then($DONE, $DONE);

View File

@ -44,16 +44,19 @@ sequence.push(1);
p1.catch(() => {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected to be called first.');
}).catch($DONE);
Promise.any([p1, p2, p3]).then(() => {
sequence.push(6);
assert.sameValue(sequence.length, 6);
checkSequence(sequence, 'Expected to be called fourth.');
}).then($DONE, $DONE);
p2.then(() => {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected to be called second.');
}).catch($DONE);
@ -61,5 +64,6 @@ sequence.push(2);
p3.catch(() => {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected to be called third.');
}).catch($DONE);

View File

@ -40,11 +40,13 @@ sequence.push(1);
p1.catch(() => {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected to be called first.');
}).catch($DONE);
Promise.any([p1, p2]).then(() => {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected to be called third.');
}).then($DONE, outcome => {
assert(outcome instanceof AggregateError);
@ -55,6 +57,7 @@ Promise.any([p1, p2]).then(() => {
p2.catch(() => {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected to be called second.');
}).catch($DONE);

View File

@ -35,16 +35,19 @@ sequence.push(1);
p1.then(function() {
sequence.push(3);
checkSequence(sequence, 'Expected to be called first.');
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected to be called first.');
}).catch($DONE);
Promise.any([p1, p2]).then(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected to be called third.');
}).then($DONE, $DONE);
p2.then(function() {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected to be called second.');
}).catch($DONE);

View File

@ -34,6 +34,6 @@ no.catch(function(e) {
sequence.push(5);
assert.sameValue(e, noReason);
}).then(function() {
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "All expected callbacks called in correct order");
$DONE();
}).catch($ERROR);
}).then($DONE, $DONE);

View File

@ -24,6 +24,6 @@ p.finally(function() {
sequence.push(2);
assert.sameValue(reason, original, 'onFinally can not override the rejection value by returning');
}).then(function() {
assert.sameValue(sequence.length, 2);
checkSequence(sequence, "All expected callbacks called in correct order");
$DONE();
}).catch($ERROR);
}).then($DONE, $DONE);

View File

@ -24,6 +24,6 @@ p.finally(function() {
sequence.push(2);
assert.sameValue(reason, thrown, 'onFinally can override the rejection reason by throwing');
}).then(function() {
assert.sameValue(sequence.length, 2);
checkSequence(sequence, "All expected callbacks called in correct order");
$DONE();
}).catch($ERROR);
}).then($DONE, $DONE);

View File

@ -20,6 +20,6 @@ p.finally(function() {
sequence.push(2);
assert.sameValue(x, obj, 'onFinally can not override the resolution value');
}).then(function() {
assert.sameValue(sequence.length, 2);
checkSequence(sequence, "All expected callbacks called in correct order");
$DONE();
}).catch($ERROR);
}).then($DONE, $DONE);

View File

@ -34,6 +34,7 @@ yes.then(function(x) {
sequence.push(5);
assert.sameValue(e, noReason);
}).then(function() {
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "All expected callbacks called in correct order");
$DONE();
}).catch($ERROR);

View File

@ -34,6 +34,7 @@ p.then(function() {
}).then(function() {
sequence.push(8);
}).then(function() {
assert.sameValue(sequence.length, 8);
checkSequence(sequence, "Sequence should be as expected");
}).then($DONE, $DONE);

View File

@ -20,7 +20,8 @@ var resolveP1, rejectP2, sequence = [];
})).then(function(msg) {
sequence.push(msg);
}).then(function() {
checkSequence(sequence, "Expected 1,2,3");
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Expected 1,2,3");
}).then($DONE, $DONE);
(new Promise(function(resolve, reject) {

View File

@ -33,6 +33,7 @@ p1.then(function(msg) {
p2.catch(function(msg) {
sequence.push(msg);
}).then(function() {
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Expected 1,2,3");
}).then($DONE, $DONE);

View File

@ -34,8 +34,9 @@ Promise.resolve().then(function() {
p2.catch(function(msg) {
sequence.push(msg);
}).then(function() {
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Expected 1,2,3");
}).then($DONE, $DONE);
});
}).then($DONE, $DONE);
sequence.push(1);

View File

@ -22,6 +22,7 @@ sequence.push(1);
p.then(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Should be second");
}).catch($DONE);
@ -29,10 +30,12 @@ Promise.resolve().then(function() {
// enqueue another then-handler
p.then(function() {
sequence.push(4);
checkSequence(sequence, "Should be third");
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "Should be third");
}).then($DONE, $DONE);
sequence.push(2);
assert.sameValue(sequence.length, 2);
checkSequence(sequence, "Should be first");
pResolve();

View File

@ -24,18 +24,21 @@ pResolve();
p.then(function() {
sequence.push(3);
checkSequence(sequence, "Should be first");
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Should be first");
}).catch($DONE);
Promise.resolve().then(function() {
// enqueue another then-handler
p.then(function() {
sequence.push(5);
checkSequence(sequence, "Should be third");
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "Should be third");
}).then($DONE, $DONE);
sequence.push(4);
checkSequence(sequence, "Should be second");
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "Should be second");
}).catch($DONE);
sequence.push(2);

View File

@ -26,6 +26,7 @@ p.then(function() {
$ERROR("Should not be called -- Promise rejected.");
}, function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Should be first");
}).catch($DONE);
@ -35,10 +36,12 @@ Promise.resolve().then(function() {
$ERROR("Should not be called (2) -- Promise rejected.");
}, function() {
sequence.push(5);
checkSequence(sequence, "Should be third");
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "Should be third");
}).then($DONE, $DONE);
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "Should be second");
}).catch($DONE);

View File

@ -17,14 +17,17 @@ sequence.push(1);
p.then(function() {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "This happens second");
}).catch($DONE);
Promise.resolve().then(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "This happens first");
}).then(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "This happens third");
}).then($DONE, $DONE);

View File

@ -20,14 +20,17 @@ p.then(function() {
$ERROR("Should not fulfill.");
}, function() {
sequence.push(4);
checkSequence(sequence, "This happens second");
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "This happens second");
}).catch($DONE);
Promise.resolve().then(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "This happens first");
}).then(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "This happens third");
}).then($DONE, $DONE);

View File

@ -23,14 +23,17 @@ p.then(function(arg) {
}
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "This happens second");
}).catch($DONE);
Promise.resolve().then(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "This happens first");
}).then(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "This happens third");
}).then($DONE, $DONE);

View File

@ -23,14 +23,17 @@ p.then(function(arg) {
}
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "This happens second");
}).catch($DONE);
Promise.resolve().then(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "This happens first");
}).then(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "This happens third");
}).then($DONE, $DONE);

View File

@ -23,14 +23,17 @@ p.then(function(arg) {
}
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "This happens second");
}).catch($DONE);
Promise.resolve().then(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "This happens first");
}).then(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "This happens third");
}).then($DONE, $DONE);

View File

@ -25,14 +25,17 @@ p.then(function() {
}
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "This happens second");
}).catch($DONE);
Promise.resolve().then(function() {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "This happens first");
}).then(function() {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "This happens third");
}).then($DONE, $DONE);

View File

@ -25,12 +25,12 @@ includes: [promiseHelper.js]
---*/
let callCount = 0;
let values = [];
let sequence = [];
function Constructor(executor) {
function reject(value) {
callCount += 1;
values.push(value);
sequence.push(value);
}
executor(() => {
throw new Test262Error();
@ -59,4 +59,5 @@ pReject(2);
pReject(3);
assert.sameValue(callCount, 3, 'callCount after resolving a');
checkSequence(values);
assert.sameValue(sequence.length, 3);
checkSequence(sequence);

View File

@ -25,12 +25,12 @@ includes: [promiseHelper.js]
---*/
let callCount = 0;
let values = [];
let sequence = [];
function Constructor(executor) {
function resolve(value) {
callCount += 1;
values.push(value);
sequence.push(value);
}
executor(resolve, $ERROR);
}
@ -56,4 +56,5 @@ pResolve(2);
pResolve(3);
assert.sameValue(callCount, 3, 'callCount after resolving a');
checkSequence(values);
assert.sameValue(sequence.length, 3);
checkSequence(sequence);

View File

@ -30,18 +30,23 @@ let sequence = [1];
Promise.all([
a.catch(() => {
sequence.push(3);
assert.sameValue(sequence.length, 3);
return checkSequence(sequence, 'Expected to be called first.');
}),
Promise.race([a, b]).catch(() => {
sequence.push(5);
assert.sameValue(sequence.length, 5);
return checkSequence(sequence, 'Expected to be called third.');
}),
b.catch(() => {
sequence.push(4);
assert.sameValue(sequence.length, 4);
return checkSequence(sequence, 'Expected to be called second.');
})
]).then(result => {
compareArray(result, [true, true, true]);
assert.sameValue(sequence.length, 5);
checkSequence(sequence);
}).then($DONE, $DONE);
sequence.push(2);

View File

@ -30,17 +30,22 @@ let sequence = [1];
Promise.all([
a.then(() => {
sequence.push(3);
assert.sameValue(sequence.length, 3);
return checkSequence(sequence, 'Expected to be called first.');
}),
Promise.race([a, b]).then(() => {
sequence.push(5);
assert.sameValue(sequence.length, 5);
return checkSequence(sequence, 'Expected to be called third.');
}),
b.then(() => {
sequence.push(4);
assert.sameValue(sequence.length, 4);
return checkSequence(sequence, 'Expected to be called second.');
})
]).then(result => {
compareArray(result, [true, true, true]);
assert.sameValue(sequence.length, 5);
checkSequence(sequence)
}).then($DONE, $DONE);
sequence.push(2);

View File

@ -16,6 +16,7 @@ var sequence = [];
var thenable = {
then: function(onResolve, onReject) {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "thenable.then called");
assert.sameValue(this, thenable);
@ -23,25 +24,30 @@ var thenable = {
onResolve('resolved');
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "after resolved");
throw new Error('interrupt flow');
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "duplicate sequence point not pushed");
}
};
sequence.push(1);
assert.sameValue(sequence.length, 1);
checkSequence(sequence, "no async calls yet");
var p1 = Promise.resolve(thenable);
sequence.push(2);
assert.sameValue(sequence.length, 2);
checkSequence(sequence, "thenable.then queued but not yet called");
p1.then(function(q) {
sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "all done");
assert.sameValue(q, 'resolved');

View File

@ -17,6 +17,7 @@ var thenable = {
then: function(onResolve, onReject) {
sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "thenable.then called");
assert.sameValue(this, thenable, "thenable.then called with `thenable` as `this`");
@ -26,15 +27,18 @@ var thenable = {
};
sequence.push(1);
assert.sameValue(sequence.length, 1);
checkSequence(sequence, "no async calls yet");
var p = Promise.resolve(thenable);
sequence.push(2);
assert.sameValue(sequence.length, 2);
checkSequence(sequence, "thenable.then queued but not yet called");
p.then(function(r) {
sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "all done");
assert.sameValue(r, 'resolved');