Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions e2e/mocks/api-feature.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Controller } from '@nestjs/common';
import { RMQInjectService, RMQService } from '../../lib';
import {
NotificationContracts,
SumContracts,
} from '../contracts/mock.contracts';

@Controller()
export class ApiFeatureController {
constructor(
public readonly rmqImplicitInject: RMQService,
@RMQInjectService("test2") public readonly rmqExplicitInject: RMQService
) { }

async sumSuccess(arrayToSum: number[]): Promise<SumContracts.Response> {
return this.rmqImplicitInject.send<SumContracts.Request, SumContracts.Response>(SumContracts.topic, { arrayToSum });
}

async sumSuccess2(arrayToSum: number[]): Promise<SumContracts.Response> {
return this.rmqExplicitInject.send<SumContracts.Request, SumContracts.Response>(SumContracts.topic, { arrayToSum });
}

async sumFailed(arrayToSum: string[]): Promise<SumContracts.Response> {
return this.rmqImplicitInject.send<any, SumContracts.Response>(SumContracts.topic, { arrayToSum });
}

async sumFailed2(arrayToSum: string[]): Promise<SumContracts.Response> {
return this.rmqExplicitInject.send<any, SumContracts.Response>(SumContracts.topic, { arrayToSum });
}

async notificationSuccess(message: string): Promise<void> {
return this.rmqImplicitInject.notify<NotificationContracts.Request>(NotificationContracts.topic, { message });
}

async notificationSuccess2(message: string): Promise<void> {
return this.rmqExplicitInject.notify<NotificationContracts.Request>(NotificationContracts.topic, { message });
}

async notificationFailed(message: number): Promise<void> {
return this.rmqImplicitInject.notify<any>(NotificationContracts.topic, { message });
}

async notificationFailed2(message: number): Promise<void> {
return this.rmqExplicitInject.notify<any>(NotificationContracts.topic, { message });
}
}
28 changes: 27 additions & 1 deletion e2e/mocks/microservice.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ManualAckContracts,
DebugContracts, CustomMessageFactoryContracts, PatternStarContracts, PatternHashContracts,
} from '../contracts/mock.contracts';
import { ERROR_TYPE } from '../../lib/constants';
import { DEFAULT_SERVICE_NAME, ERROR_TYPE } from '../../lib/constants';
import { Message } from 'amqplib';

@Controller()
Expand All @@ -33,13 +33,39 @@ export class MicroserviceController {
return { result: arrayToSum.reduce((prev, cur) => prev + cur) };
}

@RMQRoute(SumContracts.topic, { name: 'test2' })
@Validate()
sumRpc2({ arrayToSum }: SumContracts.Request): SumContracts.Response {
const result = arrayToSum.reduce((prev, cur) => prev + cur);
return { result };
}

@RMQRoute(SumContracts.topic, { name: 'test3' })
@Validate()
sumRpc3({ arrayToSum }: SumContracts.Request): SumContracts.Response {
const result = arrayToSum.reduce((prev, cur) => prev + cur);
if (result !== 2) {
throw new Error('Do I look like a calculator to you?');
}

return { result };
}

@RMQRoute(NotificationContracts.topic)
@Validate()
notificationNone({ message }: NotificationContracts.Request): void {
console.log(message);
return;
}

@RMQRoute(NotificationContracts.topic, { name: ['test2', 'test3'] })
@Validate()
notificationNone2({ message }: NotificationContracts.Request, @RMQMessage msg: ExtendedMessage): void {
console.log(message);
console.log(msg.serviceName);
return;
}

