-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rabbitmq): add graceful shutdown logic
Add message processing tracking so it can wait on them at shutdown. fix #688
- Loading branch information
Showing
4 changed files
with
131 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { | ||
AmqpConnection, | ||
RabbitMQModule, | ||
RabbitSubscribe, | ||
} from '@golevelup/nestjs-rabbitmq'; | ||
import { INestApplication, Injectable } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
|
||
const testHandler = jest.fn(); | ||
|
||
const routingKey1 = 'longConsumerRoutingKey'; | ||
let subscriberResolve: (value: unknown) => void; | ||
|
||
async function delay(milliseconds = 0, returnValue) { | ||
return new Promise((done) => | ||
setTimeout(() => done(returnValue), milliseconds), | ||
); | ||
} | ||
|
||
async function isFinished(promise) { | ||
return await Promise.race([ | ||
delay(0, false), | ||
promise.then( | ||
() => true, | ||
() => true, | ||
), | ||
]); | ||
} | ||
|
||
@Injectable() | ||
class SubscribeService { | ||
@RabbitSubscribe({ | ||
queue: routingKey1, | ||
}) | ||
async handleSubscribe(message: object) { | ||
await new Promise((resolve) => { | ||
subscriberResolve = resolve; | ||
}); | ||
testHandler(message); | ||
} | ||
} | ||
|
||
describe('Rabbit Graceful Shutdown', () => { | ||
let app: INestApplication; | ||
let amqpConnection: AmqpConnection; | ||
|
||
const rabbitHost = | ||
process.env.NODE_ENV === 'ci' ? process.env.RABBITMQ_HOST : 'localhost'; | ||
const rabbitPort = | ||
process.env.NODE_ENV === 'ci' ? process.env.RABBITMQ_PORT : '5672'; | ||
const uri = `amqp://rabbitmq:rabbitmq@${rabbitHost}:${rabbitPort}`; | ||
|
||
beforeAll(async () => { | ||
const moduleFixture = await Test.createTestingModule({ | ||
providers: [SubscribeService], | ||
imports: [ | ||
RabbitMQModule.forRoot(RabbitMQModule, { | ||
uri, | ||
connectionInitOptions: { wait: true, reject: true, timeout: 3000 }, | ||
}), | ||
], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication(); | ||
amqpConnection = app.get<AmqpConnection>(AmqpConnection); | ||
await app.init(); | ||
}); | ||
|
||
it('should wait for consumers to finish', async () => { | ||
await amqpConnection.publish('', routingKey1, 'testMessage'); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
const closePromise = app.close(); | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
expect(testHandler).not.toHaveBeenCalled(); | ||
expect(isFinished(closePromise)).toBeFalsy(); | ||
subscriberResolve(true); | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
expect(testHandler).toHaveBeenCalled(); | ||
expect(isFinished(closePromise)).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters