newserver_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package lmaxapi
  2. import (
  3. "tickserver/api/lmaxapi/request"
  4. "log"
  5. "testing"
  6. "time"
  7. )
  8. func TestNewRegister(t *testing.T) {
  9. req := &request.RegisterRequest{}
  10. req.UserName = "niniwzw"
  11. req.Password = "password123"
  12. req.AccountType = request.AccountTypeStandard
  13. req.ProductType = request.ProductType.CFD_DEMO
  14. req.Currency = request.CurrencyUSD
  15. req.Email = "niniwzw@qq.com"
  16. req.Phone = "12345678"
  17. req.Title = "Mr."
  18. req.FirstName = "king"
  19. req.Surname = "wang"
  20. SetBaseUrl("http://localhost:8086")
  21. err := Register(req)
  22. if err == nil {
  23. t.Error(err)
  24. return
  25. }
  26. }
  27. func login(t *testing.T) *Session {
  28. SetBaseUrl("http://localhost:8086")
  29. enableLog("d:/lmaxapi.log")
  30. req := request.NewLoginRequest("niniwzw", "password123", request.ProductType.CFD_DEMO)
  31. session, err := Login(req)
  32. if err != nil {
  33. t.Error(err)
  34. return nil
  35. }
  36. if session.accountDetails.Username != "niniwzw" {
  37. t.Error("account detail error.")
  38. }
  39. session.RegisterStreamFailureEvent(func(session *Session, err error) {
  40. session.Stop()
  41. })
  42. return session
  43. }
  44. func TestNewLogin(t *testing.T) {
  45. login(t)
  46. }
  47. func TestNewSubscribe(t *testing.T) {
  48. session := login(t)
  49. subReq1 := request.NewHeartbeatSubscriptionRequest()
  50. err := session.Subscribe(subReq1, nil)
  51. if err != nil {
  52. t.Error(err)
  53. return
  54. }
  55. }
  56. func BenchmarkOrderMarket(b *testing.B) {
  57. SetBaseUrl("http://localhost:8086")
  58. enableLog("d:/lmaxapi.log")
  59. req := request.NewLoginRequest("niniwzw", "password123", request.ProductType.CFD_DEMO)
  60. session, err := Login(req)
  61. if err != nil {
  62. b.Error(err)
  63. return
  64. }
  65. if session.accountDetails.Username != "niniwzw" {
  66. b.Error("account detail error.")
  67. return
  68. }
  69. session.RegisterStreamFailureEvent(func(session *Session, err error) {
  70. session.Stop()
  71. })
  72. go session.Start()
  73. b.ResetTimer()
  74. for i := 0; i < b.N; i++ {
  75. reqid := getId() + "_10000000"
  76. req := request.NewMarketOrderRequest(4001, reqid, request.TimeInForce.IMMEDIATE_OR_CANCEL, 1, 0, 0)
  77. id, err := session.PlaceMarketOrder(req, nil)
  78. if err != nil {
  79. b.Errorf("MarkerOrder Error:", id, err)
  80. return
  81. }
  82. }
  83. }
  84. func TestNewOrderMarket(t *testing.T) {
  85. session := login(t)
  86. instructionId := make(chan string, 1)
  87. subOrderEvent(session, t, instructionId, "Market")
  88. go session.Start()
  89. reqid := getId() + "_10000000"
  90. req := request.NewMarketOrderRequest(4002, reqid, request.TimeInForce.IMMEDIATE_OR_CANCEL, 1, 0, 0)
  91. id, err := session.PlaceMarketOrder(req, nil)
  92. if err != nil {
  93. t.Errorf("MarkerOrder Error:", err)
  94. return
  95. }
  96. if id != reqid {
  97. t.Errorf("MarketOrder instructionId Error:", id)
  98. return
  99. }
  100. getMyId(instructionId, reqid)
  101. //修改订单
  102. err = amendOrder(session, t, reqid, instructionId)
  103. if err != nil {
  104. t.Error("MarkerOrder, amendOrder", err)
  105. return
  106. }
  107. //平仓订单
  108. closedId := getId()
  109. creq := request.NewClosingOrderRequest(4001, reqid, closedId, -1)
  110. id, err = session.PlaceClosingOrder(creq, nil)
  111. if err != nil {
  112. t.Errorf("MarkerOrder ClosingOrder Error:", err.Error())
  113. }
  114. getMyId(instructionId, closedId)
  115. }
  116. //批量发送订单。保证按照顺序进入交易所,特殊权限用户使用
  117. func TestNewOrderActions(t *testing.T) {
  118. session := login(t)
  119. go session.Start()
  120. req := NewOrderActionsRequest()
  121. reqid := getId()
  122. order1 := request.NewMarketOrderRequest(4001, reqid, request.TimeInForce.IMMEDIATE_OR_CANCEL, 1, 0, 0)
  123. req.Add(order1)
  124. reqid = getId()
  125. order2 := request.NewMarketOrderRequest(4001, reqid, request.TimeInForce.IMMEDIATE_OR_CANCEL, 1, 0, 0)
  126. req.Add(order2)
  127. reqid = getId()
  128. creq := request.NewCancelOrderRequest(4001, reqid, "cancel")
  129. req.Add(creq)
  130. reqid = getId()
  131. areq := request.NewAmendStopsOrderRequest(4001, reqid, "amend", 50.0, 50.0)
  132. req.Add(areq)
  133. reqid = getId()
  134. closedreq := request.NewClosingOrderRequest(4001, reqid, "closed", 0.0)
  135. req.Add(closedreq)
  136. id, err := session.OrderActions(req, nil)
  137. log.Println("id = ", id)
  138. if err != nil {
  139. t.Errorf("send action: ", err.Error())
  140. }
  141. }
  142. func TestNewOrderLimit(t *testing.T) {
  143. session := login(t)
  144. instructionId := make(chan string, 1)
  145. subOrderEvent(session, t, instructionId, "Limit")
  146. go session.Start()
  147. reqId := getId()
  148. req := request.NewLimitOrderRequest(4001, reqId, request.TimeInForce.GOOD_FOR_DAY, 1.34, 1, 0, 0)
  149. id, err := session.PlaceLimitOrder(req, nil)
  150. if err != nil {
  151. t.Errorf("TestLimitOrder Error:", err)
  152. return
  153. }
  154. if id != reqId {
  155. t.Errorf("TestLimitOrder instructionId Error:", id)
  156. return
  157. }
  158. getMyId(instructionId, reqId)
  159. //修改订单
  160. err = amendOrder(session, t, reqId, instructionId)
  161. if err != nil {
  162. t.Error("TestLimitOrder, amendOrder", err)
  163. return
  164. }
  165. //撤销订单
  166. cancelId := getId()
  167. creq := request.NewCancelOrderRequest(4001, reqId, cancelId)
  168. _, err = session.CancelOrder(creq, nil)
  169. if err != nil {
  170. t.Errorf("TestLimitOrder CancelOrder Error:", err.Error())
  171. return
  172. }
  173. getMyId(instructionId, cancelId)
  174. }
  175. func TestNewOrderCancel(t *testing.T) {
  176. session := login(t)
  177. instructionId := make(chan string, 1)
  178. subOrderEvent(session, t, instructionId, "Limit")
  179. go session.Start()
  180. reqId := getId()
  181. req := request.NewLimitOrderRequest(2001, reqId, request.TimeInForce.GOOD_FOR_DAY, 1.34, 1, 0, 0)
  182. id, err := session.PlaceLimitOrder(req, nil)
  183. if err != nil {
  184. t.Errorf("TestNewOrderCancel Error:", err)
  185. return
  186. }
  187. if id != reqId {
  188. t.Errorf("TestNewOrderCancel instructionId Error:", id)
  189. return
  190. }
  191. getMyId(instructionId, reqId)
  192. //撤销订单
  193. cancelId := getId()
  194. creq := request.NewCancelOrderRequest(2001, reqId, cancelId)
  195. _, err = session.CancelOrder(creq, nil)
  196. if err != nil {
  197. t.Errorf("TestNewOrderCancel CancelOrder Error:", err.Error())
  198. return
  199. }
  200. getMyId(instructionId, cancelId)
  201. }
  202. func TestNewOrderPair(t *testing.T) {
  203. session := login(t)
  204. instructionId := make(chan string, 1)
  205. subOrderEvent(session, t, instructionId, "Limit")
  206. go session.Start()
  207. done := make(chan int)
  208. go func() {
  209. reqId := getId()
  210. req := request.NewLimitOrderRequest(2001, reqId, request.TimeInForce.GOOD_FOR_DAY, 1.34, 1, 0, 0)
  211. id, err := session.PlaceLimitOrder(req, nil)
  212. if err != nil {
  213. t.Errorf("TestNewOrderCancel Error:", err)
  214. return
  215. }
  216. if id != reqId {
  217. t.Errorf("TestNewOrderCancel instructionId Error:", id)
  218. return
  219. }
  220. getMyId(instructionId, reqId)
  221. done <- 1
  222. }()
  223. reqId := getId()
  224. req := request.NewLimitOrderRequest(2001, reqId, request.TimeInForce.GOOD_FOR_DAY, 1.34, -1, 0, 0)
  225. id, err := session.PlaceLimitOrder(req, nil)
  226. if err != nil {
  227. t.Errorf("TestNewOrderCancel Error:", err)
  228. return
  229. }
  230. if id != reqId {
  231. t.Errorf("TestNewOrderCancel instructionId Error:", id)
  232. return
  233. }
  234. getMyId(instructionId, reqId)
  235. <-done
  236. }
  237. func TestNewHeartBeat(t *testing.T) {
  238. w, err := enableLog("d:\\lmaxapi.proto")
  239. if err != nil {
  240. t.Error(err)
  241. return
  242. }
  243. defer w.Close()
  244. session := login(t)
  245. subReq1 := request.NewHeartbeatSubscriptionRequest()
  246. err = session.Subscribe(subReq1, nil)
  247. if err != nil {
  248. t.Errorf("Heartbeat sub Error %s", err.Error())
  249. }
  250. c := make(chan int, 1)
  251. session.RegisterHeartbeatEvent(func(s *Session, id int64, tk string) {
  252. log.Println("Receive heartbeat", id, tk)
  253. if tk != "hello" {
  254. t.Errorf("Heartbeat token Error %s", tk)
  255. }
  256. session.Stop()
  257. c <- 1
  258. })
  259. session.HeartbeatTimeout(time.Second * 1)
  260. go session.Start()
  261. wait(c, 10*time.Second, t, "Error Heartbeat")
  262. }
  263. func TestNewSubscribeNotLogin(t *testing.T) {
  264. session := NewSession("http://localhost:8086", "", nil)
  265. subReq1 := request.NewHeartbeatSubscriptionRequest()
  266. err := session.Subscribe(subReq1, nil)
  267. if err == nil {
  268. t.Error("TestNewSubscribeNotLogin error")
  269. return
  270. }
  271. }
  272. func TestNewSearchInstrument2(t *testing.T) {
  273. req := request.NewSearchInstrumentRequest("", 0)
  274. session := login(t)
  275. insList, hasMore, err := session.SearchInstruments(req, nil)
  276. if err != nil {
  277. t.Errorf("Search Error", err)
  278. }
  279. log.Printf("Search insList len Error %d\n", len(insList))
  280. if hasMore != false {
  281. t.Errorf("Search hasMore Error")
  282. }
  283. }