lmax_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package lmaxapi
  2. import (
  3. "fmt"
  4. "tickserver/api/lmaxapi/request"
  5. "tickserver/api/lmaxapi/response"
  6. "testing"
  7. "time"
  8. )
  9. func createSession() *Session {
  10. SetBaseUrl("https://testapi.lmaxtrader.com")
  11. req := request.NewLoginRequest("niniwzw", "Password123", request.ProductType.CFD_DEMO)
  12. session, err := Login(req)
  13. if err != nil {
  14. panic("login error:" + err.Error())
  15. }
  16. session.RegisterStreamFailureEvent(func(s *Session, err error) {
  17. fmt.Println("stream Fail:", err.Error())
  18. })
  19. return session
  20. }
  21. func sessionStart(s *Session) chan int {
  22. c := make(chan int, 1)
  23. go func() {
  24. s.Start()
  25. c <- 1
  26. }()
  27. return c
  28. }
  29. func heartbeat(session *Session) {
  30. req2 := request.NewHeartbeatSubscriptionRequest()
  31. session.Subscribe(req2, nil)
  32. req3 := request.NewHeartbeatRequest("hh")
  33. go func() {
  34. for {
  35. session.RequestHeartbeat(req3, nil)
  36. time.Sleep(1 * time.Second)
  37. }
  38. }()
  39. }
  40. func wait(c chan int, d time.Duration, t *testing.T, m string) {
  41. select {
  42. case <-c:
  43. case <-time.After(d):
  44. t.Errorf(m + " Error timeout")
  45. }
  46. }
  47. func TestLogin(t *testing.T) {
  48. SetBaseUrl("https://testapi.lmaxtrader.com")
  49. req := request.NewLoginRequest("niniwzw", "Password123", request.ProductType.CFD_DEMO)
  50. session, err := Login(req)
  51. if err != nil || session.sessionId == "" {
  52. t.Errorf("Login Error")
  53. }
  54. }
  55. func TestLoginFail(t *testing.T) {
  56. SetBaseUrl("https://testapi.lmaxtrader.com")
  57. req := request.NewLoginRequest("niniwzw", "errorPass", request.ProductType.CFD_DEMO)
  58. _, err := Login(req)
  59. if err == nil { //|| session != nil{
  60. t.Errorf("Fail Login Error")
  61. return
  62. }
  63. }
  64. func TestLogout(t *testing.T) {
  65. SetBaseUrl("https://testapi.lmaxtrader.com")
  66. req := request.NewLoginRequest("niniwzw", "Password123", request.ProductType.CFD_DEMO)
  67. session, err := Login(req)
  68. if err != nil {
  69. t.Errorf("Fail Login:", err.Error())
  70. return
  71. }
  72. err = session.Logout(nil)
  73. if err != nil {
  74. t.Errorf("Fail Logout:", err.Error())
  75. return
  76. }
  77. }
  78. func TestSessionSubscribe(t *testing.T) {
  79. session := createSession()
  80. subReq := request.NewOrderBookSubscriptionRequest(4001)
  81. err := session.Subscribe(subReq, nil)
  82. if err != nil {
  83. t.Errorf(err.Error())
  84. }
  85. c := make(chan int64, 1)
  86. session.RegisterOrderBookEvent(func(s *Session, event *response.OrderBookEvent) {
  87. fmt.Println(event)
  88. c <- event.InstrumentId
  89. })
  90. go session.Start()
  91. select {
  92. case m := <-c:
  93. if m != 4001 {
  94. t.Errorf("Session subscript InstrumentId Error:", m)
  95. }
  96. case <-time.After(10 * time.Second):
  97. t.Errorf("Session subscript timeout")
  98. }
  99. session.Stop()
  100. }
  101. func TestSessionAsynCall(t *testing.T) {
  102. session := createSession()
  103. c := make(chan int, 1)
  104. session.Logout(func(err error) {
  105. c <- 1
  106. if err != nil {
  107. t.Errorf("AsynCall Error:", err)
  108. }
  109. })
  110. wait(c, 10*time.Second, t, "AsynCallError")
  111. }
  112. func TestSessionSearchInstrument(t *testing.T) {
  113. req := request.NewSearchInstrumentRequest("", 0)
  114. session := createSession()
  115. insList, hasMore, err := session.SearchInstruments(req, nil)
  116. fmt.Println(insList)
  117. if err != nil {
  118. t.Errorf("Search Error", err)
  119. }
  120. if insList[0].Id != 4001 {
  121. t.Errorf("Search Id Error")
  122. }
  123. if hasMore != true {
  124. t.Errorf("Search hasMore Error")
  125. }
  126. }
  127. func TestLoadAllOrders(t *testing.T) {
  128. req := request.NewCompletedOrderRequest(50, "")
  129. session := createSession()
  130. for {
  131. orderList, offset, err := session.GetCompletedOrder(req, nil)
  132. if err != nil {
  133. t.Errorf("Search Error", err)
  134. }
  135. req.Offset = offset
  136. fmt.Println(len(orderList))
  137. if offset == "" {
  138. break
  139. }
  140. }
  141. }
  142. func TestLoadAllAccountStatements(t *testing.T) {
  143. req := request.NewAccountStatementRequest(200, 0)
  144. session := createSession()
  145. for {
  146. orderList, hasmore, err := session.GetAccountStatement(req, nil)
  147. if err != nil {
  148. t.Errorf("Search Error", err)
  149. }
  150. req.Offset += int32(len(orderList))
  151. fmt.Println(len(orderList))
  152. if hasmore == false {
  153. break
  154. }
  155. }
  156. }
  157. func TestLoadAllOrderTransactions(t *testing.T) {
  158. req := request.NewOrderTransactionRequest("AAIm0gAAAAAOvf5c")
  159. session := createSession()
  160. for {
  161. orderList, hasmore, err := session.GetOrderTransaction(req, nil)
  162. if err != nil {
  163. t.Errorf("Search Error", err)
  164. }
  165. fmt.Println(orderList[0].Data[0], orderList[0].Data[1])
  166. if hasmore == false {
  167. break
  168. }
  169. }
  170. }
  171. func TestLoadAllActivitys(t *testing.T) {
  172. req := request.NewActivityRequest(50, 0)
  173. session := createSession()
  174. for {
  175. orderList, hasmore, err := session.GetActivity(req, nil)
  176. if err != nil {
  177. t.Errorf("Search Error", err)
  178. }
  179. req.Offset += int32(len(orderList))
  180. fmt.Println(len(orderList))
  181. if hasmore == false {
  182. break
  183. }
  184. }
  185. }
  186. func TestLoadAllActivitysOneOrder(t *testing.T) {
  187. req := request.NewOrderActivityAuditTrailRequest("AAIm0gAAAAAOvf5c", 50, 0)
  188. session := createSession()
  189. for {
  190. orderList, hasmore, err := session.GetActivity(req, nil)
  191. if err != nil {
  192. t.Errorf("Search Error", err)
  193. }
  194. req.Offset += int32(len(orderList))
  195. fmt.Println(len(orderList))
  196. if hasmore == false {
  197. break
  198. }
  199. }
  200. }
  201. func TestSessionHistoric(t *testing.T) {
  202. session := createSession()
  203. req := request.NewHistoricSubscriptionRequest()
  204. err := session.Subscribe(req, nil)
  205. if err != nil {
  206. t.Errorf("Historic Error:", err.Error())
  207. }
  208. 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")
  209. err = session.RequestHistoricMarketData(req1, nil)
  210. if err != nil {
  211. t.Errorf("Historic Error:", err.Error())
  212. }
  213. session.RegisterHistoricMarketDataEvent(func(s *Session, event *response.HistoricMarketDataEvent) {
  214. fmt.Println("Receive HistoricMarket")
  215. session.Stop()
  216. })
  217. heartbeat(session)
  218. wait(sessionStart(session), 20*time.Second, t, "ExecutionSubscribe")
  219. }
  220. func TestSessionAccountSubscribe(t *testing.T) {
  221. session := createSession()
  222. req := request.NewAccountSubscriptionRequest()
  223. err := session.Subscribe(req, nil)
  224. session.RegisterAccountStateEvent(func(s *Session, event *response.AccountStateEvent) {
  225. fmt.Println("Receive AccountStateEvent")
  226. session.Stop()
  227. })
  228. if err != nil {
  229. t.Errorf("AccountSubscribe Error:", err.Error())
  230. }
  231. heartbeat(session)
  232. c := sessionStart(session)
  233. wait(c, 20*time.Second, t, "AccountSubscribe")
  234. }
  235. func TestSessionHeartbeat(t *testing.T) {
  236. session := createSession()
  237. subReq1 := request.NewHeartbeatSubscriptionRequest()
  238. err := session.Subscribe(subReq1, nil)
  239. if err != nil {
  240. t.Errorf("Heartbeat sub Error %s", err.Error())
  241. }
  242. session.RegisterHeartbeatEvent(func(s *Session, id int64, tk string) {
  243. fmt.Println("Receive heartbeat", id, tk)
  244. if tk != "token123" {
  245. t.Errorf("Heartbeat token Error %s", tk)
  246. }
  247. session.Stop()
  248. })
  249. go func() {
  250. for {
  251. req := request.NewHeartbeatRequest("token123")
  252. data, err := session.RequestHeartbeat(req, nil)
  253. fmt.Println(data)
  254. if err != nil {
  255. t.Errorf("Heartbeat Error: %s", err.Error())
  256. }
  257. if data != "token123" {
  258. t.Errorf("Heartbear token Error: %s", data)
  259. }
  260. time.Sleep(2 * time.Second)
  261. }
  262. }()
  263. c := make(chan int, 1)
  264. go func() {
  265. session.Start()
  266. c <- 1
  267. }()
  268. wait(c, 10*time.Second, t, "Error Heartbeat")
  269. }
  270. func TestSessionOrderBookStatus(t *testing.T) {
  271. session := createSession()
  272. subReq := request.NewOrderBookStatusRequest(4001)
  273. session.Subscribe(subReq, nil)
  274. session.RegisterOrderBookStatusEvent(func(s *Session, event *response.OrderBookStatusEvent) {
  275. fmt.Println("Receive OrderBookStatusEvent:", event.InstrumentId, event.OrderBookStatus)
  276. session.Stop()
  277. })
  278. heartbeat(session)
  279. wait(sessionStart(session), 20*time.Second, t, "PositionSubscribe")
  280. }
  281. func TestSessionEventHandler(t *testing.T) {
  282. 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>"
  283. session := NewSession("", "", &response.AccountDetails{})
  284. handler := NewEventHandler(session)
  285. c := make(chan int64, 1)
  286. handler.OnOrderBookEvent = func(s *Session, event *response.OrderBookEvent) {
  287. fmt.Println(event.InstrumentId)
  288. c <- event.InstrumentId
  289. }
  290. handler.HandleEventData(eventData, false)
  291. select {
  292. case m := <-c:
  293. if m != 4002 {
  294. t.Errorf("OrderEvent Error", m)
  295. }
  296. case <-time.After(10 * time.Second):
  297. t.Errorf("OrderEvent timeout")
  298. }
  299. }