-
-
Save ivcosla/908e717af746801787cced1f80c1c327 to your computer and use it in GitHub Desktop.
double spending in nestjs
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
import { Module } from "@nestjs/common"; | |
import { NestFactory } from "@nestjs/core"; | |
import { CqrsModule, EventBus, EventsHandler } from "@nestjs/cqrs"; | |
class Event {} | |
@EventsHandler(Event) | |
class EvntHandler { | |
async handle(event: Event) { | |
console.log("Event handled"); | |
} | |
} | |
@Module({ | |
imports: [CqrsModule], | |
providers: [EvntHandler], | |
}) | |
class Base {} | |
describe("double", () => { | |
it("should be true", async () => { | |
// if we create a microservice, the event is delivered twice | |
const app = await NestFactory.createMicroservice(Base); | |
// where if we create a regular app, the event is delivered once | |
// const app = await NestFactory.create(Base); | |
await app.init(); | |
const eventBus = await app.resolve(EventBus); | |
eventBus.publish(new Event()); | |
// sleep for 0.5 seconds to see if the event is handled | |
await new Promise((resolve) => setTimeout(resolve, 500)); | |
expect(true).toBe(true); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment