123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- package lmaxapi
- import (
- "fmt"
- "tickserver/api/lmaxapi/request"
- "tickserver/api/lmaxapi/response"
- "testing"
- "time"
- )
- func createSession() *Session {
- SetBaseUrl("https://testapi.lmaxtrader.com")
- req := request.NewLoginRequest("niniwzw", "Password123", request.ProductType.CFD_DEMO)
- session, err := Login(req)
- if err != nil {
- panic("login error:" + err.Error())
- }
- session.RegisterStreamFailureEvent(func(s *Session, err error) {
- fmt.Println("stream Fail:", err.Error())
- })
- return session
- }
- func sessionStart(s *Session) chan int {
- c := make(chan int, 1)
- go func() {
- s.Start()
- c <- 1
- }()
- return c
- }
- func heartbeat(session *Session) {
- req2 := request.NewHeartbeatSubscriptionRequest()
- session.Subscribe(req2, nil)
- req3 := request.NewHeartbeatRequest("hh")
- go func() {
- for {
- session.RequestHeartbeat(req3, nil)
- time.Sleep(1 * time.Second)
- }
- }()
- }
- func wait(c chan int, d time.Duration, t *testing.T, m string) {
- select {
- case <-c:
- case <-time.After(d):
- t.Errorf(m + " Error timeout")
- }
- }
- func TestLogin(t *testing.T) {
- SetBaseUrl("https://testapi.lmaxtrader.com")
- req := request.NewLoginRequest("niniwzw", "Password123", request.ProductType.CFD_DEMO)
- session, err := Login(req)
- if err != nil || session.sessionId == "" {
- t.Errorf("Login Error")
- }
- }
- func TestLoginFail(t *testing.T) {
- SetBaseUrl("https://testapi.lmaxtrader.com")
- req := request.NewLoginRequest("niniwzw", "errorPass", request.ProductType.CFD_DEMO)
- _, err := Login(req)
- if err == nil { //|| session != nil{
- t.Errorf("Fail Login Error")
- return
- }
- }
- func TestLogout(t *testing.T) {
- SetBaseUrl("https://testapi.lmaxtrader.com")
- req := request.NewLoginRequest("niniwzw", "Password123", request.ProductType.CFD_DEMO)
- session, err := Login(req)
- if err != nil {
- t.Errorf("Fail Login:", err.Error())
- return
- }
- err = session.Logout(nil)
- if err != nil {
- t.Errorf("Fail Logout:", err.Error())
- return
- }
- }
- func TestSessionSubscribe(t *testing.T) {
- session := createSession()
- subReq := request.NewOrderBookSubscriptionRequest(4001)
- err := session.Subscribe(subReq, nil)
- if err != nil {
- t.Errorf(err.Error())
- }
- c := make(chan int64, 1)
- session.RegisterOrderBookEvent(func(s *Session, event *response.OrderBookEvent) {
- fmt.Println(event)
- c <- event.InstrumentId
- })
- go session.Start()
- select {
- case m := <-c:
- if m != 4001 {
- t.Errorf("Session subscript InstrumentId Error:", m)
- }
- case <-time.After(10 * time.Second):
- t.Errorf("Session subscript timeout")
- }
- session.Stop()
- }
- func TestSessionAsynCall(t *testing.T) {
- session := createSession()
- c := make(chan int, 1)
- session.Logout(func(err error) {
- c <- 1
- if err != nil {
- t.Errorf("AsynCall Error:", err)
- }
- })
- wait(c, 10*time.Second, t, "AsynCallError")
- }
- func TestSessionSearchInstrument(t *testing.T) {
- req := request.NewSearchInstrumentRequest("", 0)
- session := createSession()
- insList, hasMore, err := session.SearchInstruments(req, nil)
- fmt.Println(insList)
- if err != nil {
- t.Errorf("Search Error", err)
- }
- if insList[0].Id != 4001 {
- t.Errorf("Search Id Error")
- }
- if hasMore != true {
- t.Errorf("Search hasMore Error")
- }
- }
- func TestLoadAllOrders(t *testing.T) {
- req := request.NewCompletedOrderRequest(50, "")
- session := createSession()
- for {
- orderList, offset, err := session.GetCompletedOrder(req, nil)
- if err != nil {
- t.Errorf("Search Error", err)
- }
- req.Offset = offset
- fmt.Println(len(orderList))
- if offset == "" {
- break
- }
- }
- }
- func TestLoadAllAccountStatements(t *testing.T) {
- req := request.NewAccountStatementRequest(200, 0)
- session := createSession()
- for {
- orderList, hasmore, err := session.GetAccountStatement(req, nil)
- if err != nil {
- t.Errorf("Search Error", err)
- }
- req.Offset += int32(len(orderList))
- fmt.Println(len(orderList))
- if hasmore == false {
- break
- }
- }
- }
- func TestLoadAllOrderTransactions(t *testing.T) {
- req := request.NewOrderTransactionRequest("AAIm0gAAAAAOvf5c")
- session := createSession()
- for {
- orderList, hasmore, err := session.GetOrderTransaction(req, nil)
- if err != nil {
- t.Errorf("Search Error", err)
- }
- fmt.Println(orderList[0].Data[0], orderList[0].Data[1])
- if hasmore == false {
- break
- }
- }
- }
- func TestLoadAllActivitys(t *testing.T) {
- req := request.NewActivityRequest(50, 0)
- session := createSession()
- for {
- orderList, hasmore, err := session.GetActivity(req, nil)
- if err != nil {
- t.Errorf("Search Error", err)
- }
- req.Offset += int32(len(orderList))
- fmt.Println(len(orderList))
- if hasmore == false {
- break
- }
- }
- }
- func TestLoadAllActivitysOneOrder(t *testing.T) {
- req := request.NewOrderActivityAuditTrailRequest("AAIm0gAAAAAOvf5c", 50, 0)
- session := createSession()
- for {
- orderList, hasmore, err := session.GetActivity(req, nil)
- if err != nil {
- t.Errorf("Search Error", err)
- }
- req.Offset += int32(len(orderList))
- fmt.Println(len(orderList))
- if hasmore == false {
- break
- }
- }
- }
- func TestSessionHistoric(t *testing.T) {
- session := createSession()
- req := request.NewHistoricSubscriptionRequest()
- err := session.Subscribe(req, nil)
- if err != nil {
- t.Errorf("Historic Error:", err.Error())
- }
- req1 := request.NewTopOfBookHistoricRequest("1", 4001, time.Date(2011, 5, 11, 0, 0, 0, 0, time.UTC), time.Date(2011, 6, 13, 0, 0, 0, 0, time.UTC), "CSV")
- err = session.RequestHistoricMarketData(req1, nil)
- if err != nil {
- t.Errorf("Historic Error:", err.Error())
- }
- session.RegisterHistoricMarketDataEvent(func(s *Session, event *response.HistoricMarketDataEvent) {
- fmt.Println("Receive HistoricMarket")
- session.Stop()
- })
- heartbeat(session)
- wait(sessionStart(session), 20*time.Second, t, "ExecutionSubscribe")
- }
- func TestSessionAccountSubscribe(t *testing.T) {
- session := createSession()
- req := request.NewAccountSubscriptionRequest()
- err := session.Subscribe(req, nil)
- session.RegisterAccountStateEvent(func(s *Session, event *response.AccountStateEvent) {
- fmt.Println("Receive AccountStateEvent")
- session.Stop()
- })
- if err != nil {
- t.Errorf("AccountSubscribe Error:", err.Error())
- }
- heartbeat(session)
- c := sessionStart(session)
- wait(c, 20*time.Second, t, "AccountSubscribe")
- }
- func TestSessionHeartbeat(t *testing.T) {
- session := createSession()
- subReq1 := request.NewHeartbeatSubscriptionRequest()
- err := session.Subscribe(subReq1, nil)
- if err != nil {
- t.Errorf("Heartbeat sub Error %s", err.Error())
- }
- session.RegisterHeartbeatEvent(func(s *Session, id int64, tk string) {
- fmt.Println("Receive heartbeat", id, tk)
- if tk != "token123" {
- t.Errorf("Heartbeat token Error %s", tk)
- }
- session.Stop()
- })
- go func() {
- for {
- req := request.NewHeartbeatRequest("token123")
- data, err := session.RequestHeartbeat(req, nil)
- fmt.Println(data)
- if err != nil {
- t.Errorf("Heartbeat Error: %s", err.Error())
- }
- if data != "token123" {
- t.Errorf("Heartbear token Error: %s", data)
- }
- time.Sleep(2 * time.Second)
- }
- }()
- c := make(chan int, 1)
- go func() {
- session.Start()
- c <- 1
- }()
- wait(c, 10*time.Second, t, "Error Heartbeat")
- }
- func TestSessionOrderBookStatus(t *testing.T) {
- session := createSession()
- subReq := request.NewOrderBookStatusRequest(4001)
- session.Subscribe(subReq, nil)
- session.RegisterOrderBookStatusEvent(func(s *Session, event *response.OrderBookStatusEvent) {
- fmt.Println("Receive OrderBookStatusEvent:", event.InstrumentId, event.OrderBookStatus)
- session.Stop()
- })
- heartbeat(session)
- wait(sessionStart(session), 20*time.Second, t, "PositionSubscribe")
- }
- func TestSessionEventHandler(t *testing.T) {
- eventData := "<events><header><seq>1</seq></header><body><ob2>4002|13f3373669f|10@1.32651;150@1.32649;100@1.32648;100@1.32647;300@1.32646|100@1.32653;10@1.32654;400@1.32655;200@1.32656;3@1.3266|1.32558;13f2fe36880|1.32996|1.32162|1.32651|1.32653|1.32657</ob2></body></events>"
- session := NewSession("", "", &response.AccountDetails{})
- handler := NewEventHandler(session)
- c := make(chan int64, 1)
- handler.OnOrderBookEvent = func(s *Session, event *response.OrderBookEvent) {
- fmt.Println(event.InstrumentId)
- c <- event.InstrumentId
- }
- handler.HandleEventData(eventData, false)
- select {
- case m := <-c:
- if m != 4002 {
- t.Errorf("OrderEvent Error", m)
- }
- case <-time.After(10 * time.Second):
- t.Errorf("OrderEvent timeout")
- }
- }
|