From fd2c5d46ad7f052dfa6c04d14451f80e1efce943 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 30 Oct 2017 12:05:31 +0200 Subject: Adjust tests for mempool event watcher --- test/event_watcher_test.ts | 126 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 test/event_watcher_test.ts (limited to 'test/event_watcher_test.ts') diff --git a/test/event_watcher_test.ts b/test/event_watcher_test.ts new file mode 100644 index 000000000..208871ea8 --- /dev/null +++ b/test/event_watcher_test.ts @@ -0,0 +1,126 @@ +import 'mocha'; +import * as chai from 'chai'; +import * as _ from 'lodash'; +import * as Sinon from 'sinon'; +import * as Web3 from 'web3'; +import BigNumber from 'bignumber.js'; +import {chaiSetup} from './utils/chai_setup'; +import {web3Factory} from './utils/web3_factory'; +import {Web3Wrapper} from '../src/web3_wrapper'; +import {EventWatcher} from '../src/mempool/event_watcher'; +import { + ZeroEx, + LogEvent, + DecodedLogEvent, +} from '../src'; +import {DoneCallback} from '../src/types'; + +chaiSetup.configure(); +const expect = chai.expect; + +describe('EventWatcher', () => { + let web3: Web3; + let stubs: Sinon.SinonStub[] = []; + let eventWatcher: EventWatcher; + let web3Wrapper: Web3Wrapper; + const logA = { + address: '0x71d271f8b14adef568f8f28f1587ce7271ac4ca5', + blockHash: null, + blockNumber: null, + data: '', + logIndex: null, + topics: [], + transactionHash: '0x004881d38cd4a8f72f1a0d68c8b9b8124504706041ff37019c1d1ed6bfda8e17', + transactionIndex: null, + }; + const logB = { + address: '0x8d12a197cb00d4747a1fe03395095ce2a5cc6819', + blockHash: null, + blockNumber: null, + data: '', + logIndex: null, + topics: [ '0xf341246adaac6f497bc2a656f546ab9e182111d630394f0c57c710a59a2cb567' ], + transactionHash: '0x01ef3c048b18d9b09ea195b4ed94cf8dd5f3d857a1905ff886b152cfb1166f25', + transactionIndex: null, + }; + const logC = { + address: '0x1d271f8b174adef58f1587ce68f8f27271ac4ca5', + blockHash: null, + blockNumber: null, + data: '', + logIndex: null, + topics: [ '0xf341246adaac6f497bc2a656f546ab9e182111d630394f0c57c710a59a2cb567' ], + transactionHash: '0x01ef3c048b18d9b09ea195b4ed94cf8dd5f3d857a1905ff886b152cfb1166f25', + transactionIndex: null, + }; + before(async () => { + web3 = web3Factory.create(); + const mempoolPollingIntervalMs = 10; + web3Wrapper = new Web3Wrapper(web3.currentProvider); + eventWatcher = new EventWatcher(web3Wrapper, mempoolPollingIntervalMs); + }); + afterEach(() => { + // clean up any stubs after the test has completed + _.each(stubs, s => s.restore()); + stubs = []; + eventWatcher.unsubscribe(); + }); + it('correctly emits initial log events', (done: DoneCallback) => { + const logs: Web3.LogEntry[] = [logA, logB]; + const expectedLogEvents = [ + { + removed: false, + ...logA, + }, + { + removed: false, + ...logB, + }, + ]; + const getLogsStub = Sinon.stub(web3Wrapper, 'getLogsAsync'); + getLogsStub.onCall(0).returns(logs); + stubs.push(getLogsStub); + const callback = (event: LogEvent) => { + const expectedLogEvent = expectedLogEvents.shift(); + expect(event).to.be.deep.equal(expectedLogEvent); + if (_.isEmpty(expectedLogEvents)) { + done(); + } + }; + eventWatcher.subscribe(callback); + }); + it('correctly computes the difference and emits only changes', (done: DoneCallback) => { + const initialLogs: Web3.LogEntry[] = [logA, logB]; + const changedLogs: Web3.LogEntry[] = [logA, logC]; + const expectedLogEvents = [ + { + removed: false, + ...logA, + }, + { + removed: false, + ...logB, + }, + { + removed: true, + ...logB, + }, + { + removed: false, + ...logC, + }, + ]; + const getLogsStub = Sinon.stub(web3Wrapper, 'getLogsAsync'); + getLogsStub.onCall(0).returns(initialLogs); + getLogsStub.onCall(1).returns(changedLogs); + stubs.push(getLogsStub); + const callback = (event: LogEvent) => { + const expectedLogEvent = expectedLogEvents.shift(); + expect(event).to.be.deep.equal(expectedLogEvent); + if (_.isEmpty(expectedLogEvents)) { + done(); + } + }; + eventWatcher.subscribe(callback); + }); +}); -- cgit v1.2.3 From c9e0b298781ca1522f69cae66ac966a0e4800469 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 9 Nov 2017 15:44:03 -0500 Subject: Add SubscriptionAlreadyPresent error --- test/event_watcher_test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'test/event_watcher_test.ts') diff --git a/test/event_watcher_test.ts b/test/event_watcher_test.ts index 208871ea8..a246805a0 100644 --- a/test/event_watcher_test.ts +++ b/test/event_watcher_test.ts @@ -23,6 +23,7 @@ describe('EventWatcher', () => { let stubs: Sinon.SinonStub[] = []; let eventWatcher: EventWatcher; let web3Wrapper: Web3Wrapper; + const numConfirmations = 0; const logA = { address: '0x71d271f8b14adef568f8f28f1587ce7271ac4ca5', blockHash: null, @@ -87,7 +88,7 @@ describe('EventWatcher', () => { done(); } }; - eventWatcher.subscribe(callback); + eventWatcher.subscribe(callback, numConfirmations); }); it('correctly computes the difference and emits only changes', (done: DoneCallback) => { const initialLogs: Web3.LogEntry[] = [logA, logB]; @@ -121,6 +122,6 @@ describe('EventWatcher', () => { done(); } }; - eventWatcher.subscribe(callback); + eventWatcher.subscribe(callback, numConfirmations); }); }); -- cgit v1.2.3 From 126a165f558326625d892c4a379c0ebd66088c9a Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 9 Nov 2017 16:59:41 -0500 Subject: Add nested config for orderWatcher --- test/event_watcher_test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/event_watcher_test.ts') diff --git a/test/event_watcher_test.ts b/test/event_watcher_test.ts index a246805a0..8f3898287 100644 --- a/test/event_watcher_test.ts +++ b/test/event_watcher_test.ts @@ -7,7 +7,7 @@ import BigNumber from 'bignumber.js'; import {chaiSetup} from './utils/chai_setup'; import {web3Factory} from './utils/web3_factory'; import {Web3Wrapper} from '../src/web3_wrapper'; -import {EventWatcher} from '../src/mempool/event_watcher'; +import {EventWatcher} from '../src/order_watcher/event_watcher'; import { ZeroEx, LogEvent, @@ -56,9 +56,9 @@ describe('EventWatcher', () => { }; before(async () => { web3 = web3Factory.create(); - const mempoolPollingIntervalMs = 10; + const pollingIntervalMs = 10; web3Wrapper = new Web3Wrapper(web3.currentProvider); - eventWatcher = new EventWatcher(web3Wrapper, mempoolPollingIntervalMs); + eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs); }); afterEach(() => { // clean up any stubs after the test has completed -- cgit v1.2.3 From 47f9e171fcc1e3e24c38b1bb381b8303dc8341d4 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 9 Nov 2017 23:32:22 -0500 Subject: Move numConfirmations to constructor call --- test/event_watcher_test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/event_watcher_test.ts') diff --git a/test/event_watcher_test.ts b/test/event_watcher_test.ts index 8f3898287..36153c207 100644 --- a/test/event_watcher_test.ts +++ b/test/event_watcher_test.ts @@ -58,7 +58,7 @@ describe('EventWatcher', () => { web3 = web3Factory.create(); const pollingIntervalMs = 10; web3Wrapper = new Web3Wrapper(web3.currentProvider); - eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs); + eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs, numConfirmations); }); afterEach(() => { // clean up any stubs after the test has completed @@ -88,7 +88,7 @@ describe('EventWatcher', () => { done(); } }; - eventWatcher.subscribe(callback, numConfirmations); + eventWatcher.subscribe(callback); }); it('correctly computes the difference and emits only changes', (done: DoneCallback) => { const initialLogs: Web3.LogEntry[] = [logA, logB]; @@ -122,6 +122,6 @@ describe('EventWatcher', () => { done(); } }; - eventWatcher.subscribe(callback, numConfirmations); + eventWatcher.subscribe(callback); }); }); -- cgit v1.2.3 From 041d00301c050a85ac7b41c7f367b319f09ed698 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Sat, 11 Nov 2017 08:58:01 -0500 Subject: Fix type declaration in test --- test/event_watcher_test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'test/event_watcher_test.ts') diff --git a/test/event_watcher_test.ts b/test/event_watcher_test.ts index 36153c207..98dab93b5 100644 --- a/test/event_watcher_test.ts +++ b/test/event_watcher_test.ts @@ -24,7 +24,7 @@ describe('EventWatcher', () => { let eventWatcher: EventWatcher; let web3Wrapper: Web3Wrapper; const numConfirmations = 0; - const logA = { + const logA: Web3.LogEntry = { address: '0x71d271f8b14adef568f8f28f1587ce7271ac4ca5', blockHash: null, blockNumber: null, @@ -32,9 +32,9 @@ describe('EventWatcher', () => { logIndex: null, topics: [], transactionHash: '0x004881d38cd4a8f72f1a0d68c8b9b8124504706041ff37019c1d1ed6bfda8e17', - transactionIndex: null, + transactionIndex: 0, }; - const logB = { + const logB: Web3.LogEntry = { address: '0x8d12a197cb00d4747a1fe03395095ce2a5cc6819', blockHash: null, blockNumber: null, @@ -42,9 +42,9 @@ describe('EventWatcher', () => { logIndex: null, topics: [ '0xf341246adaac6f497bc2a656f546ab9e182111d630394f0c57c710a59a2cb567' ], transactionHash: '0x01ef3c048b18d9b09ea195b4ed94cf8dd5f3d857a1905ff886b152cfb1166f25', - transactionIndex: null, + transactionIndex: 0, }; - const logC = { + const logC: Web3.LogEntry = { address: '0x1d271f8b174adef58f1587ce68f8f27271ac4ca5', blockHash: null, blockNumber: null, @@ -52,7 +52,7 @@ describe('EventWatcher', () => { logIndex: null, topics: [ '0xf341246adaac6f497bc2a656f546ab9e182111d630394f0c57c710a59a2cb567' ], transactionHash: '0x01ef3c048b18d9b09ea195b4ed94cf8dd5f3d857a1905ff886b152cfb1166f25', - transactionIndex: null, + transactionIndex: 0, }; before(async () => { web3 = web3Factory.create(); -- cgit v1.2.3 From f5608d2c94bcee05a76ef102f235f5e860820567 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Sun, 12 Nov 2017 12:53:03 -0500 Subject: Pass blockStore to eventWatcher --- test/event_watcher_test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'test/event_watcher_test.ts') diff --git a/test/event_watcher_test.ts b/test/event_watcher_test.ts index 98dab93b5..0a1e7eb63 100644 --- a/test/event_watcher_test.ts +++ b/test/event_watcher_test.ts @@ -8,6 +8,7 @@ import {chaiSetup} from './utils/chai_setup'; import {web3Factory} from './utils/web3_factory'; import {Web3Wrapper} from '../src/web3_wrapper'; import {EventWatcher} from '../src/order_watcher/event_watcher'; +import {BlockStore} from '../src/stores/block_store'; import { ZeroEx, LogEvent, @@ -23,6 +24,7 @@ describe('EventWatcher', () => { let stubs: Sinon.SinonStub[] = []; let eventWatcher: EventWatcher; let web3Wrapper: Web3Wrapper; + let blockStore: BlockStore; const numConfirmations = 0; const logA: Web3.LogEntry = { address: '0x71d271f8b14adef568f8f28f1587ce7271ac4ca5', @@ -58,12 +60,15 @@ describe('EventWatcher', () => { web3 = web3Factory.create(); const pollingIntervalMs = 10; web3Wrapper = new Web3Wrapper(web3.currentProvider); - eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs, numConfirmations); + blockStore = new BlockStore(); + await blockStore.startAsync(); + eventWatcher = new EventWatcher(web3Wrapper, blockStore, pollingIntervalMs, numConfirmations); }); afterEach(() => { // clean up any stubs after the test has completed _.each(stubs, s => s.restore()); stubs = []; + blockStore.stop(); eventWatcher.unsubscribe(); }); it('correctly emits initial log events', (done: DoneCallback) => { -- cgit v1.2.3 From a9ae555b88cc36ff2cbd92fdd37a339702860c01 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Sun, 12 Nov 2017 13:06:25 -0500 Subject: Store number of confirmations in a blockStore --- test/event_watcher_test.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'test/event_watcher_test.ts') diff --git a/test/event_watcher_test.ts b/test/event_watcher_test.ts index 0a1e7eb63..2673f978b 100644 --- a/test/event_watcher_test.ts +++ b/test/event_watcher_test.ts @@ -60,15 +60,13 @@ describe('EventWatcher', () => { web3 = web3Factory.create(); const pollingIntervalMs = 10; web3Wrapper = new Web3Wrapper(web3.currentProvider); - blockStore = new BlockStore(); - await blockStore.startAsync(); - eventWatcher = new EventWatcher(web3Wrapper, blockStore, pollingIntervalMs, numConfirmations); + blockStore = new BlockStore(numConfirmations); + eventWatcher = new EventWatcher(web3Wrapper, blockStore, pollingIntervalMs); }); afterEach(() => { // clean up any stubs after the test has completed _.each(stubs, s => s.restore()); stubs = []; - blockStore.stop(); eventWatcher.unsubscribe(); }); it('correctly emits initial log events', (done: DoneCallback) => { -- cgit v1.2.3 From 84c965d459b948eb03cb8a70a66663bd7b35f463 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Sun, 12 Nov 2017 18:10:47 -0500 Subject: Remove blockStore and default to numConfirmations === 0 --- test/event_watcher_test.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'test/event_watcher_test.ts') diff --git a/test/event_watcher_test.ts b/test/event_watcher_test.ts index 2673f978b..b4164fe63 100644 --- a/test/event_watcher_test.ts +++ b/test/event_watcher_test.ts @@ -8,7 +8,6 @@ import {chaiSetup} from './utils/chai_setup'; import {web3Factory} from './utils/web3_factory'; import {Web3Wrapper} from '../src/web3_wrapper'; import {EventWatcher} from '../src/order_watcher/event_watcher'; -import {BlockStore} from '../src/stores/block_store'; import { ZeroEx, LogEvent, @@ -24,7 +23,6 @@ describe('EventWatcher', () => { let stubs: Sinon.SinonStub[] = []; let eventWatcher: EventWatcher; let web3Wrapper: Web3Wrapper; - let blockStore: BlockStore; const numConfirmations = 0; const logA: Web3.LogEntry = { address: '0x71d271f8b14adef568f8f28f1587ce7271ac4ca5', @@ -60,8 +58,7 @@ describe('EventWatcher', () => { web3 = web3Factory.create(); const pollingIntervalMs = 10; web3Wrapper = new Web3Wrapper(web3.currentProvider); - blockStore = new BlockStore(numConfirmations); - eventWatcher = new EventWatcher(web3Wrapper, blockStore, pollingIntervalMs); + eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs); }); afterEach(() => { // clean up any stubs after the test has completed -- cgit v1.2.3