Skip to content

Commit

Permalink
Merge pull request #143 from JacobPlaster/add-travis-cli
Browse files Browse the repository at this point in the history
Add travis cli
  • Loading branch information
prdn committed Jan 15, 2019
2 parents 48518f6 + 1ae7c50 commit 306fa24
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 97 deletions.
1 change: 1 addition & 0 deletions tests/integration/v2/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func (l *listener) nextOrderUpdate() (*bitfinex.OrderUpdate, error) {
// strongly types messages and places them into a channel
func (l *listener) run(ch <-chan interface{}) {
go func() {
// nolint:megacheck
for {
select {
case msg := <-ch:
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/v2/live_websocket_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func wait2(ch <-chan interface{}, count int, bc <-chan error, t time.Duration) e
case <-time.After(t):
return fmt.Errorf("timed out waiting")
}
return nil
}

func wait(wg *sync.WaitGroup, bc <-chan error, to time.Duration) error {
Expand Down Expand Up @@ -62,6 +61,7 @@ func TestPublicTicker(t *testing.T) {

errch := make(chan error)
go func() {
// nolint:megacheck
for {
select {
case msg := <-c.Listen():
Expand Down Expand Up @@ -128,6 +128,7 @@ func TestPublicTrades(t *testing.T) {

errch := make(chan error)
go func() {
// nolint:megacheck
for {
select {
case msg := <-c.Listen():
Expand Down Expand Up @@ -200,6 +201,7 @@ func TestPublicBooks(t *testing.T) {

errch := make(chan error)
go func() {
// nolint:megacheck
for {
select {
case msg := <-c.Listen():
Expand Down Expand Up @@ -266,6 +268,7 @@ func TestPublicCandles(t *testing.T) {

errch := make(chan error)
go func() {
// nolint:megacheck
for {
select {
case msg := <-c.Listen():
Expand Down
70 changes: 55 additions & 15 deletions tests/integration/v2/mock_ws_private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func TestAuthentication(t *testing.T) {
listener.run(ws.Listen())

// set ws options
ws.Connect()
err_ws := ws.Connect()
if err_ws != nil {
t.Fatal(err_ws)
}
defer ws.Close()

// begin test
Expand Down Expand Up @@ -63,7 +66,10 @@ func TestWalletBalanceUpdates(t *testing.T) {

// set ws options
//ws.SetReadTimeout(time.Second * 2)
ws.Connect()
err_ws := ws.Connect()
if err_ws != nil {
t.Fatal(err_ws)
}
defer ws.Close()

// begin test--authentication assertions in TestAuthentication
Expand Down Expand Up @@ -133,7 +139,10 @@ func TestNewOrder(t *testing.T) {

// set ws options
//ws.SetReadTimeout(time.Second * 2)
ws.Connect()
err_ws := ws.Connect()
if err_ws != nil {
t.Fatal(err_ws)
}
defer ws.Close()

// begin test
Expand Down Expand Up @@ -195,7 +204,10 @@ func TestFills(t *testing.T) {

// set ws options
//ws.SetReadTimeout(time.Second * 2)
ws.Connect()
err_ws := ws.Connect()
if err_ws != nil {
t.Fatal(err_ws)
}
defer ws.Close()

// begin test
Expand Down Expand Up @@ -285,8 +297,14 @@ func TestFills(t *testing.T) {
async.Publish(`[0,"te",[1,"tBTCUSD",1514909325593,1234567,0.21679716,915.9,null,null,-1]]`)
async.Publish(`[0,"te",[2,"tBTCUSD",1514909325597,1234567,0.78320284,916.2,null,null,-1]]`)
te, err := listener.nextTradeExecution()
if err != nil {
t.Fatal(err)
}
assert(t, &bitfinex.TradeExecution{ID: 1, MTS: 1514909325593, Amount: 0.21679716, Price: 915.9}, te)
te, _ = listener.nextTradeExecution()
te, err = listener.nextTradeExecution()
if err != nil {
t.Fatal(err)
}
assert(t, &bitfinex.TradeExecution{ID: 2, MTS: 1514909325597, Amount: 0.78320284, Price: 916.2}, te)

// fills--trade updates
Expand Down Expand Up @@ -361,7 +379,10 @@ func TestCancel(t *testing.T) {

// set ws options
//ws.SetReadTimeout(time.Second * 2)
ws.Connect()
err_ws := ws.Connect()
if err_ws != nil {
t.Fatal(err_ws)
}
defer ws.Close()

// begin test
Expand All @@ -376,8 +397,14 @@ func TestCancel(t *testing.T) {
async.Publish(`[0,"ps",[["tBTCUSD","ACTIVE",7,916.52002351,0,0,null,null,null,null]]]`)
async.Publish(`[0,"ws",[["exchange","BTC",30,0,null],["exchange","USD",80000,0,null],["exchange","ETH",100,0,null],["margin","BTC",10,0,null],["margin","USD",9987.16871968,0,null],["funding","BTC",10,0,null],["funding","USD",10000,0,null]]]`)
// consume & assert snapshots
listener.nextPositionSnapshot()
listener.nextWalletSnapshot()
_, err_ps := listener.nextPositionSnapshot()
if err_ps != nil {
t.Fatal(err_ps)
}
_, err_was := listener.nextWalletSnapshot()
if err_was != nil {
t.Fatal(err_was)
}

// submit order
err = ws.SubmitOrder(context.Background(), &bitfinex.OrderNewRequest{
Expand All @@ -403,7 +430,10 @@ func TestCancel(t *testing.T) {
async.Publish(`[0,"on",[1234567,0,123,"tBTCUSD",1515179518260,1515179518315,1,1,"LIMIT",null,null,null,0,"ACTIVE",null,null,900,0,null,null,null,null,null,0,0,0]]`)

// eat order ack notification
listener.nextNotification()
_, err_n := listener.nextNotification()
if err_n != nil {
t.Fatal(err_n)
}

on, err := listener.nextOrderNew()
if err != nil {
Expand Down Expand Up @@ -452,9 +482,10 @@ func TestUpdateOrder(t *testing.T) {
listener := newListener()
listener.run(ws.Listen())

// set ws options
//ws.SetReadTimeout(time.Second * 2)
ws.Connect()
err_ws := ws.Connect()
if err_ws != nil {
t.Fatal(err_ws)
}
defer ws.Close()

// begin test
Expand All @@ -469,8 +500,14 @@ func TestUpdateOrder(t *testing.T) {
async.Publish(`[0,"ps",[["tBTCUSD","ACTIVE",7,916.52002351,0,0,null,null,null,null]]]`)
async.Publish(`[0,"ws",[["exchange","BTC",30,0,null],["exchange","USD",80000,0,null],["exchange","ETH",100,0,null],["margin","BTC",10,0,null],["margin","USD",9987.16871968,0,null],["funding","BTC",10,0,null],["funding","USD",10000,0,null]]]`)
// consume & assert snapshots
listener.nextPositionSnapshot()
listener.nextWalletSnapshot()
_, errps := listener.nextPositionSnapshot()
if errps != nil {
t.Fatal(errps)
}
_, errws := listener.nextWalletSnapshot()
if errws != nil {
t.Fatal(errws)
}

// submit order
err = ws.SubmitOrder(context.Background(), &bitfinex.OrderNewRequest{
Expand All @@ -496,7 +533,10 @@ func TestUpdateOrder(t *testing.T) {
async.Publish(`[0,"on",[1234567,0,123,"tBTCUSD",1515179518260,1515179518315,1,1,"LIMIT",null,null,null,0,"ACTIVE",null,null,900,0,null,null,null,null,null,0,0,0]]`)

// eat order ack notification
listener.nextNotification()
_, errn := listener.nextNotification()
if errn != nil {
t.Fatal(errn)
}

on, err := listener.nextOrderNew()
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions tests/integration/v2/mock_ws_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func TestTicker(t *testing.T) {
listener.run(ws.Listen())

// set ws options
ws.Connect()
err_ws := ws.Connect()
if err_ws != nil {
t.Fatal(err_ws)
}
defer ws.Close()

// info welcome msg
Expand Down Expand Up @@ -74,7 +77,10 @@ func TestTicker(t *testing.T) {
}, tick)

// unsubscribe
ws.Unsubscribe(context.Background(), id)
err_unsub := ws.Unsubscribe(context.Background(), id)
if err_unsub != nil {
t.Fatal(err_unsub)
}
async.Publish(`{"event":"unsubscribed","chanId":5,"status":"OK"}`)
unsub, err := listener.nextUnsubscriptionEvent()
if err != nil {
Expand Down
11 changes: 9 additions & 2 deletions tests/integration/v2/reconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ func setup(t *testing.T, hbTimeout time.Duration, autoReconnect, auth bool) {
apiRecv.run(apiClient.Listen())

// set ws options
apiClient.Connect()
err_con := apiClient.Connect()
if err_con != nil {
t.Fatal(err_con)
}

if err := wsService.WaitForClientCount(1); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -178,6 +181,7 @@ func TestReconnectResubscribeWithAuth(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// nolint:megacheck
diff := time.Now().Sub(now)
t.Logf("client disconnect detected in %s", diff.String())

Expand All @@ -187,7 +191,10 @@ func TestReconnectResubscribeWithAuth(t *testing.T) {
if wsService.TotalClientCount() != 0 {
t.Fatalf("total client count %d, expected non-zero", wsService.TotalClientCount())
}
wsService.Start()
err_ws := wsService.Start()
if err_ws != nil {
t.Fatal(err_ws)
}
if err := wsService.WaitForClientCount(1); err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/v2/test_ws_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func (s *TestWsService) Stop() {
}
}

//nolint
func (s *TestWsService) Start() error {
go s.loop()
l, err := net.Listen("tcp", fmt.Sprintf(":%d", s.port))
Expand All @@ -178,6 +179,7 @@ func (s *TestWsService) Start() error {
return nil
}

//nolint
func (s *TestWsService) serveWs(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: go

go:
- 1.11.x

install:
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.12.5

script:
- golangci-lint run
- go test -v -race ./...
15 changes: 11 additions & 4 deletions v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,22 @@ func (c *Client) newAuthenticatedRequest(m string, refURL string, data map[strin
req.Header.Add("Accept", "application/json")
req.Header.Add("X-BFX-APIKEY", c.APIKey)
req.Header.Add("X-BFX-PAYLOAD", encoded)
req.Header.Add("X-BFX-SIGNATURE", c.signPayload(encoded))
sig, err := c.signPayload(encoded)
if err != nil {
return nil, err
}
req.Header.Add("X-BFX-SIGNATURE", sig)

return req, nil
}

func (c *Client) signPayload(payload string) string {
func (c *Client) signPayload(payload string) (string, error) {
sig := hmac.New(sha512.New384, []byte(c.APISecret))
sig.Write([]byte(payload))
return hex.EncodeToString(sig.Sum(nil))
_, err := sig.Write([]byte(payload))
if err != nil {
return "", err
}
return hex.EncodeToString(sig.Sum(nil)), nil
}

// Auth sets api key and secret for usage is requests that requires authentication.
Expand Down
2 changes: 1 addition & 1 deletion v1/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (s *OrderService) CancelMulti(orderIDS []int64) (string, error) {
return "", err
}

response := make(map[string]string, 0)
response := make(map[string]string)
_, err = s.client.do(req, &response)

return response["result"], err
Expand Down
2 changes: 1 addition & 1 deletion v1/pairs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestPairsAllDetailed(t *testing.T) {
t.Error("Actual", pairs[0].Expiration)
}

if pairs[0].Margin != true {
if !pairs[0].Margin {
t.Error("Expected", true)
t.Error("Actual", pairs[0].Margin)
}
Expand Down
9 changes: 7 additions & 2 deletions v1/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (

// WebSocketService allow to connect and receive stream data
// from bitfinex.com ws service.
// nolint:megacheck,structcheck
type WebSocketService struct {
// http client
client *Client
Expand Down Expand Up @@ -161,7 +162,7 @@ func (w *WebSocketService) Subscribe() error {
w.handleDataMessage(p)
}
}

// nolint
return nil
}

Expand Down Expand Up @@ -279,10 +280,14 @@ func (w *WebSocketService) ConnectPrivate(ch chan TermData) {

nonce := utils.GetNonce()
payload := "AUTH" + nonce
sig, err_sig := w.client.signPayload(payload)
if err_sig != nil {
return
}
connectMsg, _ := json.Marshal(&privateConnect{
Event: "auth",
APIKey: w.client.APIKey,
AuthSig: w.client.signPayload(payload),
AuthSig: sig,
AuthPayload: payload,
})

Expand Down
Loading

0 comments on commit 306fa24

Please sign in to comment.