From 4ae9482d506ccdf028fcd39bbaf652bbcbef52e8 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Sat, 11 Nov 2017 11:01:59 -0500 Subject: Clean up subscription state. In the case of an exception, keep the state correct between contract wrapper, exchange wrapper and token wrapper. --- test/subscription_test.ts | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 test/subscription_test.ts (limited to 'test/subscription_test.ts') diff --git a/test/subscription_test.ts b/test/subscription_test.ts new file mode 100644 index 000000000..d328084af --- /dev/null +++ b/test/subscription_test.ts @@ -0,0 +1,93 @@ +import 'mocha'; +import * as chai from 'chai'; +import * as Sinon from 'sinon'; +import {chaiSetup} from './utils/chai_setup'; +import * as Web3 from 'web3'; +import BigNumber from 'bignumber.js'; +import promisify = require('es6-promisify'); +import {web3Factory} from './utils/web3_factory'; +import { + ZeroEx, + ZeroExError, + Token, + SubscriptionOpts, + TokenEvents, + ContractEvent, + TransferContractEventArgs, + ApprovalContractEventArgs, + TokenContractEventArgs, + LogWithDecodedArgs, + LogEvent, +} from '../src'; +import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; +import {TokenUtils} from './utils/token_utils'; +import {DoneCallback, BlockParamLiteral} from '../src/types'; + +chaiSetup.configure(); +const expect = chai.expect; +const blockchainLifecycle = new BlockchainLifecycle(); + +describe('SubscriptionTest', () => { + let web3: Web3; + let zeroEx: ZeroEx; + let userAddresses: string[]; + let tokens: Token[]; + let tokenUtils: TokenUtils; + let coinbase: string; + let addressWithoutFunds: string; + before(async () => { + web3 = web3Factory.create(); + zeroEx = new ZeroEx(web3.currentProvider); + userAddresses = await zeroEx.getAvailableAddressesAsync(); + tokens = await zeroEx.tokenRegistry.getTokensAsync(); + tokenUtils = new TokenUtils(tokens); + coinbase = userAddresses[0]; + addressWithoutFunds = userAddresses[1]; + }); + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); + + describe('#subscribe', () => { + const indexFilterValues = {}; + const shouldThrowOnInsufficientBalanceOrAllowance = true; + let tokenAddress: string; + const transferAmount = new BigNumber(42); + const allowanceAmount = new BigNumber(42); + before(() => { + const token = tokens[0]; + tokenAddress = token.address; + }); + afterEach(() => { + zeroEx.token.unsubscribeAll(); + }); + it('Should receive the Error when an error occurs', (done: DoneCallback) => { + (async () => { + const callback = (err: Error, logEvent: LogEvent) => { + expect(err).to.not.be.undefined(); + expect(logEvent).to.be.undefined(); + done(); + }; + Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync') + .throws("JSON RPC error") + zeroEx.token.subscribe( + tokenAddress, TokenEvents.Approval, indexFilterValues, callback); + await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount); + })().catch(done); + }); + it('Should allow unsubscribeAll to be called multiple times', (done: DoneCallback) => { + (async () => { + const callback = (err: Error, logEvent: LogEvent) => { }; + zeroEx.token.subscribe( + tokenAddress, TokenEvents.Approval, indexFilterValues, callback); + await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount); + zeroEx.token.unsubscribeAll(); + zeroEx.token.unsubscribeAll(); + done(); + })().catch(done); + }); + }) + }) \ No newline at end of file -- cgit From a85b1f016d96b1706dcea07aa2581a47035ba7db Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Sat, 11 Nov 2017 12:01:27 -0500 Subject: Test case was error then unsubscribe --- test/subscription_test.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'test/subscription_test.ts') diff --git a/test/subscription_test.ts b/test/subscription_test.ts index d328084af..9a2198e10 100644 --- a/test/subscription_test.ts +++ b/test/subscription_test.ts @@ -1,4 +1,5 @@ import 'mocha'; +import * as _ from 'lodash'; import * as chai from 'chai'; import * as Sinon from 'sinon'; import {chaiSetup} from './utils/chai_setup'; @@ -57,12 +58,15 @@ describe('SubscriptionTest', () => { let tokenAddress: string; const transferAmount = new BigNumber(42); const allowanceAmount = new BigNumber(42); + let stubs: Sinon.SinonStub[] = []; before(() => { const token = tokens[0]; tokenAddress = token.address; }); afterEach(() => { zeroEx.token.unsubscribeAll(); + _.each(stubs, s => s.restore()); + stubs = []; }); it('Should receive the Error when an error occurs', (done: DoneCallback) => { (async () => { @@ -71,20 +75,25 @@ describe('SubscriptionTest', () => { expect(logEvent).to.be.undefined(); done(); }; - Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync') - .throws("JSON RPC error") + stubs = [ + Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync') + .throws("JSON RPC error") + ] zeroEx.token.subscribe( tokenAddress, TokenEvents.Approval, indexFilterValues, callback); await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount); })().catch(done); }); - it('Should allow unsubscribeAll to be called multiple times', (done: DoneCallback) => { + it('Should allow unsubscribeAll to be called successfully after an error', (done: DoneCallback) => { (async () => { const callback = (err: Error, logEvent: LogEvent) => { }; zeroEx.token.subscribe( tokenAddress, TokenEvents.Approval, indexFilterValues, callback); await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount); - zeroEx.token.unsubscribeAll(); + stubs = [ + Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync') + .throws("JSON RPC error") + ] zeroEx.token.unsubscribeAll(); done(); })().catch(done); -- cgit From 394417ff073c54ba7409ac24004a9757935688cb Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Sat, 11 Nov 2017 12:15:24 -0500 Subject: Removed nits --- test/subscription_test.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'test/subscription_test.ts') diff --git a/test/subscription_test.ts b/test/subscription_test.ts index 9a2198e10..64e9acf0d 100644 --- a/test/subscription_test.ts +++ b/test/subscription_test.ts @@ -11,13 +11,8 @@ import { ZeroEx, ZeroExError, Token, - SubscriptionOpts, - TokenEvents, - ContractEvent, - TransferContractEventArgs, ApprovalContractEventArgs, - TokenContractEventArgs, - LogWithDecodedArgs, + TokenEvents, LogEvent, } from '../src'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; @@ -51,7 +46,6 @@ describe('SubscriptionTest', () => { afterEach(async () => { await blockchainLifecycle.revertAsync(); }); - describe('#subscribe', () => { const indexFilterValues = {}; const shouldThrowOnInsufficientBalanceOrAllowance = true; @@ -72,6 +66,7 @@ describe('SubscriptionTest', () => { (async () => { const callback = (err: Error, logEvent: LogEvent) => { expect(err).to.not.be.undefined(); + expect(err).to.not.be.null(); expect(logEvent).to.be.undefined(); done(); }; @@ -89,7 +84,6 @@ describe('SubscriptionTest', () => { const callback = (err: Error, logEvent: LogEvent) => { }; zeroEx.token.subscribe( tokenAddress, TokenEvents.Approval, indexFilterValues, callback); - await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount); stubs = [ Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync') .throws("JSON RPC error") -- cgit From ee73659f16dd70f15fa883e8d512e0da893b1393 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Sat, 11 Nov 2017 12:33:05 -0500 Subject: Check for null rather than undefined --- test/subscription_test.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'test/subscription_test.ts') diff --git a/test/subscription_test.ts b/test/subscription_test.ts index 64e9acf0d..985fdc1d6 100644 --- a/test/subscription_test.ts +++ b/test/subscription_test.ts @@ -65,7 +65,6 @@ describe('SubscriptionTest', () => { it('Should receive the Error when an error occurs', (done: DoneCallback) => { (async () => { const callback = (err: Error, logEvent: LogEvent) => { - expect(err).to.not.be.undefined(); expect(err).to.not.be.null(); expect(logEvent).to.be.undefined(); done(); -- cgit