Skip to content

Commit d961254

Browse files
JacobPlasterJacobPlaster
authored andcommitted
ws2: fix ob update unit tests
1 parent cc959f6 commit d961254

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

examples/ws2/ob_checksum.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const debug = require('debug')('bfx:examples:ws2_ob_checksum')
66
const bfx = require('../bfx')
77
const WSv2 = require('../../lib/transports/ws2')
88

9-
const SYMBOL = 'tBTCUSD'
10-
const PRECISION = 'R0'
11-
const LENGTH = '100'
9+
const SYMBOL = 'tXRPBTC'
10+
const PRECISION = 'P0'
11+
const LENGTH = '25'
1212

1313
const ws = bfx.ws(2, {
1414
manageOrderBooks: true // managed OBs are verified against incoming checksums

test/lib/transports/ws2-unit.js

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -739,16 +739,18 @@ describe('WSv2 channel msg handling', () => {
739739
}
740740
}
741741

742-
ws._handleOBMessage([42, [
742+
let obMsg = [42, [
743743
[100, 2, -4],
744744
[200, 4, -8],
745745
[300, 1, 3]
746-
]], ws._channelMapA[42])
747-
ws._handleOBMessage([43, [
746+
]]
747+
ws._handleOBMessage(obMsg, ws._channelMapA[42], JSON.stringify(obMsg))
748+
obMsg = [43, [
748749
[0.0008, 2, 5, 200],
749750
[0.00045, 30, 4, -300],
750751
[0.0004, 15, 3, -600]
751-
]], ws._channelMapB[43])
752+
]]
753+
ws._handleOBMessage(obMsg, ws._channelMapB[43], JSON.stringify(obMsg))
752754

753755
let obA = ws.getOB('tBTCUSD')
754756
let obB = ws.getOB('fUSD')
@@ -767,10 +769,12 @@ describe('WSv2 channel msg handling', () => {
767769
assert.deepStrictEqual(obB.getEntry(0.00045), { rate: 0.00045, count: 4, amount: -300, period: 30 })
768770
assert.deepStrictEqual(obB.getEntry(0.0008), { rate: 0.0008, count: 5, amount: 200, period: 2 })
769771

770-
ws._handleOBMessage([42, [300, 0, 1]], ws._channelMapA[42])
772+
obMsg = [42, [300, 0, 1]]
773+
ws._handleOBMessage(obMsg, ws._channelMapA[42], JSON.stringify(obMsg))
771774
obA = ws.getOB('tBTCUSD')
772775
assert.strictEqual(obA.bids.length, 0)
773-
ws._handleOBMessage([43, [0.0008, 2, 0, 1]], ws._channelMapB[43])
776+
obMsg = [43, [0.0008, 2, 0, 1]]
777+
ws._handleOBMessage(obMsg, ws._channelMapB[43], JSON.stringify(obMsg))
774778
obB = ws.getOB('fUSD')
775779
assert.strictEqual(obB.asks.length, 0)
776780
})
@@ -801,8 +805,9 @@ describe('WSv2 channel msg handling', () => {
801805
if (++errorsSeen === 2) done()
802806
})
803807

804-
wsTransform._handleOBMessage([42, [100, 0, 1]], wsTransform._channelMap[42])
805-
wsNoTransform._handleOBMessage([42, [100, 0, 1]], wsNoTransform._channelMap[42])
808+
let obMsg = [42, [100, 0, 1]]
809+
wsTransform._handleOBMessage(obMsg, wsTransform._channelMap[42], JSON.stringify(obMsg))
810+
wsNoTransform._handleOBMessage(obMsg, wsNoTransform._channelMap[42], JSON.stringify(obMsg))
806811
})
807812

808813
it('_handleOBMessage: forwards managed ob to listeners', (done) => {
@@ -825,7 +830,8 @@ describe('WSv2 channel msg handling', () => {
825830
if (++seen === 2) done()
826831
})
827832

828-
ws._handleOBMessage([42, [[100, 2, 3]]], ws._channelMap[42])
833+
let obMsg = [42, [[100, 2, 3]]]
834+
ws._handleOBMessage(obMsg, ws._channelMap[42], JSON.stringify(obMsg))
829835
})
830836

