client_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. package client
  3. // 本文件测试client的封装接口
  4. import (
  5. "log"
  6. "testing"
  7. "time"
  8. )
  9. var client *Client
  10. func newClient(t *testing.T) {
  11. newServer(t)
  12. if client == nil {
  13. c, err := NewClient("localhost:19526", "localhost:19527", "localhost:8080", "./testdata")
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. client = c
  18. }
  19. }
  20. func TestClientGetHistory(t *testing.T) {
  21. newClient(t)
  22. time.Sleep(time.Minute * 2) // 让server运行一段时间, 拥有测试的数据
  23. insIds := []string{"easyforex_EURUSD", "oanda_EURUSD"} // lmax, easyforex and oanda EURUSD
  24. for _, insId := range insIds {
  25. ticks, err := client.GetTickHistory(insId, 5, TimeNow)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if len(ticks) != 5 {
  30. t.Error("len(ticks) != 5", len(ticks))
  31. }
  32. }
  33. for _, insId := range insIds {
  34. candles, err := client.GetCandleHistory(insId, M1, 2, TimeNow)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. if len(candles) != 2 {
  39. t.Error("len(candles) != 2", len(candles))
  40. }
  41. }
  42. }
  43. func TestClientSubMarket(t *testing.T) {
  44. newClient(t)
  45. insIds := []string{"easyforex_EURUSD", "oanda_EURUSD"} // lmax, easyforex and oanda EURUSD
  46. for _, insId := range insIds {
  47. insId := insId
  48. client.SubMarket(insId)
  49. client.GetIns(insId).OnMarket().Attach(func(v interface{}) error {
  50. m, ok := v.(*Market)
  51. if !ok {
  52. t.Fatal("v.(*Market) is NOT ok")
  53. }
  54. if m.InsId != insId {
  55. t.Fatal("m.InsId != insId", m.InsId, insId)
  56. }
  57. log.Println("@@@:Market:", insId, m)
  58. return nil
  59. })
  60. }
  61. time.Sleep(time.Minute)
  62. }