|
| 1 | +import { |
| 2 | + ArgumentsHost, |
| 3 | + Catch, |
| 4 | + ExceptionFilter, |
| 5 | + HttpException, |
| 6 | + INestApplication, |
| 7 | + RpcExceptionFilter, |
| 8 | +} from '@nestjs/common'; |
| 9 | +import { RpcException, Transport } from '@nestjs/microservices'; |
| 10 | +import { Test } from '@nestjs/testing'; |
| 11 | +import { expect } from 'chai'; |
| 12 | +import * as request from 'supertest'; |
| 13 | +import { KafkaController } from '../src/kafka/kafka.controller'; |
| 14 | +import { APP_FILTER } from '@nestjs/core'; |
| 15 | +import { Observable, throwError } from 'rxjs'; |
| 16 | +import { KafkaMessagesController } from '../src/kafka/kafka.messages.controller'; |
| 17 | +import { UserDto } from '../src/kafka/dtos/user.dto'; |
| 18 | +import { UserEntity } from '../src/kafka/entities/user.entity'; |
| 19 | +import { BusinessDto } from '../src/kafka/dtos/business.dto'; |
| 20 | +import { BusinessEntity } from '../src/kafka/entities/business.entity'; |
| 21 | + |
| 22 | +@Catch() |
| 23 | +class KafkaExceptionFilter implements ExceptionFilter { |
| 24 | + catch(exception: HttpException, host: ArgumentsHost): any { |
| 25 | + console.log(exception); |
| 26 | + } |
| 27 | +} |
| 28 | +@Catch() |
| 29 | +class RpcErrorFilter implements RpcExceptionFilter { |
| 30 | + catch(exception: RpcException): Observable<any> { |
| 31 | + console.log(exception); |
| 32 | + return throwError(exception); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +describe('Kafka transport', () => { |
| 37 | + let server; |
| 38 | + let app: INestApplication; |
| 39 | + |
| 40 | + it(`Start Kafka app`, async () => { |
| 41 | + const module = await Test.createTestingModule({ |
| 42 | + controllers: [ |
| 43 | + KafkaController, |
| 44 | + KafkaMessagesController, |
| 45 | + ], |
| 46 | + providers: [ |
| 47 | + { |
| 48 | + provide: APP_FILTER, |
| 49 | + useClass: RpcErrorFilter, |
| 50 | + }, |
| 51 | + { |
| 52 | + provide: APP_FILTER, |
| 53 | + useClass: KafkaExceptionFilter, |
| 54 | + }, |
| 55 | + ], |
| 56 | + }).compile(); |
| 57 | + |
| 58 | + app = module.createNestApplication(); |
| 59 | + server = app.getHttpAdapter().getInstance(); |
| 60 | + |
| 61 | + app.connectMicroservice({ |
| 62 | + transport: Transport.KAFKA, |
| 63 | + options: { |
| 64 | + client: { |
| 65 | + brokers: ['localhost:9092'], |
| 66 | + }, |
| 67 | + }, |
| 68 | + }); |
| 69 | + await app.startAllMicroservicesAsync(); |
| 70 | + await app.init(); |
| 71 | + }).timeout(30000); |
| 72 | + |
| 73 | + it(`/POST (sync sum kafka message)`, () => { |
| 74 | + return request(server) |
| 75 | + .post('/mathSumSyncKafkaMessage') |
| 76 | + .send([1, 2, 3, 4, 5]) |
| 77 | + .expect(200) |
| 78 | + .expect(200, '15'); |
| 79 | + }); |
| 80 | + |
| 81 | + it(`/POST (sync sum kafka(ish) message without key and only the value)`, () => { |
| 82 | + return request(server) |
| 83 | + .post('/mathSumSyncWithoutKey') |
| 84 | + .send([1, 2, 3, 4, 5]) |
| 85 | + .expect(200) |
| 86 | + .expect(200, '15'); |
| 87 | + }); |
| 88 | + |
| 89 | + it(`/POST (sync sum plain object)`, () => { |
| 90 | + return request(server) |
| 91 | + .post('/mathSumSyncPlainObject') |
| 92 | + .send([1, 2, 3, 4, 5]) |
| 93 | + .expect(200) |
| 94 | + .expect(200, '15'); |
| 95 | + }); |
| 96 | + |
| 97 | + it(`/POST (sync sum array)`, () => { |
| 98 | + return request(server) |
| 99 | + .post('/mathSumSyncArray') |
| 100 | + .send([1, 2, 3, 4, 5]) |
| 101 | + .expect(200) |
| 102 | + .expect(200, '15'); |
| 103 | + }); |
| 104 | + |
| 105 | + it(`/POST (sync sum string)`, () => { |
| 106 | + return request(server) |
| 107 | + .post('/mathSumSyncString') |
| 108 | + .send([1, 2, 3, 4, 5]) |
| 109 | + .expect(200) |
| 110 | + .expect(200, '15'); |
| 111 | + }); |
| 112 | + |
| 113 | + it(`/POST (sync sum number)`, () => { |
| 114 | + return request(server) |
| 115 | + .post('/mathSumSyncNumber') |
| 116 | + .send([12345]) |
| 117 | + .expect(200) |
| 118 | + .expect(200, '15'); |
| 119 | + }); |
| 120 | + |
| 121 | + it(`/POST (async event notification)`, done => { |
| 122 | + request(server) |
| 123 | + .post('/notify') |
| 124 | + .send() |
| 125 | + .end(() => { |
| 126 | + setTimeout(() => { |
| 127 | + expect(KafkaController.IS_NOTIFIED).to.be.true; |
| 128 | + done(); |
| 129 | + }, 1000); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + const userDto: UserDto = { |
| 134 | + email: 'enriquebenavidesm@gmail.com', |
| 135 | + name: 'Ben', |
| 136 | + phone: '1112223331', |
| 137 | + years: 33, |
| 138 | + }; |
| 139 | + const newUser: UserEntity = new UserEntity(userDto); |
| 140 | + const businessDto: BusinessDto = { |
| 141 | + name: 'Example', |
| 142 | + phone: '2233441122', |
| 143 | + user: newUser, |
| 144 | + }; |
| 145 | + it(`/POST (sync command create user)`, () => { |
| 146 | + return request(server) |
| 147 | + .post('/user') |
| 148 | + .send(userDto) |
| 149 | + .expect(200); |
| 150 | + }); |
| 151 | + |
| 152 | + it(`/POST (sync command create business`, () => { |
| 153 | + return request(server) |
| 154 | + .post('/business') |
| 155 | + .send(businessDto) |
| 156 | + .expect(200); |
| 157 | + }); |
| 158 | + |
| 159 | + it(`/POST (sync command create user) Concurrency Test`, async () => { |
| 160 | + const promises = []; |
| 161 | + for (let concurrencyKey = 0; concurrencyKey < 100; concurrencyKey++) { |
| 162 | + const innerUserDto = JSON.parse(JSON.stringify(userDto)); |
| 163 | + innerUserDto.name += `+${concurrencyKey}`; |
| 164 | + promises.push(request(server).post('/user').send(userDto).expect(200)); |
| 165 | + } |
| 166 | + await Promise.all(promises); |
| 167 | + }); |
| 168 | + |
| 169 | + after(`Stopping Kafka app`, async () => { |
| 170 | + await app.close(); |
| 171 | + }); |
| 172 | + |
| 173 | +}).timeout(30000); |
0 commit comments