Skip to content

Commit 6436b3f

Browse files
committed
Expand example
1 parent 5ad2b18 commit 6436b3f

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

examples/sns-sqs/lib/01-publish-message.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ describe('Publish message', () => {
1414
})
1515

1616
it('Publishes a message', async () => {
17+
await publisherManager.publish(UserConsumer.SUBSCRIBED_TOPIC_NAME, {
18+
type: 'user.created',
19+
payload: {
20+
id: '456',
21+
name: 'Jane Doe',
22+
},
23+
})
24+
1725
await publisherManager.publish(UserConsumer.SUBSCRIBED_TOPIC_NAME, {
1826
type: 'user.created',
1927
payload: {
@@ -24,6 +32,9 @@ describe('Publish message', () => {
2432

2533
const receivedMessage = await userConsumer.handlerSpy.waitForMessage({
2634
type: 'user.created',
35+
payload: {
36+
name: 'John Doe',
37+
},
2738
})
2839

2940
expect(receivedMessage.message.payload).toMatchInlineSnapshot(`
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { SnsConsumerErrorResolver } from '@message-queue-toolkit/sns'
2+
import { beforeAll, describe, it } from 'vitest'
3+
import {
4+
errorReporter,
5+
logger,
6+
snsClient,
7+
sqsClient,
8+
stsClient,
9+
transactionObservabilityManager,
10+
} from './common/Dependencies.ts'
11+
import { publisherManager } from './common/TestPublisherManager.ts'
12+
import { UserConsumer } from './common/UserConsumer.js'
13+
14+
// This test suite illustrates the importance of only initting the consumers you need
15+
// to prevent test execution time from increasing with every new consumer added
16+
describe('Consumer init', () => {
17+
beforeAll(async () => {
18+
await publisherManager.initRegisteredPublishers([UserConsumer.SUBSCRIBED_TOPIC_NAME])
19+
})
20+
21+
it('Inits one consumer', async () => {
22+
const userConsumer = new UserConsumer({
23+
errorReporter,
24+
logger,
25+
transactionObservabilityManager,
26+
consumerErrorResolver: new SnsConsumerErrorResolver(),
27+
sqsClient,
28+
snsClient,
29+
stsClient,
30+
})
31+
32+
await userConsumer.start()
33+
await userConsumer.close()
34+
})
35+
36+
it('Inits twenty consumers', async () => {
37+
const consumers: UserConsumer[] = []
38+
for (let i = 0; i < 20; i++) {
39+
consumers.push(
40+
new UserConsumer({
41+
errorReporter,
42+
logger,
43+
transactionObservabilityManager,
44+
consumerErrorResolver: new SnsConsumerErrorResolver(),
45+
sqsClient,
46+
snsClient,
47+
stsClient,
48+
}),
49+
)
50+
}
51+
52+
for (const userConsumer of consumers) {
53+
await userConsumer.start()
54+
}
55+
56+
for (const userConsumer of consumers) {
57+
await userConsumer.close()
58+
}
59+
})
60+
})

examples/sns-sqs/lib/common/UserConsumer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type SupportedMessages = ConsumerMessageSchema<
1212
// biome-ignore lint/complexity/noBannedTypes: to be expanded later
1313
type ExecutionContext = {}
1414

15+
const isTest = true
16+
1517
export class UserConsumer extends AbstractSnsSqsConsumer<SupportedMessages, ExecutionContext> {
1618
public static readonly CONSUMED_QUEUE_NAME = 'user-my_service'
1719
public static readonly SUBSCRIBED_TOPIC_NAME = 'user'
@@ -32,6 +34,9 @@ export class UserConsumer extends AbstractSnsSqsConsumer<SupportedMessages, Exec
3234
QueueName: UserConsumer.CONSUMED_QUEUE_NAME,
3335
},
3436
},
37+
deletionConfig: {
38+
deleteIfExists: isTest,
39+
},
3540
locatorConfig: {
3641
// Topic is created by a publisher, consumer relies on it already existing.
3742
// Note that in order for this to work correctly you need to ensure that

0 commit comments

Comments
 (0)