integration(): fix grpc port conflict

This commit is contained in:
Kamil Myśliwiec
2019-05-30 13:06:39 +02:00
parent fca5411430
commit 67dacc31a0
4 changed files with 49 additions and 39 deletions

View File

@@ -1,13 +1,14 @@
import * as ProtoLoader from '@grpc/proto-loader';
import { INestApplication } from '@nestjs/common';
import { Transport } from '@nestjs/microservices';
import { ExpressAdapter } from '@nestjs/platform-express';
import { Test } from '@nestjs/testing';
import { fail } from 'assert';
import { expect } from 'chai';
import * as express from 'express';
import * as GRPC from 'grpc';
import { join } from 'path';
import * as request from 'supertest';
import * as ProtoLoader from '@grpc/proto-loader';
import * as GRPC from 'grpc';
import { expect } from 'chai';
import { fail } from 'assert';
import { AdvancedGrpcController } from '../src/grpc-advanced/advanced.grpc.controller';
describe('Advanced GRPC transport', () => {
@@ -21,13 +22,14 @@ describe('Advanced GRPC transport', () => {
}).compile();
// Create gRPC + HTTP server
server = express();
app = module.createNestApplication(server);
app = module.createNestApplication(new ExpressAdapter(server));
/*
* Create microservice configuration
*/
app.connectMicroservice({
transport: Transport.GRPC,
options: {
url: 'localhost:5001',
package: 'proto_example',
protoPath: 'root.proto',
loader: {
@@ -47,10 +49,9 @@ describe('Advanced GRPC transport', () => {
const protoGRPC = GRPC.loadPackageDefinition(proto) as any;
// Create client connected to started services at standard 5000 port
client = new protoGRPC.proto_example.orders.OrderService(
'localhost:5000',
'localhost:5001',
GRPC.credentials.createInsecure(),
);
});
it(`GRPC Sending and Receiving HTTP POST`, () => {
@@ -69,12 +70,13 @@ describe('Advanced GRPC transport', () => {
});
it('GRPC Sending and receiving message', async () => {
// Execute find in Promise
return new Promise(resolve => {
client.find({
client.find(
{
id: 1,
}, (err, result) => {
},
(err, result) => {
// Compare results
expect(err).to.be.null;
expect(result).to.eql({
@@ -88,14 +90,12 @@ describe('Advanced GRPC transport', () => {
});
// Resolve after checkups
resolve();
},
);
});
});
});
it('GRPC Sending and receiving Stream from RX handler', async () => {
const callHandler = client.sync();
callHandler.on('data', (msg: number) => {
@@ -114,7 +114,11 @@ describe('Advanced GRPC transport', () => {
callHandler.on('error', (err: any) => {
// We want to fail only on real errors while Cancellation error
// is expected
if (String(err).toLowerCase().indexOf('cancelled') === -1) {
if (
String(err)
.toLowerCase()
.indexOf('cancelled') === -1
) {
fail('gRPC Stream error happened, error: ' + err);
}
});
@@ -125,11 +129,9 @@ describe('Advanced GRPC transport', () => {
});
setTimeout(() => resolve(), 1000);
});
});
it('GRPC Sending and receiving Stream from Call handler', async () => {
const callHandler = client.syncCall();
callHandler.on('data', (msg: number) => {
@@ -148,7 +150,11 @@ describe('Advanced GRPC transport', () => {
callHandler.on('error', (err: any) => {
// We want to fail only on real errors while Cancellation error
// is expected
if (String(err).toLowerCase().indexOf('cancelled') === -1) {
if (
String(err)
.toLowerCase()
.indexOf('cancelled') === -1
) {
fail('gRPC Stream error happened, error: ' + err);
}
});
@@ -159,8 +165,5 @@ describe('Advanced GRPC transport', () => {
});
setTimeout(() => resolve(), 1000);
});
});
});

View File

@@ -10,6 +10,7 @@
"@nestjs/common": "6.2.4",
"@nestjs/core": "6.2.4",
"@nestjs/microservices": "6.2.4",
"@nestjs/platform-express": "6.2.4",
"@nestjs/testing": "6.2.4",
"@nestjs/websockets": "6.2.4",
"amqp-connection-manager": "2.3.2",

View File

@@ -1,7 +1,11 @@
import { Body, Controller, HttpCode, Post } from '@nestjs/common';
import {
Client, ClientGrpc, GrpcMethod,
GrpcStreamMethod, GrpcStreamCall, Transport,
Client,
ClientGrpc,
GrpcMethod,
GrpcStreamCall,
GrpcStreamMethod,
Transport,
} from '@nestjs/microservices';
import { join } from 'path';
import { Observable, of, Subject } from 'rxjs';
@@ -14,6 +18,7 @@ export class AdvancedGrpcController {
@Client({
transport: Transport.GRPC,
options: {
url: 'localhost:5001',
package: 'proto_example.orders',
protoPath: 'root.proto',
loader: {

View File

@@ -357,6 +357,7 @@ describe('ServerGrpc', () => {
const fn = server.createStreamDuplexMethod(handler);
const call = {
on: (event, callback) => callback(),
off: sinon.spy(),
end: sinon.spy(),
write: sinon.spy(),
};