-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rabbitmq): execution context check utility
convenience function to check whether the current handler in an execution context is related to RabbitMQ re #204
- Loading branch information
1 parent
5c3e095
commit 4256a6b
Showing
3 changed files
with
92 additions
and
0 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,84 @@ | ||
import { | ||
AmqpConnection, | ||
isRabbitContext, | ||
RabbitMQModule, | ||
RabbitSubscribe, | ||
} from '@golevelup/nestjs-rabbitmq'; | ||
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common'; | ||
import { INestApplication, Injectable } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
|
||
const interceptorHandler = jest.fn(); | ||
|
||
const exchange = 'contextExchange'; | ||
const queue = 'contextQueue'; | ||
|
||
@Injectable() | ||
class TestInterceptor implements NestInterceptor { | ||
intercept(context: ExecutionContext, next: CallHandler<any>) { | ||
const shouldSkip = isRabbitContext(context); | ||
if (shouldSkip) { | ||
return next.handle(); | ||
} | ||
|
||
interceptorHandler('invoked'); | ||
return next.handle(); | ||
} | ||
} | ||
|
||
@Injectable() | ||
class SubscribeService { | ||
@RabbitSubscribe({ | ||
exchange, | ||
routingKey: '#', | ||
queue, | ||
}) | ||
handleSubscribe(message: object) { | ||
console.log(`RECEIVED MESSAGE: ${message}`); | ||
} | ||
} | ||
|
||
describe('Rabbit Subscribe Without Register Handlers', () => { | ||
let app: INestApplication; | ||
let amqpConnection: AmqpConnection; | ||
|
||
const rabbitHost = process.env.NODE_ENV === 'ci' ? 'rabbit' : 'localhost'; | ||
const uri = `amqp://rabbitmq:rabbitmq@${rabbitHost}:5672`; | ||
|
||
beforeAll(async () => { | ||
const moduleFixture = await Test.createTestingModule({ | ||
providers: [SubscribeService, TestInterceptor], | ||
imports: [ | ||
RabbitMQModule.forRoot(RabbitMQModule, { | ||
exchanges: [ | ||
{ | ||
name: exchange, | ||
type: 'topic', | ||
}, | ||
], | ||
uri, | ||
connectionInitOptions: { wait: true, reject: true, timeout: 3000 }, | ||
}), | ||
], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication(); | ||
amqpConnection = app.get<AmqpConnection>(AmqpConnection); | ||
app.useGlobalInterceptors(new TestInterceptor()); | ||
await app.init(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
it('should recognize a rabbit handler execution context and allow for interceptors to be skipped', async (done) => { | ||
await amqpConnection.publish(exchange, 'x', `test-message`); | ||
expect.assertions(1); | ||
|
||
setTimeout(() => { | ||
expect(interceptorHandler).not.toHaveBeenCalled(); | ||
done(); | ||
}, 100); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ExecutionContext } from '@nestjs/common'; | ||
import { RABBIT_HANDLER } from './rabbitmq.constants'; | ||
|
||
export const isRabbitContext = (executionContext: ExecutionContext) => { | ||
const handler = executionContext.getHandler(); | ||
return Reflect.getMetadataKeys(handler).includes(RABBIT_HANDLER); | ||
}; |