Skip to content

Commit 67dacc3

Browse files
integration(): fix grpc port conflict
1 parent fca5411 commit 67dacc3

4 files changed

Lines changed: 49 additions & 39 deletions

File tree

integration/microservices/e2e/orders-grpc.spec.ts

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import * as ProtoLoader from '@grpc/proto-loader';
12
import { INestApplication } from '@nestjs/common';
23
import { Transport } from '@nestjs/microservices';
4+
import { ExpressAdapter } from '@nestjs/platform-express';
35
import { Test } from '@nestjs/testing';
6+
import { fail } from 'assert';
7+
import { expect } from 'chai';
48
import * as express from 'express';
9+
import * as GRPC from 'grpc';
510
import { join } from 'path';
611
import * as request from 'supertest';
7-
import * as ProtoLoader from '@grpc/proto-loader';
8-
import * as GRPC from 'grpc';
9-
import { expect } from 'chai';
10-
import { fail } from 'assert';
1112
import { AdvancedGrpcController } from '../src/grpc-advanced/advanced.grpc.controller';
1213

1314
describe('Advanced GRPC transport', () => {
@@ -21,13 +22,14 @@ describe('Advanced GRPC transport', () => {
2122
}).compile();
2223
// Create gRPC + HTTP server
2324
server = express();
24-
app = module.createNestApplication(server);
25+
app = module.createNestApplication(new ExpressAdapter(server));
2526
/*
2627
* Create microservice configuration
2728
*/
2829
app.connectMicroservice({
2930
transport: Transport.GRPC,
3031
options: {
32+
url: 'localhost:5001',
3133
package: 'proto_example',
3234
protoPath: 'root.proto',
3335
loader: {
@@ -41,16 +43,15 @@ describe('Advanced GRPC transport', () => {
4143
await app.init();
4244
// Load proto-buffers for test gRPC dispatch
4345
const proto = ProtoLoader.loadSync('root.proto', {
44-
includeDirs: [join(__dirname, '../src/grpc-advanced/proto')],
46+
includeDirs: [join(__dirname, '../src/grpc-advanced/proto')],
4547
}) as any;
4648
// Create Raw gRPC client object
4749
const protoGRPC = GRPC.loadPackageDefinition(proto) as any;
4850
// Create client connected to started services at standard 5000 port
4951
client = new protoGRPC.proto_example.orders.OrderService(
50-
'localhost:5000',
52+
'localhost:5001',
5153
GRPC.credentials.createInsecure(),
5254
);
53-
5455
});
5556

5657
it(`GRPC Sending and Receiving HTTP POST`, () => {
@@ -69,33 +70,32 @@ describe('Advanced GRPC transport', () => {
6970
});
7071

7172
it('GRPC Sending and receiving message', async () => {
72-
7373
// Execute find in Promise
7474
return new Promise(resolve => {
75-
client.find({
76-
id: 1,
77-
}, (err, result) => {
78-
// Compare results
79-
expect(err).to.be.null;
80-
expect(result).to.eql({
75+
client.find(
76+
{
8177
id: 1,
82-
itemTypes: [1],
83-
shipmentType: {
84-
from: 'test',
85-
to: 'test1',
86-
carrier: 'test-carrier',
87-
},
88-
});
89-
// Resolve after checkups
90-
resolve();
91-
});
92-
78+
},
79+
(err, result) => {
80+
// Compare results
81+
expect(err).to.be.null;
82+
expect(result).to.eql({
83+
id: 1,
84+
itemTypes: [1],
85+
shipmentType: {
86+
from: 'test',
87+
to: 'test1',
88+
carrier: 'test-carrier',
89+
},
90+
});
91+
// Resolve after checkups
92+
resolve();
93+
},
94+
);
9395
});
94-
9596
});
9697

9798
it('GRPC Sending and receiving Stream from RX handler', async () => {
98-
9999
const callHandler = client.sync();
100100

101101
callHandler.on('data', (msg: number) => {
@@ -114,7 +114,11 @@ describe('Advanced GRPC transport', () => {
114114
callHandler.on('error', (err: any) => {
115115
// We want to fail only on real errors while Cancellation error
116116
// is expected
117-
if (String(err).toLowerCase().indexOf('cancelled') === -1) {
117+
if (
118+
String(err)
119+
.toLowerCase()
120+
.indexOf('cancelled') === -1
121+
) {
118122
fail('gRPC Stream error happened, error: ' + err);
119123
}
120124
});
@@ -125,11 +129,9 @@ describe('Advanced GRPC transport', () => {
125129
});
126130
setTimeout(() => resolve(), 1000);
127131
});
128-
129132
});
130133

131134
it('GRPC Sending and receiving Stream from Call handler', async () => {
132-
133135
const callHandler = client.syncCall();
134136

135137
callHandler.on('data', (msg: number) => {
@@ -148,7 +150,11 @@ describe('Advanced GRPC transport', () => {
148150
callHandler.on('error', (err: any) => {
149151
// We want to fail only on real errors while Cancellation error
150152
// is expected
151-
if (String(err).toLowerCase().indexOf('cancelled') === -1) {
153+
if (
154+
String(err)
155+
.toLowerCase()
156+
.indexOf('cancelled') === -1
157+
) {
152158
fail('gRPC Stream error happened, error: ' + err);
153159
}
154160
});
@@ -159,8 +165,5 @@ describe('Advanced GRPC transport', () => {
159165
});
160166
setTimeout(() => resolve(), 1000);
161167
});
162-
163168
});
164-
165-
166-
});
169+
});

integration/microservices/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@nestjs/common": "6.2.4",
1111
"@nestjs/core": "6.2.4",
1212
"@nestjs/microservices": "6.2.4",
13+
"@nestjs/platform-express": "6.2.4",
1314
"@nestjs/testing": "6.2.4",
1415
"@nestjs/websockets": "6.2.4",
1516
"amqp-connection-manager": "2.3.2",

integration/microservices/src/grpc-advanced/advanced.grpc.controller.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { Body, Controller, HttpCode, Post } from '@nestjs/common';
22
import {
3-
Client, ClientGrpc, GrpcMethod,
4-
GrpcStreamMethod, GrpcStreamCall, Transport,
3+
Client,
4+
ClientGrpc,
5+
GrpcMethod,
6+
GrpcStreamCall,
7+
GrpcStreamMethod,
8+
Transport,
59
} from '@nestjs/microservices';
610
import { join } from 'path';
711
import { Observable, of, Subject } from 'rxjs';
@@ -14,6 +18,7 @@ export class AdvancedGrpcController {
1418
@Client({
1519
transport: Transport.GRPC,
1620
options: {
21+
url: 'localhost:5001',
1722
package: 'proto_example.orders',
1823
protoPath: 'root.proto',
1924
loader: {
@@ -92,4 +97,4 @@ export class AdvancedGrpcController {
9297
});
9398
});
9499
}
95-
}
100+
}

packages/microservices/test/server/server-grpc.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ describe('ServerGrpc', () => {
357357
const fn = server.createStreamDuplexMethod(handler);
358358
const call = {
359359
on: (event, callback) => callback(),
360+
off: sinon.spy(),
360361
end: sinon.spy(),
361362
write: sinon.spy(),
362363
};

0 commit comments

Comments
 (0)