From 67dacc31a0c1cb3c47990765259d614f4f92912d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Thu, 30 May 2019 13:06:39 +0200 Subject: [PATCH] integration(): fix grpc port conflict --- .../microservices/e2e/orders-grpc.spec.ts | 75 ++++++++++--------- integration/microservices/package.json | 1 + .../grpc-advanced/advanced.grpc.controller.ts | 11 ++- .../test/server/server-grpc.spec.ts | 1 + 4 files changed, 49 insertions(+), 39 deletions(-) diff --git a/integration/microservices/e2e/orders-grpc.spec.ts b/integration/microservices/e2e/orders-grpc.spec.ts index 715baea0e..948d89a79 100644 --- a/integration/microservices/e2e/orders-grpc.spec.ts +++ b/integration/microservices/e2e/orders-grpc.spec.ts @@ -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: { @@ -41,16 +43,15 @@ describe('Advanced GRPC transport', () => { await app.init(); // Load proto-buffers for test gRPC dispatch const proto = ProtoLoader.loadSync('root.proto', { - includeDirs: [join(__dirname, '../src/grpc-advanced/proto')], + includeDirs: [join(__dirname, '../src/grpc-advanced/proto')], }) as any; // Create Raw gRPC client object 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,33 +70,32 @@ describe('Advanced GRPC transport', () => { }); it('GRPC Sending and receiving message', async () => { - // Execute find in Promise return new Promise(resolve => { - client.find({ - id: 1, - }, (err, result) => { - // Compare results - expect(err).to.be.null; - expect(result).to.eql({ + client.find( + { id: 1, - itemTypes: [1], - shipmentType: { - from: 'test', - to: 'test1', - carrier: 'test-carrier', - }, - }); - // Resolve after checkups - resolve(); - }); - + }, + (err, result) => { + // Compare results + expect(err).to.be.null; + expect(result).to.eql({ + id: 1, + itemTypes: [1], + shipmentType: { + from: 'test', + to: 'test1', + carrier: 'test-carrier', + }, + }); + // 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); }); - }); - - -}); \ No newline at end of file +}); diff --git a/integration/microservices/package.json b/integration/microservices/package.json index 89c1f8d38..75ec00966 100644 --- a/integration/microservices/package.json +++ b/integration/microservices/package.json @@ -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", diff --git a/integration/microservices/src/grpc-advanced/advanced.grpc.controller.ts b/integration/microservices/src/grpc-advanced/advanced.grpc.controller.ts index 02396bf53..c8b1ed1bb 100644 --- a/integration/microservices/src/grpc-advanced/advanced.grpc.controller.ts +++ b/integration/microservices/src/grpc-advanced/advanced.grpc.controller.ts @@ -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: { @@ -92,4 +97,4 @@ export class AdvancedGrpcController { }); }); } -} \ No newline at end of file +} diff --git a/packages/microservices/test/server/server-grpc.spec.ts b/packages/microservices/test/server/server-grpc.spec.ts index 0ead6348d..b2312477d 100644 --- a/packages/microservices/test/server/server-grpc.spec.ts +++ b/packages/microservices/test/server/server-grpc.spec.ts @@ -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(), };