diff --git a/setupTests-integration.js b/setupTests-integration.js index 0fa95dfe5..cfa7652f5 100644 --- a/setupTests-integration.js +++ b/setupTests-integration.js @@ -15,6 +15,7 @@ import { } from './__tests__/integration/helpers/wallet-precalculation.helper'; import { GenesisWalletHelper } from './__tests__/integration/helpers/genesis-wallet.helper'; import { generateWalletHelper, waitNextBlock, waitTxConfirmed } from './__tests__/integration/helpers/wallet.helper'; +import { stopGLLBackgroundTask } from './src/sync/gll'; config.setTxMiningUrl(TX_MINING_URL); @@ -97,3 +98,6 @@ expect.extend({ } } }); + +// Stop gll interval to avoid background tasks during tests +stopGLLBackgroundTask(); diff --git a/setupTests.js b/setupTests.js index 681eb53a9..9dea0a63a 100644 --- a/setupTests.js +++ b/setupTests.js @@ -10,6 +10,7 @@ process.env.NODE_ENV = 'test'; // Mocking localStorage for tests import 'jest-localstorage-mock'; import helpers from './src/utils/helpers'; +import { stopGLLBackgroundTask } from './src/sync/gll'; // Mocking WebSocket for tests import { Server, WebSocket } from 'mock-socket'; global.WebSocket = WebSocket; @@ -75,3 +76,6 @@ expect.extend({ const { TextEncoder, TextDecoder } = require('util'); global.TextEncoder = TextEncoder; global.TextDecoder = TextDecoder; + +// Stop gll interval to avoid background tasks during tests +stopGLLBackgroundTask(); diff --git a/src/lib.ts b/src/lib.ts index 4e154e9d5..77f902132 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -56,6 +56,7 @@ import { TransactionTemplateBuilder, WalletTxTemplateInterpreter, } from './template/transaction'; +import { stopGLLBackgroundTask } from './sync/gll'; export { PartialTx, @@ -117,6 +118,7 @@ export { TransactionTemplate, TransactionTemplateBuilder, WalletTxTemplateInterpreter, + stopGLLBackgroundTask, }; export * from './nano_contracts/types'; diff --git a/src/models/promise_queue.ts b/src/models/promise_queue.ts index 7541e8ddc..d1e6bc067 100644 --- a/src/models/promise_queue.ts +++ b/src/models/promise_queue.ts @@ -43,11 +43,13 @@ export default class PromiseQueue extends EventEmitter { #intervalId: ReturnType | null = null; - constructor() { + constructor(autostart: boolean = true) { super(); this.#queue = new PriorityQueue<() => Promise>(); this.#jobsRunning = 0; - this.#startInterval(); + if (autostart) { + this.#startInterval(); + } } /** @@ -75,6 +77,13 @@ export default class PromiseQueue extends EventEmitter { } } + /** + * Allow users to stop the background interval so there is no hanging tasks. + */ + stopBackgroundTask() { + this.#clearInterval(); + } + /** * Check if the PromiseQueue is paused. */ diff --git a/src/sync/gll.ts b/src/sync/gll.ts index c545eea72..0574b7af1 100644 --- a/src/sync/gll.ts +++ b/src/sync/gll.ts @@ -1,3 +1,9 @@ +/** + * Copyright (c) Hathor Labs and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ import PromiseQueue from '../models/promise_queue'; const MAX_CONCURRENT_LOAD_TASKS = 3; @@ -6,3 +12,7 @@ const GLL = new PromiseQueue(); GLL.concurrent = MAX_CONCURRENT_LOAD_TASKS; export default GLL; + +export function stopGLLBackgroundTask() { + GLL.stopBackgroundTask(); +} diff --git a/src/sync/stream.ts b/src/sync/stream.ts index b80f0e49a..957a60340 100644 --- a/src/sync/stream.ts +++ b/src/sync/stream.ts @@ -1,3 +1,9 @@ +/** + * Copyright (c) Hathor Labs and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ import { Address as BitcoreAddress, HDPublicKey } from 'bitcore-lib'; import queueMicrotask from 'queue-microtask'; import FullNodeConnection from '../new/connection';