831837
it('_handleOBMessage: filters by prec and len', (done) => {
@@ -863,8 +869,9 @@ describe('WSv2 channel msg handling', () => {
863869
if (++seen === 2) done()
864870
})
865871

866-
ws._handleOBMessage([42, [[100, 2, 3]]], ws._channelMap[42])
867-
ws._handleOBMessage([42, [100, 2, 3]], ws._channelMap[42])
872+
let obMsg = [42, [[100, 2, 3]]]
873+
ws._handleOBMessage(obMsg, ws._channelMap[42], JSON.stringify(obMsg))
874+
ws._handleOBMessage(obMsg, ws._channelMap[42], JSON.stringify(obMsg))
868875
})
869876

870877
it('_handleOBMessage: emits managed ob', (done) => {
@@ -882,7 +889,8 @@ describe('WSv2 channel msg handling', () => {
882889
done()
883890
})
884891

885-
ws._handleOBMessage([42, [[100, 2, 3]]], ws._channelMap[42])
892+
let obMsg = [42, [[100, 2, 3]]]
893+
ws._handleOBMessage(obMsg, ws._channelMap[42], JSON.stringify(obMsg))
886894
})
887895

888896
it('_handleOBMessage: forwards transformed data if transform enabled', (done) => {
@@ -903,7 +911,8 @@ describe('WSv2 channel msg handling', () => {
903911
done()
904912
})
905913

906-
ws._handleOBMessage([42, [[100, 2, 3]]], ws._channelMap[42])
914+
let obMsg = [42, [[100, 2, 3]]]
915+
ws._handleOBMessage(obMsg, ws._channelMap[42], obMsg)
907916
})
908917

909918
it('_updateManagedOB: does nothing on rm non-existent entry', () => {
@@ -912,8 +921,12 @@ describe('WSv2 channel msg handling', () => {
912921
[100, 1, 1],
913922
[200, 2, 1]
914923
]
924+
ws._losslessOrderBooks.tBTCUSD = [
925+
['100', '1', '1'],
926+
['200', '2', '1']
927+
]
915928

916-
const err = ws._updateManagedOB('tBTCUSD', [150, 0, -1])
929+
const err = ws._updateManagedOB('tBTCUSD', [150, 0, -1], false, JSON.stringify([1, [150, 0, -1]]))
917930
assert.strictEqual(err, null)
918931
assert.deepStrictEqual(ws._orderBooks.tBTCUSD, [
919932
[100, 1, 1],
@@ -924,10 +937,12 @@ describe('WSv2 channel msg handling', () => {
924937
it('_updateManagedOB: correctly maintains transformed OBs', () => {
925938
const ws = new WSv2({ transform: true })
926939
ws._orderBooks.tBTCUSD = []
940+
ws._losslessOrderBooks.tBTCUSD = []
927941

928-
assert(!ws._updateManagedOB('tBTCUSD', [100, 1, 1]))
929-
assert(!ws._updateManagedOB('tBTCUSD', [200, 1, -1]))
930-
assert(!ws._updateManagedOB('tBTCUSD', [200, 0, -1]))
942+
// symbol, data, raw, rawMsg
943+
assert(!ws._updateManagedOB('tBTCUSD', [100, 1, 1], false, JSON.stringify([1, [100, 1, 1]])))
944+
assert(!ws._updateManagedOB('tBTCUSD', [200, 1, -1], false, JSON.stringify([1, [200, 1, -1]])))
945+
assert(!ws._updateManagedOB('tBTCUSD', [200, 0, -1], false, JSON.stringify([1, [200, 0, -1]])))
931946

932947
const ob = ws.getOB('tBTCUSD')
933948

@@ -939,10 +954,11 @@ describe('WSv2 channel msg handling', () => {
939954
it('_updateManagedOB: correctly maintains non-transformed OBs', () => {
940955
const ws = new WSv2()
941956
ws._orderBooks.tBTCUSD = []
957+
ws._losslessOrderBooks.tBTCUSD = []
942958

943-
assert(!ws._updateManagedOB('tBTCUSD', [100, 1, 1]))
944-
assert(!ws._updateManagedOB('tBTCUSD', [200, 1, -1]))
945-
assert(!ws._updateManagedOB('tBTCUSD', [200, 0, -1]))
959+
assert(!ws._updateManagedOB('tBTCUSD', [100, 1, 1], false, JSON.stringify([1, [100, 1, 1]])))
960+
assert(!ws._updateManagedOB('tBTCUSD', [200, 1, -1], false, JSON.stringify([1, [200, 1, -1]])))
961+
assert(!ws._updateManagedOB('tBTCUSD', [200, 0, -1], false, JSON.stringify([1, [200, 0, -1]])))
946962

947963
const ob = ws._orderBooks.tBTCUSD
948964

0 commit comments

Comments
 (0)