From fc3be810a25ce98402c686bd41606e08e7b5c2ac Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 2 Jun 2017 12:31:16 +0200 Subject: Implement zeroEx.exchange.subscribeAsync made sure to clean up subscriptions if user updates the provider --- test/0x.js_test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index efc703ea1..52580171b 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -8,7 +8,7 @@ import * as Sinon from 'sinon'; import {ZeroEx} from '../src/0x.js'; import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; -import {Order} from '../src/types'; +import {Order, DoneCallback} from '../src/types'; chai.use(ChaiBigNumber()); chai.use(dirtyChai); @@ -16,7 +16,7 @@ const expect = chai.expect; describe('ZeroEx library', () => { describe('#setProvider', () => { - it('overrides the provider in the nested web3 instance and invalidates contractInstances', async () => { + it('overrides provider in nested web3s and invalidates contractInstances', async () => { const web3 = web3Factory.create(); const zeroEx = new ZeroEx(web3); // Instantiate the contract instances with the current provider @@ -28,7 +28,7 @@ describe('ZeroEx library', () => { const newProvider = web3Factory.getRpcProvider(); // Add property to newProvider so that we can differentiate it from old provider (newProvider as any).zeroExTestId = 1; - zeroEx.setProvider(newProvider); + await zeroEx.setProviderAsync(newProvider); // Check that contractInstances with old provider are removed after provider update expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.undefined(); -- cgit v1.2.3 From efcfa1af702ca78d4d15a686136635eb775ce570 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 2 Jun 2017 12:32:38 +0200 Subject: Add tests for subscribeAsync, making sure events are caught and that subscriptions are removed on provider change --- test/exchange_wrapper_test.ts | 84 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 8fa03ea81..77e241e79 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -10,7 +10,15 @@ import {web3Factory} from './utils/web3_factory'; import {ZeroEx} from '../src/0x.js'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {orderFactory} from './utils/order_factory'; -import {FillOrderValidationErrs, Token, SignedOrder} from '../src/types'; +import { + FillOrderValidationErrs, + Token, + SignedOrder, + SubscriptionOpts, + ExchangeEvents, + ContractEvent, + DoneCallback, +} from '../src/types'; import {FillScenarios} from './utils/fill_scenarios'; chai.use(dirtyChai); @@ -294,4 +302,78 @@ describe('ExchangeWrapper', () => { }); }); }); + describe('#subscribeAsync', () => { + const indexFilterValues = {}; + const shouldCheckTransfer = false; + let makerTokenAddress: string; + let takerTokenAddress: string; + let coinBase: string; + let takerAddress: string; + let makerAddress: string; + let fillableAmount: BigNumber.BigNumber; + let signedOrder: SignedOrder; + before(() => { + [coinBase, makerAddress, takerAddress] = userAddresses; + const [makerToken, takerToken] = tokens; + makerTokenAddress = makerToken.address; + takerTokenAddress = takerToken.address; + }); + beforeEach(async () => { + fillableAmount = new BigNumber(5); + signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, + ); + }); + afterEach(async () => { + (zeroEx.exchange as any).stopWatchingExchangeLogEventsAsync(); + }); + // Hack: Mocha does not allow a test to be both async and have a `done` callback + // Since we need to await the receipt of the event in the `subscribeAsync` callback, + // we do need both. A hack is to make the top-level a sync fn w/ a done callback and then + // wrap the rest of the test in an async block + // Source: https://github.com/mochajs/mocha/issues/2407 + it ('Should receive the LogFill event when an order is filled', (done: DoneCallback) => { + (async () => { + const subscriptionOpts: SubscriptionOpts = { + fromBlock: 0, + toBlock: 'latest', + }; + await zeroEx.exchange.subscribeAsync(ExchangeEvents.LogFill, subscriptionOpts, + indexFilterValues, (err: Error, event: ContractEvent) => { + expect(err).to.be.null(); + expect(event).to.not.be.undefined(); + done(); + }); + const fillTakerAmountInBaseUnits = new BigNumber(1); + zeroEx.setTransactionSenderAccount(takerAddress); + await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); + })(); + }); + it('Outstanding subscriptions are cancelled when zeroEx.setProviderAsync called', (done: DoneCallback) => { + (async () => { + const subscriptionOpts: SubscriptionOpts = { + fromBlock: 0, + toBlock: 'latest', + }; + await zeroEx.exchange.subscribeAsync(ExchangeEvents.LogFill, subscriptionOpts, + indexFilterValues, (err: Error, event: ContractEvent) => { + done(new Error('Expected this subscription to have been cancelled')); + }); + + const newProvider = web3Factory.getRpcProvider(); + await zeroEx.setProviderAsync(newProvider); + + await zeroEx.exchange.subscribeAsync(ExchangeEvents.LogFill, subscriptionOpts, + indexFilterValues, (err: Error, event: ContractEvent) => { + expect(err).to.be.null(); + expect(event).to.not.be.undefined(); + done(); + }); + + const fillTakerAmountInBaseUnits = new BigNumber(1); + zeroEx.setTransactionSenderAccount(takerAddress); + await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); + })(); + }); + }); }); -- cgit v1.2.3 From bef967f405519eb922f5918852250ad4af544705 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 2 Jun 2017 20:22:25 +0200 Subject: remove space --- test/exchange_wrapper_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index a44d2728a..4f3a48b26 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -434,7 +434,7 @@ describe('ExchangeWrapper', () => { // we do need both. A hack is to make the top-level a sync fn w/ a done callback and then // wrap the rest of the test in an async block // Source: https://github.com/mochajs/mocha/issues/2407 - it ('Should receive the LogFill event when an order is filled', (done: DoneCallback) => { + it('Should receive the LogFill event when an order is filled', (done: DoneCallback) => { (async () => { const subscriptionOpts: SubscriptionOpts = { fromBlock: 0, -- cgit v1.2.3