Skip to content

Commit

Permalink
refactor: rabbitmq module use new configurable builder api (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfisk authored Jan 5, 2025
1 parent eed04da commit c95fd53
Show file tree
Hide file tree
Showing 23 changed files with 147 additions and 152 deletions.
2 changes: 1 addition & 1 deletion integration/rabbitmq/e2e/cancel-and-resume.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Rabbit Cancel and Resume', () => {
const moduleFixture = await Test.createTestingModule({
providers: [],
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
exchanges: [
{
name: exchange,
Expand Down
129 changes: 47 additions & 82 deletions integration/rabbitmq/e2e/configuration.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
RabbitMQModule,
AmqpConnectionManager,
} from '@golevelup/nestjs-rabbitmq';
import { ConsoleLogger } from '@nestjs/common';
import { createMock } from '@golevelup/ts-jest';
import { Logger, Provider } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as amqplib from 'amqplib';

Expand All @@ -14,22 +15,34 @@ const rabbitPort =
process.env.NODE_ENV === 'ci' ? process.env.RABBITMQ_PORT : '5672';
const uri = `amqp://rabbitmq:rabbitmq@${rabbitHost}:${rabbitPort}`;
const amqplibUri = `${uri}?heartbeat=5`;
const logger = new ConsoleLogger('Custom logger');
const customLogger = createMock<Logger>({
log: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
});

const nonExistingExchange = 'non-existing-exchange';
const nonExistingQueue = 'non-existing-queue';

const testRoutingKey = 'test';

class RabbitConfig {
createModuleConfig(): RabbitMQConfig {
create(): RabbitMQConfig {
return {
uri,
connectionManagerOptions: { heartbeatIntervalInSeconds: 5 },
connectionInitOptions: { wait: true, reject: true, timeout: 3000 },
};
}
}
const silentLoggerProvider: Provider = {
provide: Logger,
useValue: {
log: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
},
};

describe('Module Configuration', () => {
let app: TestingModule;
Expand All @@ -43,14 +56,14 @@ describe('Module Configuration', () => {
describe('forRoot', () => {
it('should configure RabbitMQ', async () => {
const spy = jest.spyOn(amqplib, 'connect');
const logSpy = jest.spyOn(logger, 'log');
const logSpy = jest.spyOn(customLogger, 'log');

app = await Test.createTestingModule({
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
uri,
connectionInitOptions: { wait: true, reject: true, timeout: 3000 },
logger,
logger: customLogger,
}),
],
}).compile();
Expand All @@ -63,24 +76,11 @@ describe('Module Configuration', () => {
expect(logSpy).toHaveBeenCalled();
});

it('should be able to configure RabbitMQ when config is not provided', async () => {
const spy = jest.spyOn(amqplib, 'connect');
app = await Test.createTestingModule({
imports: [RabbitMQModule.forRoot(RabbitMQModule)],
}).compile();
const connectionManager = app.get(AmqpConnectionManager);
const connection = app.get(AmqpConnection);

expect(spy).not.toHaveBeenCalled();
expect(app).toBeDefined();
expect(connectionManager).toBeDefined();
expect(connection).toBeUndefined();
});

it('should be able to add connection latter when rmq config is not provided through module', async () => {
const spy = jest.spyOn(amqplib, 'connect');
app = await Test.createTestingModule({
imports: [RabbitMQModule.forRoot(RabbitMQModule)],
providers: [silentLoggerProvider],
imports: [RabbitMQModule.forRoot({ uri })],
}).compile();
const connectionManager = app.get(AmqpConnectionManager);
const connection = new AmqpConnection({
Expand All @@ -97,7 +97,7 @@ describe('Module Configuration', () => {
try {
app = await Test.createTestingModule({
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
exchanges: [
{
name: nonExistingExchange,
Expand All @@ -111,7 +111,7 @@ describe('Module Configuration', () => {
reject: true,
timeout: 3000,
},
logger,
logger: customLogger,
}),
],
}).compile();
Expand All @@ -129,7 +129,7 @@ describe('Module Configuration', () => {

app = await Test.createTestingModule({
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
exchanges: [
{
name: nonExistingExchange,
Expand All @@ -143,7 +143,7 @@ describe('Module Configuration', () => {
reject: true,
timeout: 3000,
},
logger,
logger: customLogger,
}),
],
}).compile();
Expand All @@ -166,7 +166,7 @@ describe('Module Configuration', () => {

app = await Test.createTestingModule({
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
exchanges: [
{
name: nonExistingExchange,
Expand All @@ -180,7 +180,7 @@ describe('Module Configuration', () => {
reject: true,
timeout: 3000,
},
logger,
logger: customLogger,
}),
],
}).compile();
Expand All @@ -199,7 +199,7 @@ describe('Module Configuration', () => {
try {
app = await Test.createTestingModule({
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
queues: [
{
name: nonExistingQueue,
Expand All @@ -212,7 +212,7 @@ describe('Module Configuration', () => {
reject: true,
timeout: 3000,
},
logger,
logger: customLogger,
}),
],
}).compile();
Expand All @@ -230,7 +230,7 @@ describe('Module Configuration', () => {

app = await Test.createTestingModule({
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
queues: [
{
name: nonExistingQueue,
Expand All @@ -243,7 +243,7 @@ describe('Module Configuration', () => {
reject: true,
timeout: 3000,
},
logger,
logger: customLogger,
}),
],
}).compile();
Expand Down Expand Up @@ -285,8 +285,9 @@ describe('Module Configuration', () => {
});

app = await Test.createTestingModule({
providers: [silentLoggerProvider],
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
RabbitMQModule.forRootAsync({
useFactory: async () => {
return {
queues: [
Expand Down Expand Up @@ -330,8 +331,9 @@ describe('Module Configuration', () => {
const spy = jest.spyOn(amqplib, 'connect');

app = await Test.createTestingModule({
providers: [silentLoggerProvider],
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
RabbitMQModule.forRootAsync({
useFactory: async () => {
return {
uri,
Expand All @@ -356,8 +358,9 @@ describe('Module Configuration', () => {
const spy = jest.spyOn(amqplib, 'connect');

app = await Test.createTestingModule({
providers: [silentLoggerProvider],
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
RabbitMQModule.forRootAsync({
useClass: RabbitConfig,
}),
],
Expand All @@ -369,55 +372,13 @@ describe('Module Configuration', () => {
expect(spy).toHaveBeenCalledWith(amqplibUri, undefined);
});

it('should configure RabbitMQ with useExisting explicit provide', async () => {
const spy = jest.spyOn(amqplib, 'connect');

const instance = new RabbitConfig();

app = await Test.createTestingModule({
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
useExisting: {
provide: RabbitConfig,
value: instance,
},
}),
],
}).compile();

expect(app).toBeDefined();

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(amqplibUri, undefined);
});

it('should configure RabbitMQ with useExisting implicit provide', async () => {
const spy = jest.spyOn(amqplib, 'connect');

const instance = new RabbitConfig();

app = await Test.createTestingModule({
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
useExisting: {
value: instance,
},
}),
],
}).compile();

expect(app).toBeDefined();

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(amqplibUri, undefined);
});

describe('should use `createExchangeIfNotExists` flag correctly', () => {
it("should throw an error if exchange doesn't exist and `createExchangeIfNotExists` is false", async () => {
try {
app = await Test.createTestingModule({
providers: [silentLoggerProvider],
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
RabbitMQModule.forRootAsync({
useFactory: async () => {
return {
exchanges: [
Expand Down Expand Up @@ -451,8 +412,9 @@ describe('Module Configuration', () => {
const spy = jest.spyOn(amqplib, 'connect');

app = await Test.createTestingModule({
providers: [silentLoggerProvider],
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
RabbitMQModule.forRootAsync({
useFactory: async () => {
return {
exchanges: [
Expand Down Expand Up @@ -491,8 +453,9 @@ describe('Module Configuration', () => {
const spy = jest.spyOn(amqplib, 'connect');

app = await Test.createTestingModule({
providers: [silentLoggerProvider],
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
RabbitMQModule.forRootAsync({
useFactory: async () => {
return {
exchanges: [
Expand Down Expand Up @@ -568,8 +531,9 @@ describe('Module Configuration', () => {
});

app = await Test.createTestingModule({
providers: [silentLoggerProvider],
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
RabbitMQModule.forRootAsync({
useFactory: async () => {
return {
exchanges: [
Expand Down Expand Up @@ -638,8 +602,9 @@ describe('Module Configuration', () => {
const otherExchangeName = 'otherExchange';

app = await Test.createTestingModule({
providers: [silentLoggerProvider],
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
RabbitMQModule.forRootAsync({
useFactory: async () => {
return {
exchanges: [
Expand Down
2 changes: 1 addition & 1 deletion integration/rabbitmq/e2e/connection-failover.e2e-spec1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class RestController {

@Module({
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
exchanges: [
{
name: exchange,
Expand Down
2 changes: 1 addition & 1 deletion integration/rabbitmq/e2e/execution-context.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Rabbit Subscribe Without Register Handlers', () => {
const moduleFixture = await Test.createTestingModule({
providers: [SubscribeService, TestInterceptor],
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
exchanges: [
{
name: exchange,
Expand Down
2 changes: 1 addition & 1 deletion integration/rabbitmq/e2e/graceful-shutdown.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('Rabbit Graceful Shutdown', () => {
const moduleFixture = await Test.createTestingModule({
providers: [SubscribeService],
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
uri,
connectionInitOptions: { wait: true, reject: true, timeout: 3000 },
}),
Expand Down
2 changes: 1 addition & 1 deletion integration/rabbitmq/e2e/multiple-channels.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('Rabbit Multiple Channels', () => {
const moduleFixture = await Test.createTestingModule({
providers: [SubscribeToMultipleChannelsService],
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
exchanges: [
{
name: exchange,
Expand Down
2 changes: 1 addition & 1 deletion integration/rabbitmq/e2e/nack-and-requeue.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Nack and Requeue', () => {
const moduleFixture = await Test.createTestingModule({
providers: [SubscribeService],
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
RabbitMQModule.forRootAsync({
useFactory: () => ({
exchanges: [
{
Expand Down
2 changes: 1 addition & 1 deletion integration/rabbitmq/e2e/rmq-context.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('RMQ Context in Global interceptor', () => {
const moduleFixture = await Test.createTestingModule({
providers: [SubscribeService, TestInterceptor],
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
exchanges: [
{
name: exchange,
Expand Down
2 changes: 1 addition & 1 deletion integration/rabbitmq/e2e/rpc-no-direct-reply.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Rabbit Direct Reply To', () => {
const moduleFixture = await Test.createTestingModule({
providers: [RpcService],
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
exchanges: [
{
name: exchange,
Expand Down
2 changes: 1 addition & 1 deletion integration/rabbitmq/e2e/serialization.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Rabbit Subscribe', () => {
SubscribeHandlerSerializationService,
],
imports: [
RabbitMQModule.forRoot(RabbitMQModule, {
RabbitMQModule.forRoot({
exchanges: [
{
name: exchange,
Expand Down
Loading

0 comments on commit c95fd53

Please sign in to comment.