|
| 1 | +import "mocha"; |
| 2 | + |
| 3 | +import { emitterFor, HTTP, Mode, Message, Emitter } from "../../src"; |
| 4 | + |
| 5 | +import { fixture, assertStructured } from "./emitter_factory_test"; |
| 6 | + |
| 7 | +import { rejects, doesNotReject } from "assert"; |
| 8 | + |
| 9 | +describe("Emitter Singleton", () => { |
| 10 | + it("emit", async () => { |
| 11 | + let msg: Message | unknown = undefined; |
| 12 | + const fn = async (message: Message) => { |
| 13 | + msg = message; |
| 14 | + }; |
| 15 | + const emitter = emitterFor(fn, { binding: HTTP, mode: Mode.STRUCTURED }); |
| 16 | + Emitter.on("cloudevent", emitter); |
| 17 | + fixture.emit(); |
| 18 | + while (msg === undefined) { |
| 19 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 20 | + } |
| 21 | + let body: unknown = (<Message>(<unknown>msg)).body; |
| 22 | + if (typeof body === "string") { |
| 23 | + body = JSON.parse(body); |
| 24 | + } |
| 25 | + assertStructured({ ...(<any>body), ...(<Message>(<unknown>msg)).headers }); |
| 26 | + }); |
| 27 | + |
| 28 | + it("emitWithEnsureDelivery", async () => { |
| 29 | + let msg: Message | unknown = undefined; |
| 30 | + const fn = async (message: Message) => { |
| 31 | + msg = message; |
| 32 | + }; |
| 33 | + const emitter = emitterFor(fn, { binding: HTTP, mode: Mode.STRUCTURED }); |
| 34 | + Emitter.on("cloudevent", emitter); |
| 35 | + await fixture.emit(true); |
| 36 | + let body: any = (<Message>msg).body; |
| 37 | + if (typeof body === "string") { |
| 38 | + body = JSON.parse(body); |
| 39 | + } |
| 40 | + assertStructured({ ...(<any>body), ...(<Message>(<unknown>msg)).headers }); |
| 41 | + }); |
| 42 | + |
| 43 | + it("emitWithEnsureDeliveryError", async () => { |
| 44 | + const emitter = async () => { |
| 45 | + throw new Error("Not sent"); |
| 46 | + }; |
| 47 | + Emitter.on("cloudevent", emitter); |
| 48 | + // Should fail with emitWithEnsureDelivery |
| 49 | + await rejects(() => fixture.emit(true)); |
| 50 | + // Should not fail with emitWithEnsureDelivery |
| 51 | + // Work locally but not on Github Actions |
| 52 | + if (!process.env.GITHUB_WORKFLOW) { |
| 53 | + await doesNotReject(() => fixture.emit(false)); |
| 54 | + } |
| 55 | + }); |
| 56 | +}); |
0 commit comments