Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handlerSpy support to SNS #64

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/sns/lib/sns/AbstractSnsPublisherMonoSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export abstract class AbstractSnsPublisherMonoSchema<MessagePayloadType extends
} satisfies PublishCommandInput
const command = new PublishCommand(input)
await this.snsClient.send(command)
this.handleMessageProcessed(message, 'published')
} catch (error) {
this.handleError(error)
throw error
Expand Down
1 change: 1 addition & 0 deletions packages/sns/lib/sns/AbstractSnsPublisherMultiSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export abstract class AbstractSnsPublisherMultiSchema<MessagePayloadType extends
} satisfies PublishCommandInput
const command = new PublishCommand(input)
await this.snsClient.send(command)
this.handleMessageProcessed(message, 'published')
} catch (error) {
this.handleError(error)
throw error
Expand Down
1 change: 1 addition & 0 deletions packages/sns/lib/utils/snsMessageDeserializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { deserializeSNSMessage } from './snsMessageDeserializer'
describe('messageDeserializer', () => {
it('deserializes valid JSON', () => {
const messagePayload: PERMISSIONS_MESSAGE_TYPE = {
id: '1',
messageType: 'add',
userIds: [1],
permissions: ['perm'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class SnsSqsPermissionConsumerMonoSchema extends AbstractSnsSqsConsumerMo
) {
super(dependencies, {
messageSchema: PERMISSIONS_MESSAGE_SCHEMA,
handlerSpy: true,
messageTypeField: 'messageType',
deletionConfig: {
deleteIfExists: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class SnsSqsPermissionConsumerMultiSchema extends AbstractSnsSqsConsumerM
super(
dependencies,
{
handlerSpy: true,
handlers: new MessageHandlerConfigBuilder<SupportedEvents, ExecutionContext>()
.addConfig(
PERMISSIONS_ADD_MESSAGE_SCHEMA,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ describe('SNS PermissionsConsumer', () => {
userPermissionMap[300] = []

await publisher.publish({
id: '1',
messageType: 'add',
userIds,
permissions: perms,
Expand All @@ -186,6 +187,7 @@ describe('SNS PermissionsConsumer', () => {
expect(users).toHaveLength(0)

await publisher.publish({
id: '2',
messageType: 'add',
userIds,
permissions: perms,
Expand Down Expand Up @@ -216,6 +218,7 @@ describe('SNS PermissionsConsumer', () => {
userPermissionMap[100] = []

await publisher.publish({
id: '3',
messageType: 'add',
userIds,
permissions: perms,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { SNSClient } from '@aws-sdk/client-sns'
import type { SQSClient } from '@aws-sdk/client-sqs'
import { waitAndRetry } from '@message-queue-toolkit/core'
import { assertQueue } from '@message-queue-toolkit/sqs'
import type { AwilixContainer } from 'awilix'
import { describe, beforeEach, afterEach, expect, it, beforeAll } from 'vitest'
Expand Down Expand Up @@ -90,23 +89,21 @@ describe('SNS PermissionsConsumerMultiSchema', () => {
describe('happy path', () => {
it('Processes messages', async () => {
await publisher.publish({
id: '1',
messageType: 'add',
})
await publisher.publish({
id: '2',
messageType: 'remove',
})
await publisher.publish({
id: '3',
messageType: 'remove',
})

await waitAndRetry(
() => {
return consumer.addCounter === 1 && consumer.removeCounter === 2
},
// Removing this makes test flaky
30,
20,
)
await consumer.handlerSpy.waitForMessageWithId('1', 'consumed')
await consumer.handlerSpy.waitForMessageWithId('2', 'consumed')
await consumer.handlerSpy.waitForMessageWithId('3', 'consumed')

expect(consumer.addBarrierCounter).toBe(3)
expect(consumer.addCounter).toBe(1)
Expand Down
3 changes: 3 additions & 0 deletions packages/sns/test/consumers/userConsumerSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import z from 'zod'

export const PERMISSIONS_MESSAGE_SCHEMA = z.object({
id: z.string(),
messageType: z.enum(['add', 'remove']),
userIds: z.array(z.number()).describe('User IDs'),
permissions: z.array(z.string()).nonempty().describe('List of user permissions'),
})

export const PERMISSIONS_ADD_MESSAGE_SCHEMA = z.object({
id: z.string(),
messageType: z.literal('add'),
})

export const PERMISSIONS_REMOVE_MESSAGE_SCHEMA = z.object({
id: z.string(),
messageType: z.literal('remove'),
})

Expand Down
2 changes: 2 additions & 0 deletions packages/sns/test/publishers/SnsPermissionPublisher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ describe('SNSPermissionPublisher', () => {
const { permissionPublisher } = diContainer.cradle

const message = {
id: '1',
userIds,
messageType: 'add',
permissions: perms,
Expand Down Expand Up @@ -158,6 +159,7 @@ describe('SNSPermissionPublisher', () => {
})

expect(receivedMessage).toEqual({
id: '1',
messageType: 'add',
permissions: ['perm1', 'perm2'],
userIds: [100, 200, 300],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class SnsPermissionPublisherMonoSchema extends AbstractSnsPublisherMonoSc
deletionConfig: {
deleteIfExists: false,
},
handlerSpy: true,
messageSchema: PERMISSIONS_MESSAGE_SCHEMA,
messageTypeField: 'messageType',
...options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class SnsPermissionPublisherMultiSchema extends AbstractSnsPublisherMulti
deleteIfExists: false,
},
messageSchemas: [PERMISSIONS_ADD_MESSAGE_SCHEMA, PERMISSIONS_REMOVE_MESSAGE_SCHEMA],
handlerSpy: true,
messageTypeField: 'messageType',
...options,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/sqs/lib/sqs/AbstractSqsConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ export abstract class AbstractSqsConsumer<
const deserializedMessage = this.deserializeMessage(message)
if (deserializedMessage.error === 'abort') {
await this.failProcessing(message)
const messageId = this.tryToExtractId(message)

const messageId = this.tryToExtractId(message)
this.handleMessageProcessed(null, 'invalid_message', messageId.result)
return
}
Expand Down
Loading