Use Promise.all
Instead of wrongly relying on promises to resolve/reject in order of
creation
@@ -192,11 +192,13 @@ describe("circuit", function() {
retryPolicy: RetryPolicies.None,
});
+ const promises = [];
for (let i = 0; i < 10; i++) {
const mockApi = nock(mockAddress).get("/v1/authors").reply(200, { authors: [] });
- c.getAuthors({}, (err) => {
+ promises.push(c.getAuthors({}, (err, data) => {
if (i < 5) {
assert.equal(null, err, "Expected no error for first 5 client calls");
+ assert.deepEqual([], data.authors, "Expected data for first 5 client calls");
assert.equal(true, mockApi.isDone(), "Expected API call for first 5 client calls");
} else {
assert.equal(
@@ -204,15 +206,13 @@ describe("circuit", function() {
"Expected circuit breaker to short circuit last 5 client calls");
assert.equal(false, mockApi.isDone(), "Expected no API call for last 5 client calls");
}
- }).then(() => {
- // Mark the test as a success if we've made it to the last client call
- if (i === 9) {
- done();
+ }));
}
+
+ Promise.all(promises).then(() => {
+ done();
}).catch((err) => {
- // Mark the test as a failure if any assertion fails
done(err);
});
- }
});
});
