From 76c2b9f639f6439294820a6691efe6fd86bc5196 Mon Sep 17 00:00:00 2001 From: Dave Gramlich Date: Fri, 25 Jan 2019 21:49:35 -0800 Subject: [PATCH] fix(sample): factor setTimeout jitter into assertion (#449) --- samples/system-test/topics.test.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/samples/system-test/topics.test.js b/samples/system-test/topics.test.js index 32549c63b..edd9a821f 100644 --- a/samples/system-test/topics.test.js +++ b/samples/system-test/topics.test.js @@ -155,7 +155,7 @@ describe('topics', () => { }); it('should publish with specific batch settings', async () => { - const expectedWait = 1000; + const waitTime = 1000; const [subscription] = await pubsub .topic(topicNameOne) .subscription(subscriptionNameThree) @@ -164,12 +164,18 @@ describe('topics', () => { await exec( `${cmd} publish-batch ${topicNameOne} "${ expectedMessage.data - }" -w ${expectedWait}` + }" -w ${waitTime}` ); const receivedMessage = await _pullOneMessage(subscription); + const publishTime = Date.parse(receivedMessage.publishTime); + const actualWait = publishTime - startTime; + // setTimeout isn't so reliable to publish messages EXACTLY at 1000ms, + // so we should consider anything above 900 as passing. + const expectedWait = waitTime - 100; + assert.strictEqual(receivedMessage.data.toString(), expectedMessage.data); - assert.strictEqual(publishTime - startTime > expectedWait, true); + assert(actualWait >= expectedWait); }); it('should publish with retry settings', async () => {