From 220ebd031b5ac7d6af119372e36b9f26c98f66d4 Mon Sep 17 00:00:00 2001 From: holgerd77 Date: Thu, 12 Aug 2021 15:38:24 +0200 Subject: [PATCH] client -> tx pool: improved pool closing, added basic unit test setup --- packages/client/lib/service/txpool.ts | 24 +++++++++++------- packages/client/test/service/txpool.spec.ts | 27 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 packages/client/test/service/txpool.spec.ts diff --git a/packages/client/lib/service/txpool.ts b/packages/client/lib/service/txpool.ts index 0729530cb2..04fbad5001 100644 --- a/packages/client/lib/service/txpool.ts +++ b/packages/client/lib/service/txpool.ts @@ -30,6 +30,9 @@ export class TxPool extends EventEmitter { private opened: boolean + // eslint-disable-next-line no-undef + private _logInterval: NodeJS.Timeout | null + /** * List of pending tx hashes to avoid double requests */ @@ -51,6 +54,7 @@ export class TxPool extends EventEmitter { super() this.config = options.config + this._logInterval = null this.pool = new Map() this.opened = false @@ -71,7 +75,7 @@ export class TxPool extends EventEmitter { } this.opened = true - setInterval(this._logPoolStats.bind(this), LOG_STATISTICS_INTERVAL) + this._logInterval = setInterval(this._logPoolStats.bind(this), LOG_STATISTICS_INTERVAL) return true } @@ -125,6 +129,16 @@ export class TxPool extends EventEmitter { } } + /** + * Close pool + */ + async close() { + this.pool.clear() + this.opened = false + // eslint-disable-next-line no-undef + clearInterval(this._logInterval as NodeJS.Timeout) + } + _logPoolStats() { let count = 0 this.pool.forEach((poolObjects) => { @@ -132,12 +146,4 @@ export class TxPool extends EventEmitter { }) this.config.logger.info(`TxPool Statistics transactions=${count} senders=${this.pool.size}`) } - - /** - * Close pool - */ - async close() { - //this.pool.clear() - this.opened = false - } } diff --git a/packages/client/test/service/txpool.spec.ts b/packages/client/test/service/txpool.spec.ts new file mode 100644 index 0000000000..0319031753 --- /dev/null +++ b/packages/client/test/service/txpool.spec.ts @@ -0,0 +1,27 @@ +import tape from 'tape-catch' +import { Config } from '../../lib/config' + +tape('[TxPool]', async (t) => { + const { TxPool } = await import('../../lib/service/txpool') + + t.test('should initialize correctly', (t) => { + const config = new Config({ transports: [], loglevel: 'error' }) + const pool = new TxPool({ config }) + t.equal(pool.pool.size, 0, 'pool empty') + t.notOk((pool as any).opened, 'pool not opened yet') + t.end() + }) + + t.test('should open/close', async (t) => { + t.plan(3) + const config = new Config({ transports: [], loglevel: 'error' }) + const pool = new TxPool({ config }) + + await pool.open() + t.ok((pool as any).opened, 'pool opened') + t.equals(await pool.open(), false, 'already opened') + await pool.close() + t.notOk((pool as any).opened, 'closed') + t.end() + }) +})