Skip to content

Commit 0e9dbee

Browse files
committed
GRPC Streaming tests updated with support both Streaming decorators
- RX Streaming test - Call streaming test
1 parent 9f10e08 commit 0e9dbee

3 files changed

Lines changed: 49 additions & 4 deletions

File tree

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('GRPC transport', () => {
5252
.expect(200, { result: 15 });
5353
});
5454

55-
it('GRPC Sending and receiving Stream', async () => {
55+
it('GRPC Sending and receiving Stream from RX handler', async () => {
5656

5757
const callHandler = client.SumStream();
5858

@@ -77,6 +77,31 @@ describe('GRPC transport', () => {
7777

7878
});
7979

80+
it('GRPC Sending and receiving Stream from Call Passthrough handler', async () => {
81+
82+
const callHandler = client.SumStreamPass();
83+
84+
callHandler.on('data', (msg: number) => {
85+
// Do deep comparison (to.eql)
86+
expect(msg).to.eql({result: 15});
87+
callHandler.cancel();
88+
});
89+
90+
callHandler.on('error', (err: any) => {
91+
// We want to fail only on real errors while Cancellation error
92+
// is expected
93+
if (String(err).toLowerCase().indexOf('cancelled') === -1) {
94+
fail('gRPC Stream error happened, error: ' + err);
95+
}
96+
});
97+
98+
return new Promise((resolve, reject) => {
99+
callHandler.write({data: [1, 2, 3, 4, 5]});
100+
setTimeout(() => resolve(), 1000);
101+
});
102+
103+
});
104+
80105
after(async () => {
81106
await app.close();
82107
client.close;

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Body, Controller, HttpCode, Post } from '@nestjs/common';
2-
import { Client, ClientGrpc, GrpcMethod, GrpcStream, Transport } from '@nestjs/microservices';
2+
import {
3+
Client, ClientGrpc, GrpcMethod,
4+
GrpcStreamMethod, GrpcStreamCall, Transport,
5+
} from '@nestjs/microservices';
36
import { join } from 'path';
47
import { Observable, of } from 'rxjs';
58

@@ -28,8 +31,24 @@ export class GrpcController {
2831
});
2932
}
3033

31-
@GrpcStream('Math')
32-
async sumStream(stream: any) {
34+
@GrpcStreamMethod('Math')
35+
async sumStream(messages: Observable<any>): Promise<any> {
36+
// Form a resulting promise
37+
return new Promise<any>((resolve, reject) => {
38+
// Subscribe for a message to for a test answer
39+
messages.subscribe(msg => {
40+
// Resolve with reduce function
41+
resolve({
42+
result: msg.data.reduce((a, b) => a + b),
43+
});
44+
}, err => {
45+
reject(err);
46+
});
47+
});
48+
}
49+
50+
@GrpcStreamCall('Math')
51+
async sumStreamPass(stream: any) {
3352
stream.on('data', (msg: any) => {
3453
stream.write({result: msg.data.reduce((a, b) => a + b)});
3554
});

integration/microservices/src/grpc/math.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package math;
55
service Math {
66
rpc Sum (RequestSum) returns (SumResult);
77
rpc SumStream(stream RequestSum) returns(stream SumResult);
8+
rpc SumStreamPass(stream RequestSum) returns(stream SumResult);
89
}
910

1011
message SumResult {

0 commit comments

Comments
 (0)