Skip to content

Commit aa2e8f9

Browse files
committed
add tests for bindEvent empty messageHandlers.
1 parent 28ca9ba commit aa2e8f9

2 files changed

Lines changed: 81 additions & 16 deletions

File tree

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ import { UserDto } from '../src/kafka/dtos/user.dto';
1818
import { UserEntity } from '../src/kafka/entities/user.entity';
1919
import { BusinessDto } from '../src/kafka/dtos/business.dto';
2020
import { BusinessEntity } from '../src/kafka/entities/business.entity';
21+
import * as async from 'async';
2122

2223
@Catch()
2324
class KafkaExceptionFilter implements ExceptionFilter {
2425
catch(exception: HttpException, host: ArgumentsHost): any {
25-
console.log(exception);
26+
throw exception;
2627
}
2728
}
2829
@Catch()
2930
class RpcErrorFilter implements RpcExceptionFilter {
3031
catch(exception: RpcException): Observable<any> {
31-
console.log(exception);
3232
return throwError(exception);
3333
}
3434
}
@@ -108,13 +108,27 @@ describe('Kafka transport', () => {
108108
}).timeout(50000);
109109

110110
it(`/POST (async command create business`, () => {
111-
const newBusiness: BusinessEntity = new BusinessEntity(businessDto);
112111
return request(server)
113112
.post('/business')
114113
.send(businessDto)
115114
.expect(200);
116115
}).timeout(50000);
117116

117+
it(`/POST (async command create user) Concurrency Test`, (done) => {
118+
const tasks = [];
119+
for (let concurrencyKey = 0; concurrencyKey < 100; concurrencyKey++) {
120+
tasks.push((cb) => {
121+
const innerUserDto = JSON.parse(JSON.stringify(userDto));
122+
innerUserDto.name += `+${concurrencyKey}`;
123+
request(server)
124+
.post('/user')
125+
.send(userDto)
126+
.expect(200, cb);
127+
});
128+
}
129+
async.parallel(tasks, done);
130+
}).timeout(50000);
131+
118132
after(`Stopping Kafka app`, async () => {
119133
await app.close();
120134
});
Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { expect } from 'chai';
22
import * as sinon from 'sinon';
3-
import { NO_MESSAGE_HANDLER } from '../../constants';
4-
import { ServerTCP } from '../../server/server-tcp';
53
import { ServerKafka } from '../../server';
6-
import { Consumer, IHeaders, KafkaMessage } from '../../external/kafka.interface';
4+
import { Logger } from '@nestjs/common';
5+
6+
class NoopLogger extends Logger {
7+
log(message: any, context?: string): void {}
8+
error(message: any, trace?: string, context?: string): void {}
9+
warn(message: any, context?: string): void {}
10+
}
711

812
describe('ServerKafka', () => {
913
let server: ServerKafka;
@@ -12,9 +16,6 @@ describe('ServerKafka', () => {
1216
server = new ServerKafka({});
1317
});
1418

15-
afterEach(() => {
16-
server.close();
17-
});
1819
describe('close', () => {
1920
it('should close server', () => {
2021
server.close();
@@ -23,17 +24,67 @@ describe('ServerKafka', () => {
2324
expect(server.client).to.be.null;
2425
});
2526
});
27+
28+
let callback: sinon.SinonSpy;
29+
let bindEventsStub: sinon.SinonStub;
30+
let connect: sinon.SinonSpy;
31+
let subscribe: sinon.SinonSpy;
32+
let run: sinon.SinonSpy;
33+
let consumerStub: sinon.SinonStub;
34+
let producerStub: sinon.SinonStub;
35+
let client;
36+
beforeEach(() => {
37+
callback = sinon.spy();
38+
connect = sinon.spy();
39+
subscribe = sinon.spy();
40+
run = sinon.spy();
41+
bindEventsStub = sinon
42+
.stub(server, 'bindEvents')
43+
.callsFake(() => ({} as any));
44+
consumerStub = sinon.stub(server, 'consumer')
45+
.callsFake( () => {
46+
return {
47+
connect,
48+
subscribe,
49+
run,
50+
};
51+
});
52+
producerStub = sinon.stub(server, 'producer')
53+
.callsFake( () => {
54+
return {
55+
connect,
56+
};
57+
});
58+
client = {
59+
consumer: consumerStub,
60+
producer: producerStub,
61+
};
62+
sinon.stub(server, 'createClient').callsFake(() => client);
63+
});
64+
2665
describe('listen', () => {
27-
it('should start server', async () => {
28-
const callback = sinon.spy();
66+
it('should call "bindEvents"', async () => {
67+
await server.listen(callback);
68+
expect(bindEventsStub.called).to.be.true;
69+
});
70+
it('should call "client.start"', async () => {
71+
72+
await server.listen(callback);
73+
expect(client.producer.called).to.be.true;
74+
});
75+
it('should call callback', async () => {
2976
await server.listen(callback);
3077
expect(callback.called).to.be.true;
3178
});
32-
it('should have kafka, producer and consumer connected', async () => {
33-
await server.listen(() => {});
34-
expect(server.client).to.be.not.null;
35-
expect(server.producer).to.be.not.null;
36-
expect(server.consumer).to.be.not.null;
79+
});
80+
81+
describe('bindEvents', () => {
82+
it('should not call subscribe nor run on consumer when there are no messageHandlers', async () => {
83+
(server as any).logger = new NoopLogger();
84+
await server.bindEvents(server.consumer);
85+
expect(subscribe.called).to.be.not.true;
86+
expect(run.called).to.be.not.true;
87+
expect(connect.called).to.be.not.true;
3788
});
3889
});
3990
});

0 commit comments

Comments
 (0)