Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Downgrade Sinon version to match monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
dekz committed Aug 14, 2019
1 parent 7eb839e commit 4117e4f
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 157 deletions.
9 changes: 4 additions & 5 deletions packages/orderbook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@
"@0x/tslint-config": "^3.0.1",
"@0x/types": "^2.4.1",
"@types/jest": "^24.0.17",
"@types/sinon": "^7.0.13",
"@types/websocket": "^0.0.40",
"@types/sinon": "^2.2.2",
"@types/websocket": "^0.0.39",
"jest": "^24.8.0",
"prettier": "^1.18.2",
"sinon": "^7.4.1",
"sinon": "^4.0.0",
"ts-jest": "^24.0.2",
"shx": "^0.2.2",
"typescript": "^3.5.3"
"typescript": "^3.0.1"
},
"dependencies": {
"@0x/assert": "^2.1.3",
Expand Down
53 changes: 36 additions & 17 deletions packages/orderbook/test/order_provider/mesh_order_provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { createOrder } from '../utils';
describe('MeshOrderProvider', () => {
let orderStore: OrderStore;
let provider: BaseOrderProvider;
const stubs: sinon.SinonStub[] = [];

const websocketEndpoint = `ws://localhost:${SERVER_PORT}`;
const makerAssetData = '0xf47261b000000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359';
const takerAssetData = '0xf47261b0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
Expand Down Expand Up @@ -61,14 +63,16 @@ describe('MeshOrderProvider', () => {
let connection: any;
afterEach(() => {
void provider.destroyAsync();
sinon.restore();
stubs.forEach(s => s.restore());
stopServer();
});
beforeEach(async () => {
orderStore = new OrderStore();
sinon
.stub(WSClient.prototype as any, '_startInternalLivenessCheckAsync')
.callsFake(async () => Promise.resolve());
stubs.push(
sinon
.stub(WSClient.prototype as any, '_startInternalLivenessCheckAsync')
.callsFake(async () => Promise.resolve()),
);
});
describe('#createSubscriptionForAssetPairAsync', () => {
beforeEach(async () => {
Expand All @@ -93,19 +97,24 @@ describe('MeshOrderProvider', () => {
const getOrdersStub = sinon
.stub(WSClient.prototype, 'getOrdersAsync')
.callsFake(async () => Promise.resolve([]));
stubs.push(getOrdersStub);
const subscriptionStub = sinon
.stub(WSClient.prototype, 'subscribeToOrdersAsync')
.callsFake(async () => Promise.resolve('suscriptionId'));
stubs.push(subscriptionStub);
provider = new MeshOrderProvider({ websocketEndpoint }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
expect(getOrdersStub.callCount).toBe(1);
expect(subscriptionStub.callCount).toBe(1);
});
test('fetches once when the same subscription is called', async () => {
const stub = sinon.stub(WSClient.prototype, 'getOrdersAsync').callsFake(async () => Promise.resolve([]));
sinon
.stub(WSClient.prototype, 'subscribeToOrdersAsync')
.callsFake(async () => Promise.resolve(subscriptionId));
stubs.push(stub);
stubs.push(
sinon
.stub(WSClient.prototype, 'subscribeToOrdersAsync')
.callsFake(async () => Promise.resolve(subscriptionId)),
);
provider = new MeshOrderProvider({ websocketEndpoint }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
Expand All @@ -119,18 +128,22 @@ describe('MeshOrderProvider', () => {
signedOrder: order.order,
fillableTakerAssetAmount: new BigNumber(1),
};
sinon.stub(WSClient.prototype, 'getOrdersAsync').callsFake(async () => Promise.resolve([orderInfo]));
sinon
.stub(WSClient.prototype, 'subscribeToOrdersAsync')
.callsFake(async () => Promise.resolve(subscriptionId));
stubs.push(
sinon.stub(WSClient.prototype, 'getOrdersAsync').callsFake(async () => Promise.resolve([orderInfo])),
);
stubs.push(
sinon
.stub(WSClient.prototype, 'subscribeToOrdersAsync')
.callsFake(async () => Promise.resolve(subscriptionId)),
);
provider = new MeshOrderProvider({ websocketEndpoint }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
const orders = orderStore.getOrderSetForAssets(makerAssetData, takerAssetData);
expect(orders.size()).toBe(1);
});
test('stores the orders from a subscription update', async () => {
const eventResponse = JSON.stringify(addedResponse);
sinon.stub(WSClient.prototype, 'getOrdersAsync').callsFake(async () => Promise.resolve([]));
stubs.push(sinon.stub(WSClient.prototype, 'getOrdersAsync').callsFake(async () => Promise.resolve([])));
provider = new MeshOrderProvider({ websocketEndpoint }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
connection.sendUTF(eventResponse);
Expand All @@ -141,7 +154,7 @@ describe('MeshOrderProvider', () => {
test('stores removed orders on a subscription update', async () => {
const added = JSON.stringify(addedResponse);
const removed = JSON.stringify(removedResponse);
sinon.stub(WSClient.prototype, 'getOrdersAsync').callsFake(async () => Promise.resolve([]));
stubs.push(sinon.stub(WSClient.prototype, 'getOrdersAsync').callsFake(async () => Promise.resolve([])));
provider = new MeshOrderProvider({ websocketEndpoint }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
connection.sendUTF(added);
Expand Down Expand Up @@ -192,6 +205,8 @@ describe('MeshOrderProvider', () => {
const addOrdersStub = sinon
.stub(WSClient.prototype, 'addOrdersAsync')
.callsFake(async () => Promise.resolve({ accepted: [], rejected: [] }));
stubs.push(getOrdersStub);
stubs.push(addOrdersStub);
provider = new MeshOrderProvider(
{
websocketEndpoint,
Expand Down Expand Up @@ -227,10 +242,14 @@ describe('MeshOrderProvider', () => {
signedOrder: order.order,
fillableTakerAssetAmount: new BigNumber(1),
};
sinon.stub(WSClient.prototype, 'getOrdersAsync').callsFake(async () => Promise.resolve([orderInfo]));
sinon
.stub(WSClient.prototype, 'subscribeToOrdersAsync')
.callsFake(async () => Promise.resolve(subscriptionId));
stubs.push(
sinon.stub(WSClient.prototype, 'getOrdersAsync').callsFake(async () => Promise.resolve([orderInfo])),
);
stubs.push(
sinon
.stub(WSClient.prototype, 'subscribeToOrdersAsync')
.callsFake(async () => Promise.resolve(subscriptionId)),
);
provider = new MeshOrderProvider({ websocketEndpoint }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
const assetPairs = await provider.getAvailableAssetDatasAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ describe('SRAPollingOrderProvider', () => {
const httpEndpoint = 'https://localhost';
const makerAssetData = '0xf47261b000000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359';
const takerAssetData = '0xf47261b0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
const stubs: sinon.SinonStub[] = [];
afterEach(() => {
void provider.destroyAsync();
sinon.restore();
stubs.forEach(s => s.restore());
});
beforeEach(() => {
orderStore = new OrderStore();
Expand All @@ -25,6 +26,7 @@ describe('SRAPollingOrderProvider', () => {
const stub = sinon
.stub(HttpClient.prototype, 'getOrdersAsync')
.callsFake(async () => Promise.resolve({ records: [], total: 0, perPage: 0, page: 1 }));
stubs.push(stub);
provider = new SRAPollingOrderProvider({ httpEndpoint, pollingIntervalMs: 5 }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
expect(stub.callCount).toBe(2);
Expand All @@ -33,6 +35,7 @@ describe('SRAPollingOrderProvider', () => {
const stub = sinon
.stub(HttpClient.prototype, 'getOrdersAsync')
.callsFake(async () => Promise.resolve({ records: [], total: 0, perPage: 0, page: 1 }));
stubs.push(stub);
provider = new SRAPollingOrderProvider({ httpEndpoint, pollingIntervalMs: 5 }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
Expand All @@ -48,20 +51,23 @@ describe('SRAPollingOrderProvider', () => {
page: 1,
}),
);
stubs.push(stub);
provider = new SRAPollingOrderProvider({ httpEndpoint, pollingIntervalMs: 1 }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
await utils.delayAsync(5);
expect(stub.callCount).toBeGreaterThan(2);
});
test('stores the orders returned from the API response', async () => {
const records = [createOrder(makerAssetData, takerAssetData)];
sinon.stub(HttpClient.prototype, 'getOrdersAsync').callsFake(async () =>
Promise.resolve({
records,
total: 1,
perPage: 1,
page: 1,
}),
stubs.push(
sinon.stub(HttpClient.prototype, 'getOrdersAsync').callsFake(async () =>
Promise.resolve({
records,
total: 1,
perPage: 1,
page: 1,
}),
),
);
provider = new SRAPollingOrderProvider({ httpEndpoint, pollingIntervalMs: 30000 }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
Expand All @@ -70,13 +76,15 @@ describe('SRAPollingOrderProvider', () => {
});
test('removes the order from the set when the API response no longer returns the order', async () => {
const records = [createOrder(makerAssetData, takerAssetData)];
sinon.stub(HttpClient.prototype, 'getOrdersAsync').callsFake(async () =>
Promise.resolve({
records,
total: 1,
perPage: 1,
page: 1,
}),
stubs.push(
sinon.stub(HttpClient.prototype, 'getOrdersAsync').callsFake(async () =>
Promise.resolve({
records,
total: 1,
perPage: 1,
page: 1,
}),
),
);
provider = new SRAPollingOrderProvider({ httpEndpoint, pollingIntervalMs: 5 }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ describe('SRAWebsocketOrderProvider', () => {
const websocketEndpoint = 'wss://localhost';
const makerAssetData = '0xf47261b000000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359';
const takerAssetData = '0xf47261b0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
const stubs: sinon.SinonStub[] = [];
afterEach(() => {
void provider.destroyAsync();
sinon.restore();
stubs.forEach(s => s.restore());
});
beforeEach(() => {
orderStore = new OrderStore();
Expand All @@ -28,9 +29,12 @@ describe('SRAWebsocketOrderProvider', () => {
const httpStub = sinon
.stub(HttpClient.prototype, 'getOrdersAsync')
.callsFake(async () => Promise.resolve({ records: [], total: 0, perPage: 0, page: 1 }));
sinon
.stub(ordersChannelFactory, 'createWebSocketOrdersChannelAsync')
.callsFake(async () => Promise.resolve({ subscribe: NOOP, close: NOOP }));
stubs.push(
sinon
.stub(ordersChannelFactory, 'createWebSocketOrdersChannelAsync')
.callsFake(async () => Promise.resolve({ subscribe: NOOP, close: NOOP })),
);
stubs.push(httpStub);
provider = new SRAWebsocketOrderProvider({ httpEndpoint, websocketEndpoint }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
expect(httpStub.callCount).toBe(2);
Expand All @@ -39,9 +43,12 @@ describe('SRAWebsocketOrderProvider', () => {
const stub = sinon
.stub(HttpClient.prototype, 'getOrdersAsync')
.callsFake(async () => Promise.resolve({ records: [], total: 0, perPage: 0, page: 1 }));
sinon
.stub(ordersChannelFactory, 'createWebSocketOrdersChannelAsync')
.callsFake(async () => Promise.resolve({ subscribe: NOOP, close: NOOP }));
stubs.push(
sinon
.stub(ordersChannelFactory, 'createWebSocketOrdersChannelAsync')
.callsFake(async () => Promise.resolve({ subscribe: NOOP, close: NOOP })),
);
stubs.push(stub);
provider = new SRAWebsocketOrderProvider({ httpEndpoint, websocketEndpoint }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
Expand All @@ -57,13 +64,15 @@ describe('SRAWebsocketOrderProvider', () => {
page: 1,
}),
);
stubs.push(stub);
let handler: OrdersChannelHandler | undefined;
const wsStub = sinon
.stub(ordersChannelFactory, 'createWebSocketOrdersChannelAsync')
.callsFake(async (_url, updateHandler) => {
handler = updateHandler;
return Promise.resolve({ subscribe: NOOP, close: NOOP });
});
stubs.push(wsStub);
provider = new SRAWebsocketOrderProvider({ httpEndpoint, websocketEndpoint }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
expect(handler).not.toBe(undefined);
Expand All @@ -79,30 +88,36 @@ describe('SRAWebsocketOrderProvider', () => {
expect(storedOrders.size()).toBe(1);
});
test('stores the orders', async () => {
sinon.stub(HttpClient.prototype, 'getOrdersAsync').callsFake(async () =>
Promise.resolve({
records: [createOrder(makerAssetData, takerAssetData)],
total: 1,
perPage: 1,
page: 1,
}),
stubs.push(
sinon.stub(HttpClient.prototype, 'getOrdersAsync').callsFake(async () =>
Promise.resolve({
records: [createOrder(makerAssetData, takerAssetData)],
total: 1,
perPage: 1,
page: 1,
}),
),
);
stubs.push(
sinon
.stub(ordersChannelFactory, 'createWebSocketOrdersChannelAsync')
.callsFake(async () => Promise.resolve({ subscribe: NOOP, close: NOOP })),
);
sinon
.stub(ordersChannelFactory, 'createWebSocketOrdersChannelAsync')
.callsFake(async () => Promise.resolve({ subscribe: NOOP, close: NOOP }));
provider = new SRAWebsocketOrderProvider({ httpEndpoint, websocketEndpoint }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
const orders = orderStore.getOrderSetForAssets(makerAssetData, takerAssetData);
expect(orders.size()).toBe(1);
});
test('reconnects on channel close', async () => {
sinon.stub(HttpClient.prototype, 'getOrdersAsync').callsFake(async () =>
Promise.resolve({
records: [],
total: 0,
perPage: 1,
page: 1,
}),
stubs.push(
sinon.stub(HttpClient.prototype, 'getOrdersAsync').callsFake(async () =>
Promise.resolve({
records: [],
total: 0,
perPage: 1,
page: 1,
}),
),
);
let handler: OrdersChannelHandler | undefined;
const wsStub = sinon
Expand All @@ -111,6 +126,7 @@ describe('SRAWebsocketOrderProvider', () => {
handler = updateHandler;
return Promise.resolve({ subscribe: NOOP, close: NOOP });
});
stubs.push(wsStub);
provider = new SRAWebsocketOrderProvider({ httpEndpoint, websocketEndpoint }, orderStore);
await provider.createSubscriptionForAssetPairAsync(makerAssetData, takerAssetData);
expect(handler).not.toBe(undefined);
Expand Down
Loading

0 comments on commit 4117e4f

Please sign in to comment.