server_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package tick
  2. import "testing"
  3. import "log"
  4. import "net/http"
  5. import "encoding/json"
  6. import "bytes"
  7. import "io/ioutil"
  8. import "errors"
  9. import "io"
  10. import "time"
  11. import "github.com/niniwzw/http2"
  12. import "fmt"
  13. var _ = log.Println
  14. var isssl = true
  15. var client *http.Client
  16. func init() {
  17. if isssl {
  18. client = &http.Client{Transport: tr}
  19. } else {
  20. client = &http.Client{}
  21. }
  22. }
  23. //获取sessionId
  24. func httpreq(name string, req interface{}) ([]byte, error) {
  25. s, err := json.Marshal(req)
  26. if err != nil {
  27. return nil, err
  28. }
  29. var url = "https://localhost:2107/"
  30. if !isssl {
  31. url = "http://localhost:2107/"
  32. }
  33. resp, err := client.Post(url+name, "text/json", bytes.NewBuffer(s))
  34. if err != nil {
  35. return nil, err
  36. }
  37. defer resp.Body.Close()
  38. body, err := ioutil.ReadAll(resp.Body)
  39. if err != nil {
  40. return nil, err
  41. }
  42. log.Println(string(body))
  43. return body, nil
  44. }
  45. func httpreqstream(name string, req interface{}) (io.ReadCloser, error) {
  46. s, err := json.Marshal(req)
  47. if err != nil {
  48. return nil, err
  49. }
  50. var url = "https://localhost:2107/"
  51. if !isssl {
  52. url = "http://localhost:2107/"
  53. }
  54. resp, err := client.Post(url+name, "text/json", bytes.NewBuffer(s))
  55. if err != nil {
  56. return nil, err
  57. }
  58. return resp.Body, err
  59. }
  60. type JsonResponse2 struct {
  61. Result *json.RawMessage `json:"result"`
  62. Err string `json:"err"`
  63. Code int `json:"code"`
  64. }
  65. func decodeResponse(datain []byte, dataout interface{}) (int, error) {
  66. var resp JsonResponse2
  67. err := json.Unmarshal(datain, &resp)
  68. if err != nil {
  69. return -1, err
  70. }
  71. if resp.Err != "" {
  72. return -1, errors.New(resp.Err)
  73. }
  74. if resp.Result == nil {
  75. return 0, nil
  76. }
  77. json.Unmarshal(*resp.Result, &dataout)
  78. return 0, nil
  79. }
  80. func TestTicks(t *testing.T) {
  81. req := &TypeRequest{Type: "sina"}
  82. body, err := httpreq("ticks", req)
  83. if err != nil {
  84. t.Error(err)
  85. }
  86. var ticks []*Market
  87. _, err = decodeResponse(body, &ticks)
  88. if err != nil {
  89. t.Error(err)
  90. }
  91. for i := 0; i < len(ticks); i++ {
  92. ti := ticks[i]
  93. t := time.Unix(ti.Timestamp/1000, 0)
  94. log.Println(ti.Timestamp/1000, t)
  95. }
  96. }
  97. func TestInstruments(t *testing.T) {
  98. req := &InstrumentsRequest{Type: "lmax"}
  99. body, err := httpreq("instruments", req)
  100. if err != nil {
  101. t.Error(err)
  102. }
  103. var inss []Instrument
  104. _, err = decodeResponse(body, &inss)
  105. if err != nil {
  106. t.Error(err)
  107. }
  108. for i, v := range inss {
  109. log.Println(i, v)
  110. }
  111. }
  112. func TestGetIns(t *testing.T) {
  113. req := &InstrumentRequest{Type: "lmax", Id: 4001}
  114. body, err := httpreq("instrument", req)
  115. if err != nil {
  116. t.Error(err)
  117. }
  118. var ins Instrument
  119. _, err = decodeResponse(body, &ins)
  120. if err != nil {
  121. t.Error(err)
  122. }
  123. log.Println(ins)
  124. }
  125. func TestStream(t *testing.T) {
  126. http2.VerboseLogs = true
  127. req := &StreamRequest{Type: "lmax"}
  128. body, err := httpreqstream("stream", req)
  129. if err != nil {
  130. t.Error(err)
  131. return
  132. }
  133. defer body.Close()
  134. decoder := json.NewDecoder(body)
  135. var tick Market
  136. for {
  137. err = decoder.Decode(&tick)
  138. if err != nil {
  139. t.Error(err)
  140. return
  141. }
  142. printDelay(DataTypeName(int(tick.Type)), fmt.Sprint(tick.InsId), tick.Timestamp)
  143. }
  144. }
  145. func TestDownload(t *testing.T) {
  146. start := time.Now().Unix() - 3600*2
  147. req := &DownloadRequest{Type: "lmax", Start: start * 1000, End: 0, Offset: 0, Count: 1000, OrderBy: "time asc"}
  148. body, err := httpreq("download", req)
  149. if err != nil {
  150. t.Error(err)
  151. }
  152. var ticks []*Market
  153. _, err = decodeResponse(body, &ticks)
  154. if err != nil {
  155. t.Error(err)
  156. }
  157. log.Println(ticks)
  158. }
  159. func TestHistory(t *testing.T) {
  160. start := time.Now().Unix() - 10*3600
  161. req := &DownloadRequest{Type: "lmax", Start: start * 1000, End: 0, Offset: 0, Count: 1000, OrderBy: "time asc"}
  162. body, err := httpreq("history", req)
  163. if err != nil {
  164. t.Error(err)
  165. }
  166. var ticks []TickIndex
  167. _, err = decodeResponse(body, &ticks)
  168. if err != nil {
  169. t.Error(err)
  170. }
  171. log.Println(ticks)
  172. }