github.com/vanclief/uniex@v0.15.8/platforms/trading/kraken/ws/ws_test.go (about)

     1  package ws
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/vanclief/finmod/market"
    10  	"github.com/vanclief/uniex/interfaces/ws/genericws"
    11  )
    12  
    13  func TestWs(t *testing.T) {
    14  
    15  	var opts []genericws.Option
    16  
    17  	btc := market.Pair{
    18  		Base:  market.Asset{Symbol: "BTC"},
    19  		Quote: market.Asset{Symbol: "USD"},
    20  	}
    21  
    22  	//eth := market.Pair{
    23  	//	Base:  market.Asset{Symbol: "ETH"},
    24  	//	Quote: market.Asset{Symbol: "USD"},
    25  	//}
    26  
    27  	opts = append(opts, genericws.WithSubscriptionTo(btc))
    28  	//opts = append(opts, genericws.WithSubscriptionTo(eth))
    29  
    30  	handler := NewHandler()
    31  
    32  	opts = append(opts, genericws.WithName("Kraken"))
    33  	ws, err := genericws.NewClient(handler, opts...)
    34  
    35  	assert.Nil(t, err)
    36  	assert.NotNil(t, ws)
    37  
    38  	ctx := context.Background()
    39  
    40  	wsChannel, err := ws.Listen(ctx)
    41  	assert.Nil(t, err)
    42  
    43  	for {
    44  		select {
    45  		case <-ctx.Done():
    46  			return
    47  		case msg, ok := <-wsChannel:
    48  			assert.True(t, ok)
    49  
    50  			if msg.OrderBook.Time > 0 {
    51  				if len(msg.OrderBook.Bids) > 0 && len(msg.OrderBook.Asks) > 0 {
    52  					if msg.OrderBook.Asks[0].Price < msg.OrderBook.Bids[0].Price {
    53  						//msg.OrderBook.Print()
    54  						t.FailNow()
    55  					}
    56  					fmt.Println(msg.Pair.String(), len(msg.OrderBook.Asks), len(msg.OrderBook.Bids))
    57  				} else {
    58  					fmt.Println("empty")
    59  				}
    60  				//fmt.Println("ob", msg.OrderBook.String())
    61  			}
    62  
    63  			//if len(msg.Tickers) > 0 && msg.Tickers[0].Time > 0 {
    64  			//	fmt.Println("tick", msg.Tickers[0])
    65  			//}
    66  		}
    67  	}
    68  }
    69  
    70  func BenchmarkWs(t *testing.B) {
    71  
    72  	var opts []genericws.Option
    73  
    74  	btc := market.Pair{
    75  		Base:  market.Asset{Symbol: "BTC"},
    76  		Quote: market.Asset{Symbol: "USD"},
    77  	}
    78  
    79  	//eth := market.Pair{
    80  	//	Base:  market.Asset{Symbol: "ETH"},
    81  	//	Quote: market.Asset{Symbol: "USD"},
    82  	//}
    83  
    84  	opts = append(opts, genericws.WithSubscriptionTo(btc))
    85  	//opts = append(opts, genericws.WithSubscriptionTo(eth))
    86  
    87  	handler := NewHandler()
    88  
    89  	opts = append(opts, genericws.WithName("Kraken"))
    90  	ws, err := genericws.NewClient(handler, opts...)
    91  
    92  	assert.Nil(t, err)
    93  	assert.NotNil(t, ws)
    94  
    95  	ctx := context.Background()
    96  
    97  	wsChannel, err := ws.Listen(ctx)
    98  	assert.Nil(t, err)
    99  
   100  	select {
   101  	case <-ctx.Done():
   102  		return
   103  	case msg, ok := <-wsChannel:
   104  		assert.True(t, ok)
   105  
   106  		if msg.OrderBook.Time > 0 {
   107  			if len(msg.OrderBook.Bids) > 0 && len(msg.OrderBook.Asks) > 0 {
   108  				if msg.OrderBook.Asks[0].Price < msg.OrderBook.Bids[0].Price {
   109  					fmt.Println("ob", msg.OrderBook.String())
   110  					t.FailNow()
   111  				}
   112  				// fmt.Println(msg.OrderBook.Asks[0].Price, msg.OrderBook.Bids[0].Price)
   113  			}
   114  			//fmt.Println("ob", msg.OrderBook.String())
   115  		}
   116  
   117  		//if len(msg.Tickers) > 0 && msg.Tickers[0].Time > 0 {
   118  		//	fmt.Println("tick", msg.Tickers[0])
   119  		//}
   120  	}
   121  }