Skip to content

Commit

Permalink
feat(rabbitmq): execution context check utility
Browse files Browse the repository at this point in the history
convenience function to check whether the current handler in an execution context is related to
RabbitMQ

re #204
  • Loading branch information
WonderPanda committed Dec 8, 2020
1 parent 5c3e095 commit 4256a6b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
84 changes: 84 additions & 0 deletions integration/rabbitmq/e2e/execution-context.e2e-spec.ts
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);
});
});
1 change: 1 addition & 0 deletions packages/rabbitmq/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './rabbitmq.constants';
export * from './rabbitmq.decorators';
export * from './rabbitmq.interfaces';
export * from './rabbitmq.module';
export * from './rabbitmq.helpers';
7 changes: 7 additions & 0 deletions packages/rabbitmq/src/rabbitmq.helpers.ts
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);
};

0 comments on commit 4256a6b

Please sign in to comment.