8000 bugfix(): normalize TCP pattern (event) · ITCSsDeveloper/nest@d9a28f2 · GitHub
Skip to content

Commit d9a28f2

Browse files
bugfix(): normalize TCP pattern (event)
1 parent 6d9f15a commit d9a28f2

4 files changed

Lines changed: 44 additions & 14 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { INestApplication } from '@nestjs/common';
22
import { Transport } from '@nestjs/microservices';
33
import { Test } from '@nestjs/testing';
4+
import { expect } from 'chai';
45
import * as request from 'supertest';
6+
import { AppController } from '../src/app.controller';
57
import { ApplicationModule } from '../src/app.module';
68

79
describe('RPC transport', () => {
@@ -76,6 +78,18 @@ describe('RPC transport', () => {
7678
.expect(500);
7779
});
7880

81+
it(`/POST (event notification)`, done => {
82+
request(server)
83+
.post('/notify')
84+
.send([1, 2, 3, 4, 5])
85+
.end(() => {
86+
setTimeout(() => {
87+
expect(AppController.IS_NOTIFIED).to.be.true;
88+
done();
89+
}, 1000);
90+
});
91+
});
92+
7993
afterEach(async () => {
8094
await app.close();
8195
});

integration/microservices/src/app.controller.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import { Controller, Post, Body, Query, HttpCode } from '@nestjs/common';
1+
import { Body, Controller, HttpCode, Post, Query } from '@nestjs/common';
22
import {
33
Client,
4-
MessagePattern,
54
ClientProxy,
5+
EventPattern,
6+
MessagePattern,
67
Transport,
78
} from '@nestjs/microservices';
8-
import { Observable, of, from } from 'rxjs';
9+
import { from, Observable, of } from 'rxjs';
910
import { scan } from 'rxjs/operators';
1011

1112
@Controller()
1213
export class AppController {
14+
static IS_NOTIFIED = false;
15+
1316
@Client({ transport: Transport.TCP })
1417
client: ClientProxy;
1518

@@ -62,4 +65,14 @@ export class AppController {
6265
streaming(data: number[]): Observable<number> {
6366
return from(data);
6467
}
68+
69+
@Post('notify')
70+
async sendNotification(): Promise<any> {
71+
return this.client.emit<number>('notification', true);
72+
}
73+
74+
@EventPattern('notification')
75+
eventHandler(data: boolean) {
76+
AppController.IS_NOTIFIED = data;
77+
}
6578
}

packages/microservices/client/client-tcp.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ export class ClientTCP extends ClientProxy {
5252
share(),
5353
);
5454

55-
this.socket.connect(
56-
this.port,
57-
this.host,
58-
);
55+
this.socket.connect(this.port, this.host);
5956
this.connection = source$.toPromise();
6057
return this.connection;
6158
}
@@ -122,6 +119,9 @@ export class ClientTCP extends ClientProxy {
122119
}
123120

124121
protected async dispatchEvent(packet: ReadPacket): Promise<any> {
125-
return this.socket.sendMessage(packet);
122+
return this.socket.sendMessage({
123+
...packet,
124+
pattern: this.normalizePattern(packet.pattern),
125+
});
126126
}
127127
}

packages/microservices/test/client/client-proxy.spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,19 @@ describe('ClientProxy', () => {
119119
});
120120

121121
describe('createObserver', () => {
122-
let testClient: TestClientProxy;
123-
beforeEach(() => {
124-
testClient = new TestClientProxy();
125-
});
126-
127122
it(`should return function`, () => {
128-
expect(typeof testClient['createObserver'](null)).to.be.eql('function');
123+
const testClientProxy = new TestClientProxy();
124+
expect(typeof testClientProxy['createObserver']({} as any)).to.be.eql(
125+
'function',
126+
);
129127
});
130128

131129
describe('returned function calls', () => {
130+
let testClient: TestClientProxy;
131+
beforeEach(() => {
132+
testClient = new TestClientProxy();
133+
});
134+
132135
it(`"error" when first parameter is not null or undefined`, () => {
133136
const err = 'test';
134137
const error = sinon.spy();

0 commit comments

Comments
 (0)