Skip to content

Commit

Permalink
Merge pull request #72 from hpcc-systems/aws-sqs-messages
Browse files Browse the repository at this point in the history
feat: Added new sqs step difition to check the aproximite number of messages
  • Loading branch information
martdo02 authored Sep 11, 2024
2 parents 718d106 + ddf76c3 commit 4fbd8a5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
with:
image-tag: '3.7.2'
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v4.0.3
with:
node-version: ${{ matrix.node-version }}
- name: Setup Terraform
Expand Down
4 changes: 4 additions & 0 deletions packages/aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ If a queue name is used, a regex search will be done to find the queue.

Checks if the queue is empty within the specified time. The string can be the url, or the queue name.

- `Then queue {string} has {int} messages within {int} seconds`

Checks if the queue has the specified number of messages within the specified time. The string can be the url, or the queue name.

- `When attributes of queue {string} are received`

Gets all attributes for a SQS queue to `lastRun`. The string can be the url, or the queue name.
Expand Down
12 changes: 5 additions & 7 deletions packages/aws/features/sqs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Feature: AWS: SQS Testing
Given queue "testQueueAlpha" exists on SQS
And "Hello there!" is written to file "./test/hello.txt"
When file "./test/hello.txt" is sent to queue "testQueueAlpha"
Then wait 5000 milliseconds
Then wait 2000 milliseconds
And the next message is received from queue "testQueueAlpha"
And item "lastRun" is equal to "Hello there!"

Expand All @@ -23,7 +23,7 @@ Feature: AWS: SQS Testing
When "Alpha" is sent to queue "testQueueBeta"
And "Beta" is sent to queue "testQueueBeta"
And "Charlie" is sent to queue "testQueueBeta"
Then wait 5000 milliseconds
Then wait 2000 milliseconds
And 3 messages are received from queue "testQueueBeta"
And item "lastRun" is equal to:
"""
Expand All @@ -47,13 +47,11 @@ Feature: AWS: SQS Testing
And attributes of queue "testQueueBeta" are received
And item "lastRun.ApproximateNumberOfMessages" is equal to "0"

Scenario: Test Purge Queue - Wait for Empty Queue
Scenario: Test Purge Queue - Wait for Message Count
Given queue "testQueueBeta" exists on SQS
And "qwe" is sent to queue "testQueueBeta"
And "asd" is sent to queue "testQueueBeta"
And "zxc" is sent to queue "testQueueBeta"
And wait 1000 milliseconds
And attributes of queue "testQueueBeta" are received
And item "lastRun.ApproximateNumberOfMessages" is equal to "3"
Then queue "testQueueBeta" has 3 messages within 15 seconds
When queue "testQueueBeta" is purged
Then queue "testQueueBeta" is empty within 10000 seconds
Then queue "testQueueBeta" has 0 messages within 15 seconds
5 changes: 2 additions & 3 deletions packages/aws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publishConfig": {
"access": "public"
},
"version": "2.3.0",
"version": "2.4.0",
"description": "AWS steps for MAF. This contains S3, DynamoDB, SQS, ECS, Cloudwatch, and Lambda stepDefinitions",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -36,6 +36,5 @@
"eslint-plugin-promise": "^6.0.0",
"multiple-cucumber-html-reporter": "3.5.0",
"nyc": "^15.1.0"
},
"gitHead": "c1cb2220ce18a8e3ceee4b375950a0beec4f3a6f"
}
}
31 changes: 24 additions & 7 deletions packages/aws/stepDefinitions/sqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,39 @@ MAFWhen('queue {string} exists on SQS', async function (queueName) {
})

/**
* Waits for a queue to be empty by checking the ApproximateNumberOfMessages attribute
* Waits for a queue to have a specific number of messages by checking the ApproximateNumberOfMessages attribute. Checks every 5 seconds.
* @param {String} queueName The name of the queue
* @param {int} timeout The maximum time to wait for the queue to be empty
* @returns true if the queue is empty within the timeout, otherwise throws an error
* @param {int} messageCount The number of messages to wait for
* @param {int} timeout The maximum time to wait for the queue to have the specified number of messages in seconds
* @throws An error if the queue is not empty within the timeout
*/
MAFWhen('queue {string} is empty within {int} seconds', async function (queueName, timeout) {
queueName = filltemplate(queueName, this.results)
async function waitUntilQueueHasCount (queueName, messageCount, timeout) {
if (messageCount < 0) {
throw new Error('Message count must be greater than or equal to 0')
}
if (timeout < 0) {
throw new Error('Timeout must be greater than or equal to 0')
}
const startTime = Date.now()
let queueAttributes
do {
queueAttributes = await attributesOfQueue(queueName)
if (queueAttributes.ApproximateNumberOfMessages === '0') {
if (queueAttributes.ApproximateNumberOfMessages === messageCount.toString()) {
return true
}
await new Promise(resolve => setTimeout(resolve, 5000))
} while (Date.now() - startTime < timeout * 1000)
throw new Error('Queue is not empty within ' + timeout + ' seconds')
throw new Error('Queue ' + queueName + ' did not have ' + messageCount + ' messages within ' + timeout + ' seconds. Current message count: ' + queueAttributes.ApproximateNumberOfMessages)
}

MAFWhen('queue {string} is empty within {int} second(s)', async function (queueName, timeout) {
queueName = filltemplate(queueName, this.results)
await waitUntilQueueHasCount(queueName, 0, timeout)
})

MAFWhen('queue {string} has {int} message(s) within {int} second(s)', async function (queueName, messageCount, timeout) {
queueName = filltemplate(queueName, this.results)
await waitUntilQueueHasCount(queueName, messageCount, timeout)
})

/**
Expand Down

0 comments on commit 4fbd8a5

Please sign in to comment.