describe("#waitForTotalInvocations", () => { sharedListObjectsTest(spier => spier.waitForTotalInvocations(0)); it("should wait for total invocations", async () => { const spier = new S3Spier("bucket name", "function name", awsS3); await Promise.all([ spier.waitForTotalInvocations(2), delay(25).then(_ => { s3.listObjects.mockImplementation( yields({ Contents: [{ Key: "key" }, { Key: "key2" }] }) ); }) ]); }, 200); });
it('should wait if all tokens are exhausted when options.wait is true', async () => { const tokens = ['A']; const suppliedTokens = []; // Should give A await tokenDealer(tokens, (token, exhaust) => { suppliedTokens.push(token); return delay(50) .then(() => exhaust(Date.now() + 2500)); }, { lru }); expect(suppliedTokens).toEqual(['A']); // Should still give A, but wait 2.5sec const timeBefore = Date.now(); await tokenDealer(tokens, (token) => { suppliedTokens.push(token); }, { lru, wait: true }); expect(suppliedTokens).toEqual(['A', 'A']); expect(Date.now() - timeBefore).toBeGreaterThanOrEqual(2400, 3000); expect(Date.now() - timeBefore).toBeLessThanOrEqual(3000); });
// Demonstrates using res.write to send chunks of data to a user app.get( '/write', cache, (req, res) => { delay(1000) .then(() => { res.write('1') console.log('wrote chunk 1') }) .then(() => delay(1000)) .then(() => { res.write('2') console.log('wrote chunk 2') }) .then(() => delay(1000)) .then(() => { res.write('3') console.log('wrote chunk 3') }) .then(() => delay(1000)) .then(() => res.end()) .then(() => console.log('request complete')) } )
.then(() => { if (token === 'A') { exhaust(Date.now() + 2000, true);
describe("#waitForTotalInvocations", () => { sharedListObjectsTest(spier => spier.waitForTotalInvocations(0)); it("should wait for total invocations", async () => { const spier = new S3Spier("bucket name", "function name", awsS3); await Promise.all([ spier.waitForTotalInvocations(2), delay(25).then(_ => { s3.listObjects.mockImplementation( yields({ Contents: [{ Key: "key" }, { Key: "key2" }] }) ); }) ]); }, 200); });