@RMQRoute(MultiplyContracts.topic)
@Validate()
multiplyRpc({ arrayToMultiply }: MultiplyContracts.Request): MultiplyContracts.Response {
Expand Down
File renamed without changes.
297 changes: 297 additions & 0 deletions e2e/tests/rmq-feature.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
import { Test } from '@nestjs/testing';
import { RMQModule, RMQService } from '../../lib';
import { INestApplication } from '@nestjs/common';
import { ApiController } from '../mocks/api.controller';
import { MicroserviceController } from '../mocks/microservice.controller';
import { DEFAULT_SERVICE_NAME, ERROR_UNDEFINED_FROM_RPC } from '../../lib/constants';
import { DoublePipe } from '../mocks/double.pipe';
import { ZeroIntercepter } from '../mocks/zero.intercepter';
import { ErrorHostHandler } from '../mocks/error-host.handler';
import { getServiceToken } from '../../lib/utils/get-service-token';
import { ApiFeatureController } from '../mocks/api-feature.controller';

class OverrideController extends ApiFeatureController { }

describe('RMQe2e forFeature()', () => {
let api: INestApplication;
let apiController: ApiFeatureController;
let overrideController: OverrideController;
let microserviceController: MicroserviceController;
let rmqServiceDefault: RMQService;
let rmqServiceTest2: RMQService;
let rmqServiceTest3: RMQService;

beforeAll(async () => {
const apiModule = await Test.createTestingModule({
imports: [
RMQModule.forRoot({
exchangeName: 'test1',
connections: [
{
login: 'guest',
password: 'guest',
host: 'localhost',
},
],
queueName: 'test-queue1',
heartbeatIntervalInSeconds: 10,
prefetchCount: 10,
middleware: [DoublePipe],
intercepters: [ZeroIntercepter],
errorHandler: ErrorHostHandler,
serviceName: 'test-service',
messagesTimeout: 2000,
}),
RMQModule.forRoot({
name: 'test2',
exchangeName: 'test2',
connections: [
{
login: 'guest',
password: 'guest',
host: 'localhost',
},
],
queueName: '', // random exclusive
heartbeatIntervalInSeconds: 10,
prefetchCount: 10,
middleware: [DoublePipe],
intercepters: [ZeroIntercepter],
errorHandler: ErrorHostHandler,
serviceName: 'test-service',
messagesTimeout: 2000,
}),
RMQModule.forRootAsync(
{
imports: [],
inject: [],
name: 'test3',
useFactory: () => (
{
exchangeName: 'test3',
connections: [
{
login: 'guest',
password: 'guest',
host: 'localhost',
},
],
queueName: 'test-queue3',
heartbeatIntervalInSeconds: 10,
prefetchCount: 10,
middleware: [DoublePipe],
intercepters: [ZeroIntercepter],
errorHandler: ErrorHostHandler,
serviceName: 'test-service',
messagesTimeout: 2000,
}
)
}
),
{
// uses test3 implicitly and test2 explicitly
imports: [
RMQModule.forFeature('test3')
],
controllers: [OverrideController],
module: class OverrideRMQModule { },
},
{
// uses default implicitly and test2 explicitly
imports: [],
controllers: [ApiFeatureController],
module: class SampleRMQModule { },
}
],
controllers: [MicroserviceController],
}).compile();
api = apiModule.createNestApplication();
await api.init();

apiController = apiModule.get<ApiFeatureController>(ApiFeatureController);
overrideController = apiModule.get<ApiFeatureController>(OverrideController);
microserviceController = apiModule.get<MicroserviceController>(MicroserviceController);
rmqServiceDefault = apiController.rmqImplicitInject;
rmqServiceTest2 = apiController.rmqExplicitInject;
rmqServiceTest3 = overrideController.rmqImplicitInject;
console.warn = jest.fn();
console.log = jest.fn();
});

describe('rpc', () => {
it('check name', async () => {
expect(apiController.rmqImplicitInject.name).toBe(DEFAULT_SERVICE_NAME);
expect(apiController.rmqExplicitInject.name).toBe('test2');

expect(overrideController.rmqImplicitInject.name).toBe('test3');
expect(overrideController.rmqExplicitInject.name).toBe('test2');
});

it('check connection', async () => {
const isConnected = rmqServiceDefault.healthCheck();
expect(isConnected).toBe(true);

const isConnected2 = rmqServiceTest2.healthCheck();
expect(isConnected2).toBe(true);

const isConnected3 = rmqServiceTest3.healthCheck();
expect(isConnected3).toBe(true);
});

it('default: successful send()', async () => {
const { result } = await apiController.sumSuccess([1, 2, 3]);
expect(result).toBe(6);
});

it('default: request validation failed', async () => {
try {
await apiController.sumFailed(['a', 'b', 'c']);
expect(true).toBe(false);
} catch (error) {
expect(error.message).toBe(
'each value in arrayToSum must be a number conforming to the specified constraints',
);
expect(error.type).toBeUndefined();
expect(error.code).toBeUndefined();
expect(error.data).toBeUndefined();
expect(error.service).toBe('test-service');
expect(error.host).not.toBeNull();
}
});

it('test2: successful send()', async () => {
const { result } = await apiController.sumSuccess2([-10, 8, -9, 5]);
expect(result).toBe(-6);
});

it('test2: request validation failed', async () => {
try {
await apiController.sumFailed2(['a', 'b', 'c']);
expect(true).toBe(false);
} catch (error) {
expect(error.message).toBe(
'each value in arrayToSum must be a number conforming to the specified constraints',
);
expect(error.type).toBeUndefined();
expect(error.code).toBeUndefined();
expect(error.data).toBeUndefined();
expect(error.service).toBe('test-service');
expect(error.host).not.toBeNull();
}
});

it('override - test3: successful send()', async () => {
const { result } = await overrideController.sumSuccess([1, 2, -1]);
expect(result).toBe(2);
});

it('override - test3: error thrown by microservice', async () => {
try {
await overrideController.sumSuccess([1, 2, 3]);
expect(true).toBe(false);
} catch (error) {
expect(error.message).toBe(
'Do I look like a calculator to you?',
);
expect(error.type).toBeUndefined();
expect(error.code).toBeUndefined();
expect(error.data).toBeUndefined();
expect(error.service).toBe('test-service');
expect(error.host).not.toBeNull();
}
});

it('override - test2: successful send()', async () => {
const { result } = await overrideController.sumSuccess2([-10, 8, -9, 5]);
expect(result).toBe(-6);
});

it('override - test2: request validation failed', async () => {
try {
await overrideController.sumFailed2(['a', 'b', 'c']);
expect(true).toBe(false);
} catch (error) {
expect(error.message).toBe(
'each value in arrayToSum must be a number conforming to the specified constraints',
);
expect(error.type).toBeUndefined();
expect(error.code).toBeUndefined();
expect(error.data).toBeUndefined();
expect(error.service).toBe('test-service');
expect(error.host).not.toBeNull();
}
});
});

describe('none', () => {
it('default: successful notify()', async () => {
const res = await apiController.notificationSuccess('SECRETMESSAGE');
await delay(1000);
expect(console.log).toBeCalledTimes(1);
expect(console.log).toHaveBeenCalledWith('SECRETMESSAGE');
expect(res).toBeUndefined();
jest.clearAllMocks();
});

it('default: notify validation failed', async () => {
const res = await apiController.notificationFailed(0);
expect(console.log).toBeCalledTimes(0);
expect(res).toBeUndefined();
expect(res).toBeUndefined();
});

it('test2: successful notify()', async () => {
const res = await apiController.notificationSuccess2('SECRETMESSAGE2');
await delay(1000);
expect(console.log).toBeCalledTimes(2);
expect(console.log).toHaveBeenCalledWith('SECRETMESSAGE2');
expect(console.log).toHaveBeenCalledWith('test2');
expect(res).toBeUndefined();
jest.clearAllMocks();
});

it('test2: notify validation failed', async () => {
const res = await apiController.notificationFailed2(0);
expect(console.log).toBeCalledTimes(0);
expect(res).toBeUndefined();
expect(res).toBeUndefined();
});

it('override - test3: successful notify()', async () => {
const res = await overrideController.notificationSuccess('SECRETMESSAGE3');
await delay(1000);
expect(console.log).toBeCalledTimes(2);
expect(console.log).toHaveBeenCalledWith('SECRETMESSAGE3');
expect(console.log).toHaveBeenCalledWith('test3');
expect(res).toBeUndefined();
jest.clearAllMocks();
});

it('override - test2: successful notify()', async () => {
const res = await overrideController.notificationSuccess2('SECRETMESSAGE4');
await delay(1000);
expect(console.log).toBeCalledTimes(2);
expect(console.log).toHaveBeenCalledWith('SECRETMESSAGE4');
expect(console.log).toHaveBeenCalledWith('test2');
expect(res).toBeUndefined();
jest.clearAllMocks();
});
});

afterAll(async () => {
await delay(500);
await rmqServiceDefault.disconnect();
await rmqServiceTest2.disconnect();
await rmqServiceTest3.disconnect();
await api.close();
await delay(500);
});
});

async function delay(time: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, time);
});
}
Loading