From 74a3b1265925d2efc7518b43ef50400331be493c Mon Sep 17 00:00:00 2001 From: Dominick Martelly <97986187+martdo02@users.noreply.github.com> Date: Wed, 11 Sep 2024 11:03:00 -0400 Subject: [PATCH 1/3] feat: Added new sqs step difition to check the aproximite number of messages --- .github/workflows/Test.yml | 2 +- packages/aws/features/sqs.feature | 12 +++++------- packages/aws/package.json | 5 ++--- packages/aws/stepDefinitions/sqs.js | 27 ++++++++++++++++++++++----- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 27e6473..4212dbc 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -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 diff --git a/packages/aws/features/sqs.feature b/packages/aws/features/sqs.feature index 711308b..ffffc10 100644 --- a/packages/aws/features/sqs.feature +++ b/packages/aws/features/sqs.feature @@ -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!" @@ -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: """ @@ -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 diff --git a/packages/aws/package.json b/packages/aws/package.json index ccb0487..443553b 100644 --- a/packages/aws/package.json +++ b/packages/aws/package.json @@ -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": { @@ -36,6 +36,5 @@ "eslint-plugin-promise": "^6.0.0", "multiple-cucumber-html-reporter": "3.5.0", "nyc": "^15.1.0" - }, - "gitHead": "c1cb2220ce18a8e3ceee4b375950a0beec4f3a6f" + } } diff --git a/packages/aws/stepDefinitions/sqs.js b/packages/aws/stepDefinitions/sqs.js index eed355d..3d20c1f 100644 --- a/packages/aws/stepDefinitions/sqs.js +++ b/packages/aws/stepDefinitions/sqs.js @@ -78,13 +78,19 @@ 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 { @@ -92,8 +98,19 @@ MAFWhen('queue {string} is empty within {int} seconds', async function (queueNam if (queueAttributes.ApproximateNumberOfMessages === '0') { 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') +} + +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) }) /** From 0362878bf72d0892d6693cfc99dbba1f63c69ff1 Mon Sep 17 00:00:00 2001 From: Dominick Martelly <97986187+martdo02@users.noreply.github.com> Date: Wed, 11 Sep 2024 11:07:53 -0400 Subject: [PATCH 2/3] chore: updated readme w/ SQS step definition --- packages/aws/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/aws/README.md b/packages/aws/README.md index bf28917..9ca2e2d 100644 --- a/packages/aws/README.md +++ b/packages/aws/README.md @@ -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. From ddf76c342dc059ba0387eb9bf4a5180783516799 Mon Sep 17 00:00:00 2001 From: Dominick Martelly <97986187+martdo02@users.noreply.github.com> Date: Wed, 11 Sep 2024 11:10:17 -0400 Subject: [PATCH 3/3] chore: fix message count bug --- packages/aws/stepDefinitions/sqs.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/aws/stepDefinitions/sqs.js b/packages/aws/stepDefinitions/sqs.js index 3d20c1f..60b1299 100644 --- a/packages/aws/stepDefinitions/sqs.js +++ b/packages/aws/stepDefinitions/sqs.js @@ -95,12 +95,12 @@ async function waitUntilQueueHasCount (queueName, messageCount, timeout) { 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) {