-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Await final poll for graceful shutdown
- Loading branch information
Showing
7 changed files
with
105 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Feature: Graceful shutdown | ||
|
||
Scenario: Several messages in flight | ||
Given Several messages are sent to the SQS queue | ||
Then the application is stopped while messages are in flight | ||
Then the in-flight messages should be processed before stopped is emitted |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const { Given, Then, After } = require('@cucumber/cucumber'); | ||
const assert = require('assert'); | ||
const { PurgeQueueCommand } = require('@aws-sdk/client-sqs'); | ||
const pEvent = require('p-event'); | ||
|
||
const { consumer } = require('../utils/consumer/gracefulShutdown'); | ||
const { producer } = require('../utils/producer'); | ||
const { sqs, QUEUE_URL } = require('../utils/sqs'); | ||
|
||
Given('Several messages are sent to the SQS queue', async () => { | ||
const params = { | ||
QueueUrl: QUEUE_URL | ||
}; | ||
const command = new PurgeQueueCommand(params); | ||
const response = await sqs.send(command); | ||
|
||
assert.strictEqual(response['$metadata'].httpStatusCode, 200); | ||
|
||
const size = await producer.queueSize(); | ||
assert.strictEqual(size, 0); | ||
|
||
await producer.send(['msg1', 'msg2', 'msg3']); | ||
|
||
const size2 = await producer.queueSize(); | ||
|
||
assert.strictEqual(size2, 3); | ||
}); | ||
|
||
Then('the application is stopped while messages are in flight', async () => { | ||
consumer.start(); | ||
|
||
consumer.stop({ waitForInFlightMessagesMs: 5000 }); | ||
|
||
assert.strictEqual(consumer.isRunning, false); | ||
}); | ||
|
||
Then( | ||
'the in-flight messages should be processed before stopped is emitted', | ||
async () => { | ||
let numProcessed = 0; | ||
consumer.on('message_processed', () => { | ||
numProcessed++; | ||
}); | ||
|
||
await pEvent(consumer, 'stopped'); | ||
|
||
assert.strictEqual(numProcessed, 3); | ||
|
||
const size = await producer.queueSize(); | ||
assert.strictEqual(size, 0); | ||
} | ||
); | ||
|
||
After(() => { | ||
return consumer.stop(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const { Consumer } = require('../../../../dist/consumer'); | ||
|
||
const { QUEUE_URL, sqs } = require('../sqs'); | ||
|
||
const consumer = Consumer.create({ | ||
queueUrl: QUEUE_URL, | ||
sqs, | ||
pollingWaitTimeMs: 1000, | ||
batchSize: 10, | ||
handleMessage: async (message) => { | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
return message; | ||
} | ||
}); | ||
|
||
exports.consumer = consumer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters