mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
feat: allow for microservice options to come from the di container
Microservices are now able to be created by getting their options from within the DI container itself. This has been a long requested feature of developers and I finally had some time to work through how we could possibly let this happen.
This commit is contained in:
98
integration/microservices/e2e/sum-rpc-async.spec.ts
Normal file
98
integration/microservices/e2e/sum-rpc-async.spec.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import {
|
||||
Controller,
|
||||
INestMicroservice,
|
||||
Injectable,
|
||||
Module,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
AsyncOptions,
|
||||
ClientTCP,
|
||||
ClientsModule,
|
||||
MessagePattern,
|
||||
MicroserviceOptions,
|
||||
Payload,
|
||||
TcpClientOptions,
|
||||
Transport,
|
||||
} from '@nestjs/microservices';
|
||||
import { expect } from 'chai';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
|
||||
let port: number;
|
||||
|
||||
do {
|
||||
port = Math.round(Math.random() * 10000);
|
||||
} while (port < 1000);
|
||||
|
||||
@Injectable()
|
||||
class RpcOptionsProvider {
|
||||
getOptions(): TcpClientOptions {
|
||||
return {
|
||||
transport: Transport.TCP,
|
||||
options: {
|
||||
port,
|
||||
host: '0.0.0.0',
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@Controller()
|
||||
class RpcController {
|
||||
@MessagePattern({ cmd: 'sum' })
|
||||
sumPayload(@Payload() payload: number[]) {
|
||||
return payload.reduce((a, b) => a + b, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ClientsModule.register([
|
||||
{
|
||||
name: 'RPC_CLIENT',
|
||||
transport: Transport.TCP,
|
||||
options: {
|
||||
port,
|
||||
host: '0.0.0.0',
|
||||
},
|
||||
},
|
||||
]),
|
||||
],
|
||||
controllers: [RpcController],
|
||||
providers: [RpcOptionsProvider],
|
||||
})
|
||||
class RpcModule {}
|
||||
|
||||
describe('RPC Async transport', () => {
|
||||
let app: INestMicroservice;
|
||||
let client: ClientTCP;
|
||||
|
||||
beforeEach(async () => {
|
||||
app = await NestFactory.createMicroservice<
|
||||
AsyncOptions<MicroserviceOptions>
|
||||
>(RpcModule, {
|
||||
logger: false,
|
||||
inject: [RpcOptionsProvider],
|
||||
useFactory: (optionsProvider: RpcOptionsProvider) =>
|
||||
optionsProvider.getOptions(),
|
||||
});
|
||||
|
||||
await app.listen();
|
||||
client = app.get('RPC_CLIENT', { strict: false });
|
||||
});
|
||||
|
||||
it(`/POST`, done => {
|
||||
let retData = 0;
|
||||
client.send({ cmd: 'sum' }, [1, 2, 3, 4, 5]).subscribe({
|
||||
next: val => (retData += val),
|
||||
error: done,
|
||||
complete: () => {
|
||||
expect(retData).to.eq(15);
|
||||
done();
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await app.close();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user