From e2f2415938a26548531c37453ce21cefa8d758f7 Mon Sep 17 00:00:00 2001 From: Ronan-Yann Lorin Date: Wed, 11 Oct 2023 08:51:48 +0200 Subject: [PATCH] Test case added for issue #193 --- ...ymbols.ts => get-matching-symbols.test.ts} | 0 .../api-next/subscription-registry.test.ts | 72 +++++++++++++++++++ 2 files changed, 72 insertions(+) rename src/tests/unit/api-next/{get-matching-symbols.ts => get-matching-symbols.test.ts} (100%) create mode 100644 src/tests/unit/api-next/subscription-registry.test.ts diff --git a/src/tests/unit/api-next/get-matching-symbols.ts b/src/tests/unit/api-next/get-matching-symbols.test.ts similarity index 100% rename from src/tests/unit/api-next/get-matching-symbols.ts rename to src/tests/unit/api-next/get-matching-symbols.test.ts diff --git a/src/tests/unit/api-next/subscription-registry.test.ts b/src/tests/unit/api-next/subscription-registry.test.ts new file mode 100644 index 00000000..29e380c7 --- /dev/null +++ b/src/tests/unit/api-next/subscription-registry.test.ts @@ -0,0 +1,72 @@ +import { Subscription } from "rxjs"; +import { IBApiNext, IBApiNextError } from "../../.."; + +const _awaitTimeout = (delay: number): Promise => + new Promise((resolve): NodeJS.Timeout => setTimeout(resolve, delay * 1000)); + +describe("Subscription registry Tests", () => { + jest.setTimeout(20000); + + const clientId = Math.floor(Math.random() * 32766) + 1; // ensure unique client + + let subscription$: Subscription; + let api: IBApiNext; + let error$: Subscription; + + beforeEach(() => { + api = new IBApiNext(); + + if (!error$) { + error$ = api.errorSubject.subscribe((error) => { + if (error.reqId === -1) { + console.warn(`${error.error.message} (Error #${error.code})`); + } else { + console.error( + `${error.error.message} (Error #${error.code}) ${ + error.advancedOrderReject ? error.advancedOrderReject : "" + }`, + ); + } + }); + } + + try { + api.connect(clientId); + } catch (error) { + console.error(error.message); + } + }); + + afterEach(() => { + if (api) { + api.disconnect(); + api = undefined; + } + }); + + it("Twice the same event callback bug", (done) => { + subscription$ = api.getOpenOrders().subscribe({ + next: (data) => { + console.log(data); + }, + error: (err: IBApiNextError) => { + console.error(`getOpenOrders failed with '${err.error.message}'`); + }, + }); + + api + .getAllOpenOrders() + .then((orders) => { + console.log(orders); + done(); + }) + .catch((err: IBApiNextError) => { + console.error(`getAllOpenOrders failed with '${err}'`); + }); + + // awaitTimeout(15).then(() => { + // subscription$.unsubscribe(); + // done(); + // }); + }); +});