github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/tests/integration/v2/live_websocket_private_test.go (about) 1 package tests 2 3 import ( 4 "context" 5 "sync" 6 "testing" 7 "time" 8 9 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/common" 10 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/notification" 11 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/order" 12 "github.com/bitfinexcom/bitfinex-api-go/v2" 13 "github.com/bitfinexcom/bitfinex-api-go/v2/websocket" 14 ) 15 16 func TestWebsocketOrder(t *testing.T) { 17 if !auth { 18 t.Skip("no credentials, skipping order creation") 19 } 20 21 wg := sync.WaitGroup{} 22 wg.Add(1) // 1. Authentication event 23 24 c := websocket.New().Credentials(key, secret) 25 26 errch := make(chan error) 27 28 err := c.Connect() 29 if err != nil { 30 t.Fatalf("connecting to websocket service: %s", err) 31 } 32 defer c.Close() 33 34 go func() { 35 for ev := range c.Listen() { 36 switch e := ev.(type) { 37 case *notification.Notification: 38 if e.Status == "ERROR" && e.Type == "on-req" { 39 t.Errorf("failed to create order: %s", e.Text) 40 } 41 case *order.New: 42 wg.Done() 43 case *order.Cancel: 44 wg.Done() 45 case error: 46 t.Logf("Listen() error: %s", ev) 47 errch <- ev.(error) 48 wg.Done() 49 } 50 } 51 }() 52 /* 53 err = c.Authenticate(context.Background()) 54 if err != nil { 55 t.Fatalf("authenticating with websocket service: %s", err) 56 } 57 if err := wait(&wg, errch, 2*time.Second); err != nil { 58 t.Fatalf("failed to authenticate with websocket service: %s", err) 59 } 60 */ 61 wg.Add(1) 62 n := time.Now() 63 cid := n.Unix() 64 cidDate := n.Format("2006-01-02") 65 o := &order.NewRequest{ 66 CID: cid, 67 Type: common.OrderTypeExchangeLimit, 68 Symbol: common.TradingPrefix + bitfinex.BTCUSD, 69 Amount: 1.0, 70 Price: 28.5, 71 } 72 73 ctx, cxl1 := context.WithTimeout(context.Background(), 2*time.Second) 74 defer cxl1() 75 err = c.SubmitOrder(ctx, o) 76 if err != nil { 77 t.Fatalf("failed to send OrderNewRequest: %s", err) 78 } 79 if err := wait(&wg, errch, 2*time.Second); err != nil { 80 t.Fatalf("failed to create order: %s", err) 81 } 82 83 oc := &order.CancelRequest{ 84 CID: cid, 85 CIDDate: cidDate, 86 } 87 88 wg.Add(1) 89 ctx, cxl2 := context.WithTimeout(context.Background(), 2*time.Second) 90 defer cxl2() 91 err = c.SubmitCancel(ctx, oc) 92 if err != nil { 93 t.Fatalf("failed to send OrderCancelRequest: %s", err) 94 } 95 if err := wait(&wg, errch, 2*time.Second); err != nil { 96 t.Fatalf("failed to cancel order: %s", err) 97 } 98 }