Skip to content

Commit

Permalink
client -> tx pool: improved pool closing, added basic unit test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 committed Aug 12, 2021
1 parent cad02e5 commit 1032b71
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/client/lib/service/txpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -51,6 +54,7 @@ export class TxPool extends EventEmitter {
super()

this.config = options.config
this._logInterval = null

this.pool = new Map<string, TxPoolObject[]>()
this.opened = false
Expand All @@ -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
}

Expand Down Expand Up @@ -125,19 +129,21 @@ 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) => {
count += poolObjects.length
})
this.config.logger.info(`TxPool Statistics transactions=${count} senders=${this.pool.size}`)
}

/**
* Close pool
*/
async close() {
//this.pool.clear()
this.opened = false
}
}
27 changes: 27 additions & 0 deletions packages/client/test/service/txpool.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})

0 comments on commit 1032b71

Please sign in to comment.