Skip to content

Commit 3273f9f

Browse files
committed
add tests for close and listen for kafka server.
1 parent b5d8965 commit 3273f9f

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

integration/microservices/src/kafka/kafka.messages.controller.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { UserEntity } from './entities/user.entity';
77
import { BusinessEntity } from './entities/business.entity';
88
import * as util from 'util';
99
import { UserDto } from './dtos/user.dto';
10+
import { KafkaClient } from 'kafka-node';
1011

1112
@Controller()
1213
export class KafkaMessagesController {
@@ -44,4 +45,7 @@ export class KafkaMessagesController {
4445
async createBusiness(params: {value: {business: BusinessDto}}) {
4546
return new BusinessEntity(params.value.business);
4647
}
48+
49+
async doSomething() {
50+
}
4751
}

packages/microservices/server/server-kafka.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ let kafkaPackage: any = {};
3131

3232
export class ServerKafka extends Server implements CustomTransportStrategy {
3333
protected readonly logger = new Logger(ServerKafka.name);
34-
private client: Kafka = null;
35-
private consumer: Consumer = null;
36-
private producer: Producer = null;
34+
public client: Kafka = null;
35+
public consumer: Consumer = null;
36+
public producer: Producer = null;
3737
private readonly brokers: string[];
3838
private readonly clientId: string;
3939
private readonly groupId: string;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { expect } from 'chai';
2+
import * as sinon from 'sinon';
3+
import { NO_MESSAGE_HANDLER } from '../../constants';
4+
import { ServerTCP } from '../../server/server-tcp';
5+
import { ServerKafka } from '../../server';
6+
import { Consumer, IHeaders, KafkaMessage } from '../../external/kafka.interface';
7+
8+
describe('ServerKafka', () => {
9+
let server: ServerKafka;
10+
11+
beforeEach(() => {
12+
server = new ServerKafka({});
13+
});
14+
15+
afterEach(() => {
16+
server.close();
17+
});
18+
describe('close', () => {
19+
it('should close server', () => {
20+
server.close();
21+
expect(server.consumer).to.be.null;
22+
expect(server.producer).to.be.null;
23+
expect(server.client).to.be.null;
24+
});
25+
});
26+
describe('listen', () => {
27+
it('should start server', async () => {
28+
const callback = sinon.spy();
29+
await server.listen(callback);
30+
expect(callback.called).to.be.true;
31+
});
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;
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)