-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
190 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { setTimeout } from 'node:timers/promises'; | ||
|
||
import { Controller, Get, Logger } from '@nestjs/common'; | ||
|
||
import { AppService } from './app.service'; | ||
|
||
@Controller() | ||
export class AppController { | ||
private logger = new Logger('AppController'); | ||
constructor(private readonly appService: AppService) {} | ||
|
||
@Get('/') | ||
async index() { | ||
this.logger.debug('Start index'); | ||
await setTimeout(10); // Imitate running process | ||
this.appService.addToShared(new Date().toUTCString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { INestMicroservice, Provider } from '@nestjs/common'; | ||
import { | ||
ClientKafka, | ||
ClientProxyFactory, | ||
Transport, | ||
} from '@nestjs/microservices'; | ||
import { Test } from '@nestjs/testing'; | ||
import expect from 'expect'; | ||
import { createAwaitableEmit } from '../src'; | ||
import { AppModule, configureApp } from './app.module'; | ||
import { AppService } from './app.service'; | ||
|
||
describe('AppController (e2e)', () => { | ||
let app: INestMicroservice; | ||
let clientKafka: ClientKafka; | ||
let service: AppService; | ||
|
||
const { emitMessage, AwaitableEmitInterceptor, dispose } = | ||
createAwaitableEmit({ | ||
getKafkaClient: () => clientKafka, | ||
brokers: ['127.0.0.1:9092'], | ||
}); | ||
|
||
before(async () => { | ||
const providers: Provider[] = [ | ||
{ | ||
provide: 'KAFKA_CLIENT', | ||
useFactory: () => { | ||
return ClientProxyFactory.create({ | ||
transport: Transport.KAFKA, | ||
options: { | ||
client: { | ||
clientId: 'KAFKA_CLIENT', | ||
brokers: ['127.0.0.1:9092'], | ||
}, | ||
}, | ||
}); | ||
}, | ||
}, | ||
]; | ||
const testingModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
providers, | ||
}).compile(); | ||
|
||
app = testingModule.createNestMicroservice({ | ||
transport: Transport.KAFKA, | ||
options: { | ||
client: { | ||
clientId: 'KAFKA_CLIENT', | ||
brokers: ['127.0.0.1:9092'], | ||
}, | ||
}, | ||
}); | ||
|
||
configureApp(app); | ||
|
||
app.useGlobalInterceptors(new AwaitableEmitInterceptor()); | ||
|
||
await app.init(); | ||
await app.listen(); | ||
|
||
clientKafka = app.get<ClientKafka>('KAFKA_CLIENT'); | ||
service = app.get(AppService); | ||
}); | ||
|
||
after(async () => { | ||
await dispose(); | ||
await clientKafka?.close(); | ||
await app?.close(); | ||
}); | ||
|
||
it('smoke', () => { | ||
expect(clientKafka).toBeTruthy(); | ||
}); | ||
|
||
it('test emit message', async () => { | ||
await emitMessage('user-created', { | ||
key: Date.now.toString(), | ||
value: { name: 'Bob' }, | ||
}); | ||
expect(service.shared.at(-1)).toEqual({ name: 'Bob' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
INestApplication, | ||
INestMicroservice, | ||
Logger, | ||
Module, | ||
} from '@nestjs/common'; | ||
|
||
import { NestologModule } from '../src'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
|
||
@Module({ | ||
controllers: [AppController], | ||
imports: [NestologModule.register({})], | ||
providers: [AppService], | ||
}) | ||
export class AppModule {} | ||
|
||
export function configureApp(app: INestApplication | INestMicroservice) { | ||
const oldfn = app.registerRequestByContextId.bind(app); | ||
|
||
app.registerRequestByContextId = function (req, contextId) { | ||
console.log('contextId', contextId); | ||
debugger; | ||
return oldfn.apply(this, arguments as any); | ||
}; | ||
|
||
if ('use' in app && typeof app.use === 'function') { | ||
// not alwasy may be handled | ||
// const server = app.getHttpServer(); | ||
// server.on('request', function (req, res) { | ||
// debugger; | ||
// }); | ||
|
||
app.use(function (req, res, next) { | ||
// debugger; | ||
next(); | ||
}); | ||
} | ||
|
||
const logger = app.get(Logger); | ||
|
||
logger.log('Application configured'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { NestoLogger } from '../src'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
constructor(private logger: NestoLogger) {} | ||
|
||
shared: string[] = []; | ||
|
||
addToShared(value: string) { | ||
this.shared.push(value); | ||
|
||
this.logger.debug(`Value to shared added '${value}'`, 'AppService'); | ||
} | ||
|
||
getHello(): string { | ||
return 'Hello World! ' + new Date().toUTCString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
|
||
import { NestoLogger } from '../src'; | ||
import { AppModule, configureApp } from './app.module'; | ||
|
||
async function main() { | ||
const app = await NestFactory.create(AppModule, { | ||
logger: NestoLogger.create(), | ||
}); | ||
configureApp(app); | ||
|
||
await app.init(); | ||
await app.listen(3000); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters