github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/tests/integration/v2/mock_ws_public_test.go (about) 1 package tests 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/common" 8 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/ticker" 9 "github.com/bitfinexcom/bitfinex-api-go/v2" 10 "github.com/bitfinexcom/bitfinex-api-go/v2/websocket" 11 ) 12 13 // method of testing with mocked endpoints 14 func TestTicker(t *testing.T) { 15 // create transport & nonce mocks 16 async := newTestAsync() 17 nonce := &IncrementingNonceGenerator{} 18 19 // create client 20 ws := websocket.NewWithAsyncFactoryNonce(newTestAsyncFactory(async), nonce) 21 22 // setup listener 23 listener := newListener() 24 listener.run(ws.Listen()) 25 26 // set ws options 27 err_ws := ws.Connect() 28 if err_ws != nil { 29 t.Fatal(err_ws) 30 } 31 defer ws.Close() 32 33 // info welcome msg 34 async.Publish(`{"event":"info","version":2}`) 35 ev, err := listener.nextInfoEvent() 36 if err != nil { 37 t.Fatal(err) 38 } 39 assert(t, &websocket.InfoEvent{Version: 2}, ev) 40 41 // subscribe 42 id, err := ws.SubscribeTicker(context.Background(), "tBTCUSD") 43 if err != nil { 44 t.Fatal(err) 45 } 46 47 // subscribe ack 48 async.Publish(`{"event":"subscribed","channel":"ticker","chanId":5,"symbol":"tBTCUSD","subId":"nonce1","pair":"BTCUSD"}`) 49 sub, err := listener.nextSubscriptionEvent() 50 if err != nil { 51 t.Fatal(err) 52 } 53 assert(t, &websocket.SubscribeEvent{ 54 SubID: "nonce1", 55 Channel: "ticker", 56 ChanID: 5, 57 Symbol: "tBTCUSD", 58 Pair: "BTCUSD", 59 }, sub) 60 61 // tick data 62 async.Publish(`[5,[14957,68.17328796,14958,55.29588132,-659,-0.0422,14971,53723.08813995,16494,14454]]`) 63 tick, err := listener.nextTick() 64 if err != nil { 65 t.Fatal(err) 66 } 67 assert(t, &ticker.Ticker{ 68 Symbol: "tBTCUSD", 69 Bid: 14957, 70 Ask: 14958, 71 BidSize: 68.17328796, 72 AskSize: 55.29588132, 73 DailyChange: -659, 74 DailyChangePerc: -0.0422, 75 LastPrice: 14971, 76 Volume: 53723.08813995, 77 High: 16494, 78 Low: 14454, 79 }, tick) 80 81 // unsubscribe 82 err_unsub := ws.Unsubscribe(context.Background(), id) 83 if err_unsub != nil { 84 t.Fatal(err_unsub) 85 } 86 async.Publish(`{"event":"unsubscribed","chanId":5,"status":"OK"}`) 87 unsub, err := listener.nextUnsubscriptionEvent() 88 if err != nil { 89 t.Fatal(err) 90 } 91 assert(t, &websocket.UnsubscribeEvent{ChanID: 5, Status: "OK"}, unsub) 92 } 93 94 func TestOrderbook(t *testing.T) { 95 // create transport & nonce mocks 96 async := newTestAsync() 97 98 // create client 99 p := websocket.NewDefaultParameters() 100 p.ManageOrderbook = true 101 ws := websocket.NewWithParamsAsyncFactory(p, newTestAsyncFactory(async)) 102 103 // setup listener 104 listener := newListener() 105 listener.run(ws.Listen()) 106 107 // set ws options 108 err_ws := ws.Connect() 109 if err_ws != nil { 110 t.Fatal(err_ws) 111 } 112 defer ws.Close() 113 114 // info welcome msg 115 async.Publish(`{"event":"info","version":2}`) 116 ev, err := listener.nextInfoEvent() 117 if err != nil { 118 t.Fatal(err) 119 } 120 assert(t, &websocket.InfoEvent{Version: 2}, ev) 121 122 // we will use XRPBTC since that uses reallllyy small numbers 123 bId, err_st := ws.SubscribeBook(context.Background(), common.TradingPrefix+bitfinex.XRPBTC, common.Precision0, common.FrequencyRealtime, 25) 124 if err_st != nil { 125 t.Fatal(err_st) 126 } 127 // checksum enabled ack 128 async.Publish(`{"event":"conf","status":"OK","flags":131072}`) 129 // subscribe ack 130 async.Publish(`{"event":"subscribed","channel":"book","chanId":81757,"symbol":"tXRPBTC","prec":"P0","freq":"F0","len":"25","subId":"` + bId + `","pair":"XRPBTC"}`) 131 132 // publish a snapshot 133 async.Publish(`[81757,[[0.0000011,13,271510.49],[0.00000109,4,500793.10790141],[0.00000108,5,776367.43],[0.00000107,1,23329.54842056],[0.00000106,3,116868.87735849],[0.00000105,3,205000],[0.00000103,3,227308.25386407],[0.00000102,2,105000],[0.00000101,1,2970],[0.000001,2,21000],[7e-7,1,10000],[6.6e-7,1,10000],[6e-7,1,100000],[4.9e-7,1,10000],[2.5e-7,1,2000],[6e-8,1,100000],[5e-8,1,200000],[1e-8,4,640000],[0.00000111,1,-4847.13],[0.00000112,7,-528102.69042633],[0.00000113,5,-302397.07],[0.00000114,3,-339088.93],[0.00000126,4,-245944.06],[0.00000127,1,-5000],[0.0000013,1,-5000],[0.00000134,1,-8249.18938656],[0.00000136,1,-13161.25184766],[0.00000145,1,-2914],[0.0000015,3,-54448.5],[0.00000152,2,-5538.54849594],[0.00000153,1,-62691.75475079],[0.00000159,1,-2914],[0.0000016,1,-52631.10296831],[0.00000164,1,-4000],[0.00000166,1,-3831.46784605],[0.00000171,1,-14575.17730379],[0.00000174,1,-3124.81815395],[0.0000018,1,-18000],[0.00000182,1,-16000],[0.00000186,1,-4000],[0.00000189,1,-10000.686624],[0.00000191,1,-14500],[0.00000193,1,-2422]]]`) 134 135 // publish new trade update 136 async.Publish(`[81757,[0.0000011,12,266122.94]]`) 137 138 // test that we can retrieve the orderbook 139 ob, err_ob := ws.GetOrderbook("tXRPBTC") 140 if err_ob != nil { 141 t.Fatal(err_ob) 142 } 143 144 // test that changing the orderbook values will not invalidate the checksum 145 // since they have been dereferenced 146 ob.Bids()[0].Amount = 9999999 147 148 // publish new checksum 149 pre := async.SentCount() 150 async.Publish(`[81757,"cs",-1175357890]`) 151 152 // test that the new trade has been added to the orderbook 153 newTrade := ob.Bids()[0] 154 // check that it has overwritten the original trade in the book at that price 155 if newTrade.PriceJsNum.String() != "0.0000011" { 156 t.Fatal("Newly submitted trade did not update into orderbook") 157 } 158 if newTrade.AmountJsNum.String() != "266122.94" { 159 t.Fatal("Newly submitted trade did not update into orderbook") 160 } 161 // check that we did not send an unsubscribe message 162 // because that would mean the checksum was incorrect 163 if err_unsub := async.waitForMessage(pre); err_unsub != nil { 164 // no message sent 165 return 166 } else { 167 t.Fatal("A new unsubscribe message was sent") 168 } 169 } 170 171 // func TestCreateNewSocket(t *testing.T) { 172 // // create transport & nonce mocks 173 // async := newTestAsync() 174 175 // // create client 176 // p := websocket.NewDefaultParameters() 177 // // lock the capacity to 10 178 // p.CapacityPerConnection = 10 179 // p.ManageOrderbook = true 180 // ws := websocket.NewWithParamsAsyncFactory(p, newTestAsyncFactory(async)) 181 182 // // setup listener 183 // listener := newListener() 184 // listener.run(ws.Listen()) 185 186 // // set ws options 187 // err_ws := ws.Connect() 188 // if err_ws != nil { 189 // t.Fatal(err_ws) 190 // } 191 // defer ws.Close() 192 193 // // info welcome msg 194 // async.Publish(`{"event":"info","version":2}`) 195 // ev, err := listener.nextInfoEvent() 196 // if err != nil { 197 // t.Fatal(err) 198 // } 199 // assert(t, &websocket.InfoEvent{Version: 2}, ev) 200 201 // tickers := []string{"tBTCUSD", "tETHUSD", "tBTCUSD", "tVETUSD", "tDGBUSD", "tEOSUSD", "tTRXUSD", "tEOSETH", "tBTCETH", 202 // "tBTCEOS", "tXRPUSD", "tXRPBTC", "tTRXETH", "tTRXBTC", "tLTCUSD", "tLTCBTC", "tLTCETH"} 203 // for i, ticker := range tickers { 204 // id := i * 10 205 // // subscribe to 15m candles 206 // id1, err := ws.SubscribeCandles(context.Background(), ticker, common.FifteenMinutes) 207 // if err != nil { 208 // t.Fatal(err) 209 // } 210 // async.Publish(`{"event":"subscribed","channel":"candles","chanId":` + fmt.Sprintf("%d", id) + `,"key":"trade:15m:` + ticker + `","subId":"` + id1 + `"}`) 211 // // subscribe to 1hr candles 212 // id2, err := ws.SubscribeCandles(context.Background(), ticker, common.OneHour) 213 // if err != nil { 214 // t.Fatal(err) 215 // } 216 // // subscribe ack 217 // async.Publish(`{"event":"subscribed","channel":"candles","chanId":` + fmt.Sprintf("%d", id+1) + `,"key":"trade:1hr:` + ticker + `","subId":"` + id2 + `"}`) 218 // // subscribe to 30min candles 219 // id3, err := ws.SubscribeCandles(context.Background(), ticker, common.OneHour) 220 // if err != nil { 221 // t.Fatal(err) 222 // } 223 // // subscribe ack 224 // async.Publish(`{"event":"subscribed","channel":"candles","chanId":` + fmt.Sprintf("%d", id+2) + `,"key":"trade:30m:` + ticker + `","subId":"` + id3 + `"}`) 225 // } 226 // conCount := ws.ConnectionCount() 227 // if conCount != 6 { 228 // t.Fatal("Expected socket count to be 6 but got", conCount) 229 // } 230 // }