2019-06-20 00:20:09 +02:00
|
|
|
import { assert, expect, spy, use } from 'chai'
|
2019-11-11 12:27:18 +01:00
|
|
|
import spies from 'chai-spies'
|
2020-01-30 22:08:18 +01:00
|
|
|
import { EventHandler } from '../../../src/keeper/EventHandler'
|
|
|
|
import { Ocean } from '../../../src/ocean/Ocean'
|
2019-06-20 00:20:09 +02:00
|
|
|
import config from '../config'
|
2019-03-21 02:56:58 +01:00
|
|
|
|
|
|
|
use(spies)
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('EventHandler', () => {
|
2019-03-21 02:56:58 +01:00
|
|
|
let ocean: Ocean
|
|
|
|
let eventHandler: EventHandler
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
ocean = await Ocean.getInstance(config)
|
2019-03-21 03:17:36 +01:00
|
|
|
eventHandler = new EventHandler((ocean as any).instanceConfig)
|
2019-03-21 02:56:58 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
spy.restore()
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('#subscribe()', () => {
|
|
|
|
it('should subscribe to an event', async () => {
|
2019-03-21 02:56:58 +01:00
|
|
|
const countBefore = eventHandler.count
|
|
|
|
|
2019-03-21 03:17:36 +01:00
|
|
|
const subscription = eventHandler.subscribe(() => null)
|
2019-03-21 02:56:58 +01:00
|
|
|
assert.isDefined(subscription)
|
|
|
|
|
|
|
|
const countAfter = eventHandler.count
|
2019-09-09 12:18:54 +02:00
|
|
|
assert.equal(countBefore + 1, countAfter, 'The event seems not added.')
|
2019-03-21 02:56:58 +01:00
|
|
|
|
2019-03-21 03:17:36 +01:00
|
|
|
subscription.unsubscribe()
|
2019-03-21 02:56:58 +01:00
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should unsubscribe using the subscription', async () => {
|
2019-03-21 02:56:58 +01:00
|
|
|
const countBefore = eventHandler.count
|
|
|
|
|
2019-03-21 03:17:36 +01:00
|
|
|
const subscription = eventHandler.subscribe(() => null)
|
2019-03-21 02:56:58 +01:00
|
|
|
assert.isDefined(subscription)
|
|
|
|
|
|
|
|
subscription.unsubscribe()
|
|
|
|
|
|
|
|
const countAfter = eventHandler.count
|
2019-06-20 00:20:09 +02:00
|
|
|
assert.equal(countBefore, countAfter, 'The event seems not added.')
|
2019-03-21 02:56:58 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('#unsubscribe()', () => {
|
|
|
|
it('should unsubscribe from an event', async () => {
|
2019-03-21 02:56:58 +01:00
|
|
|
const countBefore = eventHandler.count
|
2019-03-21 03:17:36 +01:00
|
|
|
const callback = () => null
|
2019-03-21 02:56:58 +01:00
|
|
|
|
|
|
|
eventHandler.subscribe(callback)
|
|
|
|
eventHandler.unsubscribe(callback)
|
|
|
|
|
|
|
|
const countAfter = eventHandler.count
|
2019-09-09 12:18:54 +02:00
|
|
|
assert.equal(countBefore, countAfter, 'The event seems not removed.')
|
2019-03-21 02:56:58 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('#checkBlock()', () => {
|
|
|
|
it('should call the callback on each new block', async () => {
|
2019-03-21 02:56:58 +01:00
|
|
|
let blockNumber = 100000000000
|
|
|
|
const callbackSpy = spy()
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
spy.on((ocean as any).web3.eth, 'getBlockNumber', () => blockNumber)
|
2019-03-21 02:56:58 +01:00
|
|
|
|
|
|
|
const subscription = eventHandler.subscribe(callbackSpy)
|
|
|
|
|
2020-05-19 16:15:56 +02:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 300))
|
2019-03-21 02:56:58 +01:00
|
|
|
|
|
|
|
expect(callbackSpy).not.to.has.been.called()
|
|
|
|
blockNumber++
|
|
|
|
|
2020-05-19 16:15:56 +02:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 300))
|
2019-03-21 02:56:58 +01:00
|
|
|
|
|
|
|
expect(callbackSpy).to.has.been.called.with(blockNumber)
|
|
|
|
|
2019-03-21 03:17:36 +01:00
|
|
|
subscription.unsubscribe()
|
2019-03-21 02:56:58 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|