2024-01-02 04:02:33 +00:00
|
|
|
package okex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2024-01-10 05:56:17 +00:00
|
|
|
"time"
|
2024-01-02 04:02:33 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/testutil"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getTestClientOrSkip(t *testing.T) *Stream {
|
|
|
|
if b, _ := strconv.ParseBool(os.Getenv("CI")); b {
|
|
|
|
t.Skip("skip test for CI")
|
|
|
|
}
|
|
|
|
|
|
|
|
key, secret, passphrase, ok := testutil.IntegrationTestWithPassphraseConfigured(t, "OKEX")
|
|
|
|
if !ok {
|
|
|
|
t.Skip("OKEX_* env vars are not configured")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
exchange := New(key, secret, passphrase)
|
2024-01-17 03:34:15 +00:00
|
|
|
return NewStream(exchange.client, exchange)
|
2024-01-02 04:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStream(t *testing.T) {
|
|
|
|
t.Skip()
|
|
|
|
s := getTestClientOrSkip(t)
|
|
|
|
|
2024-01-09 02:57:33 +00:00
|
|
|
t.Run("account test", func(t *testing.T) {
|
|
|
|
err := s.Connect(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s.OnBalanceUpdate(func(balances types.BalanceMap) {
|
|
|
|
t.Log("got snapshot", balances)
|
|
|
|
})
|
2024-01-17 03:34:15 +00:00
|
|
|
s.OnBalanceSnapshot(func(balances types.BalanceMap) {
|
|
|
|
t.Log("got snapshot", balances)
|
|
|
|
})
|
2024-01-09 02:57:33 +00:00
|
|
|
s.OnBookUpdate(func(book types.SliceOrderBook) {
|
|
|
|
t.Log("got update", book)
|
|
|
|
})
|
|
|
|
c := make(chan struct{})
|
|
|
|
<-c
|
|
|
|
})
|
|
|
|
|
2024-01-02 04:02:33 +00:00
|
|
|
t.Run("book test", func(t *testing.T) {
|
|
|
|
s.Subscribe(types.BookChannel, "BTCUSDT", types.SubscribeOptions{
|
2024-01-10 06:02:03 +00:00
|
|
|
Depth: types.DepthLevel400,
|
2024-01-02 04:02:33 +00:00
|
|
|
})
|
|
|
|
s.SetPublicOnly()
|
|
|
|
err := s.Connect(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s.OnBookSnapshot(func(book types.SliceOrderBook) {
|
|
|
|
t.Log("got snapshot", book)
|
|
|
|
})
|
|
|
|
s.OnBookUpdate(func(book types.SliceOrderBook) {
|
|
|
|
t.Log("got update", book)
|
|
|
|
})
|
|
|
|
c := make(chan struct{})
|
|
|
|
<-c
|
|
|
|
})
|
2024-01-08 07:05:32 +00:00
|
|
|
|
|
|
|
t.Run("market trade test", func(t *testing.T) {
|
|
|
|
s.Subscribe(types.MarketTradeChannel, "BTCUSDT", types.SubscribeOptions{})
|
|
|
|
s.SetPublicOnly()
|
|
|
|
err := s.Connect(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s.OnMarketTrade(func(trade types.Trade) {
|
|
|
|
t.Log("got trade upgrade", trade)
|
|
|
|
})
|
|
|
|
c := make(chan struct{})
|
|
|
|
<-c
|
|
|
|
})
|
|
|
|
|
2024-01-02 15:18:57 +00:00
|
|
|
t.Run("kline test", func(t *testing.T) {
|
|
|
|
s.Subscribe(types.KLineChannel, "LTC-USD-200327", types.SubscribeOptions{
|
|
|
|
Interval: types.Interval1m,
|
|
|
|
})
|
|
|
|
s.SetPublicOnly()
|
|
|
|
err := s.Connect(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s.OnKLine(func(kline types.KLine) {
|
|
|
|
t.Log("got update", kline)
|
|
|
|
})
|
|
|
|
s.OnKLineClosed(func(kline types.KLine) {
|
|
|
|
t.Log("got closed", kline)
|
|
|
|
})
|
|
|
|
c := make(chan struct{})
|
|
|
|
<-c
|
|
|
|
})
|
2024-01-10 05:56:17 +00:00
|
|
|
|
|
|
|
t.Run("Subscribe/Unsubscribe test", func(t *testing.T) {
|
|
|
|
s.Subscribe(types.BookChannel, "BTCUSDT", types.SubscribeOptions{
|
2024-01-10 06:02:03 +00:00
|
|
|
Depth: types.DepthLevel400,
|
2024-01-10 05:56:17 +00:00
|
|
|
})
|
|
|
|
s.SetPublicOnly()
|
|
|
|
err := s.Connect(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s.OnBookSnapshot(func(book types.SliceOrderBook) {
|
|
|
|
t.Log("got snapshot", book)
|
|
|
|
})
|
|
|
|
s.OnBookUpdate(func(book types.SliceOrderBook) {
|
|
|
|
t.Log("got update", book)
|
|
|
|
})
|
|
|
|
|
|
|
|
<-time.After(5 * time.Second)
|
|
|
|
|
|
|
|
s.Unsubscribe()
|
|
|
|
c := make(chan struct{})
|
|
|
|
<-c
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Resubscribe test", func(t *testing.T) {
|
|
|
|
s.Subscribe(types.BookChannel, "BTCUSDT", types.SubscribeOptions{
|
2024-01-10 06:02:03 +00:00
|
|
|
Depth: types.DepthLevel400,
|
2024-01-10 05:56:17 +00:00
|
|
|
})
|
|
|
|
s.SetPublicOnly()
|
|
|
|
err := s.Connect(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s.OnBookSnapshot(func(book types.SliceOrderBook) {
|
|
|
|
t.Log("got snapshot", book)
|
|
|
|
})
|
|
|
|
s.OnBookUpdate(func(book types.SliceOrderBook) {
|
|
|
|
t.Log("got update", book)
|
|
|
|
})
|
|
|
|
|
|
|
|
<-time.After(5 * time.Second)
|
|
|
|
|
|
|
|
s.Resubscribe(func(old []types.Subscription) (new []types.Subscription, err error) {
|
|
|
|
return old, nil
|
|
|
|
})
|
|
|
|
c := make(chan struct{})
|
|
|
|
<-c
|
|
|
|
})
|
2024-01-02 04:02:33 +00:00
|
|
|
}
|