Skip to content

Commit

Permalink
fix(sample): factor setTimeout jitter into assertion (googleapis#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
callmehiphop authored and Praveen Kumar Singh committed Jan 30, 2019
1 parent 4c409b1 commit 76c2b9f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions samples/system-test/topics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 () => {
Expand Down

0 comments on commit 76c2b9f

Please sign in to comment.