-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathsum-kafka.spec.ts
138 lines (122 loc) · 3.81 KB
/
sum-kafka.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { INestApplication } from '@nestjs/common';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import * as request from 'supertest';
import { BusinessDto } from '../src/kafka/dtos/business.dto';
import { UserDto } from '../src/kafka/dtos/user.dto';
import { UserEntity } from '../src/kafka/entities/user.entity';
import { KafkaController } from '../src/kafka/kafka.controller';
import { KafkaMessagesController } from '../src/kafka/kafka.messages.controller';
/**
* Skip this flaky test in CI/CD pipeline as it frequently
* fails to connect to Kafka container in the cloud.
*/
describe('Kafka transport', function () {
let server: any;
let app: INestApplication;
// set timeout to be longer (especially for the after hook)
this.timeout(50000);
this.retries(10);
before(`Start Kafka app`, async function () {
const module = await Test.createTestingModule({
controllers: [KafkaController, KafkaMessagesController],
}).compile();
app = module.createNestApplication();
server = app.getHttpAdapter().getInstance();
app.connectMicroservice<MicroserviceOptions>({
transport: Transport.KAFKA,
options: {
client: {
brokers: ['localhost:9092'],
},
},
});
app.enableShutdownHooks();
await app.startAllMicroservices();
await app.init();
});
it(`/POST (sync sum kafka message)`, function () {
return request(server)
.post('/mathSumSyncKafkaMessage')
.send([1, 2, 3, 4, 5])
.expect(200)
.expect(200, '15');
});
it(`/POST (sync sum kafka(ish) message without key and only the value)`, () => {
return request(server)
.post('/mathSumSyncWithoutKey')
.send([1, 2, 3, 4, 5])
.expect(200)
.expect(200, '15');
});
it(`/POST (sync sum plain object)`, () => {
return request(server)
.post('/mathSumSyncPlainObject')
.send([1, 2, 3, 4, 5])
.expect(200)
.expect(200, '15');
});
it(`/POST (sync sum array)`, () => {
return request(server)
.post('/mathSumSyncArray')
.send([1, 2, 3, 4, 5])
.expect(200)
.expect(200, '15');
});
it(`/POST (sync sum string)`, () => {
return request(server)
.post('/mathSumSyncString')
.send([1, 2, 3, 4, 5])
.expect(200)
.expect(200, '15');
});
it(`/POST (sync sum number)`, () => {
return request(server)
.post('/mathSumSyncNumber')
.send([12345])
.expect(200)
.expect(200, '15');
});
it(`/POST (async event notification)`, done => {
request(server)
.post('/notify')
.send()
.end(() => {
setTimeout(() => {
expect(KafkaController.IS_NOTIFIED).to.be.true;
done();
}, 1000);
});
});
const userDto: UserDto = {
email: '[email protected]',
name: 'Ben',
phone: '1112223331',
years: 33,
};
const newUser: UserEntity = new UserEntity(userDto);
const businessDto: BusinessDto = {
name: 'Example',
phone: '2233441122',
user: newUser,
};
it(`/POST (sync command create user)`, () => {
return request(server).post('/user').send(userDto).expect(200);
});
it(`/POST (sync command create business`, () => {
return request(server).post('/business').send(businessDto).expect(200);
});
it(`/POST (sync command create user) Concurrency Test`, async () => {
const promises = [];
for (let concurrencyKey = 0; concurrencyKey < 100; concurrencyKey++) {
const innerUserDto = JSON.parse(JSON.stringify(userDto));
innerUserDto.name += `+${concurrencyKey}`;
promises.push(request(server).post('/user').send(userDto).expect(200));
}
await Promise.all(promises);
});
after(`Stopping Kafka app`, async () => {
await app.close();
});
});