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(); + // }); + }); +});