Skip to content

Commit

Permalink
feat: support FIFO queues with the SQS sendMessage helper
Browse files Browse the repository at this point in the history
  • Loading branch information
blefebvre committed Oct 31, 2024
1 parent f299dff commit dc5085b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/spacecat-shared-utils/src/sqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SQS {
* @param {string} messageGroupId - (Optional) The message group ID for FIFO queues.
* @return {Promise<void>}
*/
async sendMessage(queueUrl, message, messageGroupId) {
async sendMessage(queueUrl, message, messageGroupId = undefined) {
const body = {
...message,
timestamp: new Date().toISOString(),
Expand Down
61 changes: 57 additions & 4 deletions packages/spacecat-shared-utils/test/sqs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/* eslint-env mocha */

import { SQSClient } from '@aws-sdk/client-sqs';
import wrap from '@adobe/helix-shared-wrap';
import sinon from 'sinon';
import { expect, use } from 'chai';
Expand Down Expand Up @@ -105,8 +106,30 @@ describe('SQS', () => {
});

describe('SQS helpers', () => {
const exampleHandler = sinon.spy(async (message, context) => {
const { log } = context;
let sqsClientStub;
let sendStub;
let context;

beforeEach(() => {
// Stub the AWS SQS client so we can inspect the arguments we send it
sqsClientStub = sandbox.createStubInstance(SQSClient);
sendStub = sandbox.stub().callsFake(() => ({ MessageId: '12345' }));
sandbox.stub(SQSClient.prototype, 'constructor').callsFake(() => sqsClientStub);
sandbox.stub(SQSClient.prototype, 'send').callsFake(sendStub);
context = {
log: console,
runtime: {
region: 'us-east-1',
},
};
});

afterEach('clean', () => {
sandbox.restore();
});

const exampleHandler = sandbox.spy(async (message, ctx) => {
const { log } = ctx;
const messageStr = JSON.stringify(message);
log.info(`Handling message ${messageStr}`);
return new Response(messageStr);
Expand All @@ -127,7 +150,7 @@ describe('SQS', () => {
});

it('should handle a valid context with an event record', async () => {
const context = {
const ctx = {
log: console,
invocation: {
event: {
Expand All @@ -142,12 +165,42 @@ describe('SQS', () => {
};

const handler = sqsEventAdapter(exampleHandler);
const response = await handler(emptyRequest, context);
const response = await handler(emptyRequest, ctx);

expect(response.status).to.equal(200);
const result = await response.json();
expect(result.id).to.equal('1234567890');
expect(exampleHandler.calledWith({ id: '1234567890' })).to.be.true;
});

it('should not include a MessageGroupId when one is not provided', async () => {
const action = wrap(async (req, ctx) => {
await ctx.sqs.sendMessage('queue-url', { key: 'value' });
}).with(sqsWrapper);

await action({}, context);

const firstSendArg = sendStub.getCall(0).args[0];
expect(Object.keys(firstSendArg.input)).to.deep.equal([
'MessageBody',
'QueueUrl',
]);
});

it('should include a MessageGroupId when provided', async () => {
const action = wrap(async (req, ctx) => {
await ctx.sqs.sendMessage('queue-url', { key: 'value' }, 'job-id');
}).with(sqsWrapper);

await action({}, context);

const firstSendArg = sendStub.getCall(0).args[0];
expect(Object.keys(firstSendArg.input)).to.deep.equal([
'MessageBody',
'QueueUrl',
'MessageGroupId',
]);
expect(firstSendArg.input.MessageGroupId).to.equal('job-id');
});
});
});

0 comments on commit dc5085b

Please sign in to comment.