2019-06-20 00:20:09 +02:00
|
|
|
import { assert } from 'chai'
|
|
|
|
import { EventHandler } from '../../src/keeper/EventHandler'
|
|
|
|
import { ContractEventSubscription } from '../../src/keeper/ContractEvent'
|
|
|
|
import { Ocean } from '../../src/ocean/Ocean'
|
|
|
|
import config from '../config'
|
|
|
|
import TestContractHandler from './TestContractHandler'
|
|
|
|
|
|
|
|
describe('ContractEvent', () => {
|
2019-03-21 02:56:58 +01:00
|
|
|
let ocean: Ocean
|
|
|
|
let account: string
|
|
|
|
let eventHandler: EventHandler
|
|
|
|
let executeTransaction: () => Promise<any>
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
await TestContractHandler.prepareContracts()
|
|
|
|
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
|
|
|
account = (await ocean.accounts.list())[0].getId()
|
|
|
|
|
2019-09-09 12:18:54 +02:00
|
|
|
executeTransaction = () => ocean.keeper.dispenser.requestTokens(10, account)
|
2019-03-21 02:56:58 +01:00
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('#subscribe()', () => {
|
2019-10-31 11:23:41 +01:00
|
|
|
it('should be able to listen to events', async () => {
|
2019-11-15 00:00:10 +01:00
|
|
|
const event = eventHandler.getEvent(ocean.keeper.token, 'Transfer', {
|
|
|
|
to: account
|
|
|
|
})
|
2019-03-21 02:56:58 +01:00
|
|
|
let validResolve = false
|
|
|
|
let subscription: ContractEventSubscription
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
const waitUntilEvent = new Promise(resolve => {
|
|
|
|
subscription = event.subscribe(events => {
|
2019-03-21 02:56:58 +01:00
|
|
|
assert.isDefined(events)
|
|
|
|
assert.lengthOf(events, 2)
|
|
|
|
if (validResolve) {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
await Promise.all([executeTransaction(), executeTransaction()])
|
2019-03-21 02:56:58 +01:00
|
|
|
|
2019-06-24 13:06:38 +02:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 2000))
|
2019-03-21 02:56:58 +01:00
|
|
|
validResolve = true
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
await Promise.all([executeTransaction(), executeTransaction()])
|
2019-03-21 02:56:58 +01:00
|
|
|
|
|
|
|
await waitUntilEvent
|
|
|
|
|
|
|
|
subscription.unsubscribe()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
describe('#once()', () => {
|
2019-10-31 11:23:41 +01:00
|
|
|
it('should listen to event only once', async () => {
|
2019-03-21 02:56:58 +01:00
|
|
|
const to = account
|
2019-09-09 12:18:54 +02:00
|
|
|
const event = eventHandler.getEvent(ocean.keeper.token, 'Transfer', { to })
|
2019-03-21 02:56:58 +01:00
|
|
|
let canBeRejected = false
|
|
|
|
|
|
|
|
const waitUntilEvent = new Promise((resolve, reject) => {
|
2019-10-31 11:23:41 +01:00
|
|
|
event.once(() => {
|
2019-03-21 02:56:58 +01:00
|
|
|
if (canBeRejected) {
|
2019-06-24 13:06:38 +02:00
|
|
|
reject(new Error(''))
|
2019-03-21 02:56:58 +01:00
|
|
|
}
|
|
|
|
setTimeout(resolve, 600)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
await executeTransaction()
|
|
|
|
|
2019-06-24 13:06:38 +02:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 2000))
|
2019-03-21 02:56:58 +01:00
|
|
|
canBeRejected = true
|
|
|
|
|
|
|
|
await executeTransaction()
|
|
|
|
|
|
|
|
await waitUntilEvent
|
|
|
|
})
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
it('should get the event like a promise', async () => {
|
2019-03-21 02:56:58 +01:00
|
|
|
const to = account
|
2019-09-09 12:18:54 +02:00
|
|
|
const event = eventHandler.getEvent(ocean.keeper.token, 'Transfer', { to })
|
2019-03-21 02:56:58 +01:00
|
|
|
|
|
|
|
const waitUntilEvent = event.once()
|
|
|
|
|
2019-06-24 13:06:38 +02:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 400))
|
2019-03-21 02:56:58 +01:00
|
|
|
|
|
|
|
await executeTransaction()
|
|
|
|
|
|
|
|
await waitUntilEvent
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|