|
1 | 1 | import {
|
| 2 | + SqsConsumerEvents, |
2 | 3 | SqsProducer,
|
3 | 4 | SqsConsumer,
|
4 | 5 | SqsProducerOptions,
|
@@ -93,28 +94,51 @@ async function publishMessage(msg: any, options: Partial<SnsProducerOptions> = {
|
93 | 94 | await snsProducer.publishJSON(msg);
|
94 | 95 | }
|
95 | 96 |
|
96 |
| -async function receiveMessages(expectedMsgsCount: number, options: Partial<SqsConsumerOptions> = {}): Promise<any> { |
| 97 | +async function receiveMessages( |
| 98 | + expectedMsgsCount: number, |
| 99 | + options: Partial<SqsConsumerOptions> = {}, |
| 100 | + eventHandlers?: Record<string | symbol, (...args) => void> |
| 101 | +): Promise<any> { |
97 | 102 | const { s3 } = getClients();
|
98 |
| - return new Promise((res, rej) => { |
| 103 | + return new Promise((resolve, rej) => { |
99 | 104 | const messages = [];
|
100 | 105 | let timeoutId;
|
101 | 106 |
|
102 | 107 | const sqsConsumer = SqsConsumer.create({
|
103 | 108 | queueUrl: TEST_QUEUE_URL,
|
104 | 109 | region: TEST_REGION,
|
105 | 110 | parsePayload: (raw) => JSON.parse(raw),
|
106 |
| - handleMessage: async ({ payload }) => { |
107 |
| - messages.push(payload); |
108 |
| - if (messages.length === expectedMsgsCount) { |
109 |
| - sqsConsumer.stop(); |
110 |
| - clearTimeout(timeoutId); |
111 |
| - res(messages); |
112 |
| - } |
113 |
| - }, |
114 | 111 | ...options,
|
115 | 112 | s3,
|
116 | 113 | });
|
117 | 114 |
|
| 115 | + sqsConsumer.on(SqsConsumerEvents.messageParsed, ({ payload }) => { |
| 116 | + messages.push(payload); |
| 117 | + }); |
| 118 | + |
| 119 | + sqsConsumer.on(SqsConsumerEvents.messageProcessed, () => { |
| 120 | + if (messages.length === expectedMsgsCount) { |
| 121 | + sqsConsumer.stop(); |
| 122 | + clearTimeout(timeoutId); |
| 123 | + resolve(messages); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + sqsConsumer.on(SqsConsumerEvents.error, () => { |
| 128 | + sqsConsumer.stop(); |
| 129 | + clearTimeout(timeoutId); |
| 130 | + }); |
| 131 | + |
| 132 | + sqsConsumer.on(SqsConsumerEvents.processingError, () => { |
| 133 | + sqsConsumer.stop(); |
| 134 | + clearTimeout(timeoutId); |
| 135 | + resolve(); |
| 136 | + }); |
| 137 | + |
| 138 | + if (eventHandlers) { |
| 139 | + Object.entries(eventHandlers).forEach(([event, handler]) => sqsConsumer.on(event, handler)); |
| 140 | + } |
| 141 | + |
118 | 142 | timeoutId = setTimeout(() => {
|
119 | 143 | rej(new Error("Timeout: SqsConsumer didn't get any messages for 5 seconds."));
|
120 | 144 | sqsConsumer.stop();
|
@@ -153,6 +177,61 @@ describe('sns-sqs-big-payload', () => {
|
153 | 177 | });
|
154 | 178 | });
|
155 | 179 |
|
| 180 | + describe('events', () => { |
| 181 | + function getEventHandlers() { |
| 182 | + const handlers = Object.keys(SqsConsumerEvents).reduce((acc, key) => { |
| 183 | + acc[SqsConsumerEvents[key]] = jest.fn(); |
| 184 | + return acc; |
| 185 | + }, {}); |
| 186 | + return handlers; |
| 187 | + } |
| 188 | + |
| 189 | + it('should trigger success events event', async () => { |
| 190 | + const message = { it: 'works' }; |
| 191 | + const handlers = getEventHandlers(); |
| 192 | + sendMessage(message); |
| 193 | + const [receivedMessage] = await receiveMessages(1, {}, handlers); |
| 194 | + expect(receivedMessage).toEqual(message); |
| 195 | + |
| 196 | + // success |
| 197 | + expect(handlers[SqsConsumerEvents.started]).toBeCalled(); |
| 198 | + expect(handlers[SqsConsumerEvents.messageReceived]).toBeCalled(); |
| 199 | + expect(handlers[SqsConsumerEvents.messageParsed]).toBeCalled(); |
| 200 | + expect(handlers[SqsConsumerEvents.messageProcessed]).toBeCalled(); |
| 201 | + expect(handlers[SqsConsumerEvents.stopped]).toBeCalled(); |
| 202 | + // errors |
| 203 | + expect(handlers[SqsConsumerEvents.error]).not.toBeCalled(); |
| 204 | + expect(handlers[SqsConsumerEvents.processingError]).not.toBeCalled(); |
| 205 | + expect(handlers[SqsConsumerEvents.payloadParseError]).not.toBeCalled(); |
| 206 | + expect(handlers[SqsConsumerEvents.s3PayloadError]).not.toBeCalled(); |
| 207 | + expect(handlers[SqsConsumerEvents.connectionError]).not.toBeCalled(); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should should trigger processingError event', async () => { |
| 211 | + const message = { it: 'works' }; |
| 212 | + const handlers = getEventHandlers(); |
| 213 | + sendMessage(message); |
| 214 | + await receiveMessages( |
| 215 | + 1, |
| 216 | + { |
| 217 | + handleMessage: () => { |
| 218 | + throw new Error('Processing error'); |
| 219 | + }, |
| 220 | + }, |
| 221 | + handlers |
| 222 | + ); |
| 223 | + |
| 224 | + // errors |
| 225 | + expect(handlers[SqsConsumerEvents.messageReceived]).toBeCalled(); |
| 226 | + expect(handlers[SqsConsumerEvents.messageParsed]).toBeCalled(); |
| 227 | + expect(handlers[SqsConsumerEvents.processingError]).toBeCalled(); |
| 228 | + expect(handlers[SqsConsumerEvents.messageProcessed]).not.toBeCalled(); |
| 229 | + expect(handlers[SqsConsumerEvents.error]).not.toBeCalled(); |
| 230 | + expect(handlers[SqsConsumerEvents.connectionError]).not.toBeCalled(); |
| 231 | + expect(handlers[SqsConsumerEvents.payloadParseError]).not.toBeCalled(); |
| 232 | + }); |
| 233 | + }); |
| 234 | + |
156 | 235 | describe('sending message through s3', () => {
|
157 | 236 | it('should send all message though s3 if configured', async () => {
|
158 | 237 | const message = { it: 'works' };
|
|
0 commit comments