getTid_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package http
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. . "github.com/smartystreets/goconvey/convey"
  7. "go-common/app/service/live/wallet/model"
  8. "go-common/library/ecode"
  9. "net/url"
  10. "sync"
  11. "testing"
  12. "time"
  13. )
  14. type TidRes struct {
  15. Code int `json:"code"`
  16. Resp *model.TidResp `json:"data"`
  17. }
  18. type TestTidParams struct {
  19. Biz string
  20. Time int64
  21. }
  22. func getTestTidParams(biz string) *TestTidParams {
  23. return &TestTidParams{
  24. Biz: biz,
  25. Time: time.Now().Unix(),
  26. }
  27. }
  28. func getTestRandServiceType() int32 {
  29. return r.Int31n(4)
  30. }
  31. func testWith(f func()) func() {
  32. once.Do(startHTTP)
  33. return f
  34. }
  35. func getTestParamsJson() string {
  36. params := getTestTidParams("gift")
  37. paramsBytes, _ := json.Marshal(params)
  38. paramsJson := string(paramsBytes[:])
  39. return paramsJson
  40. }
  41. func queryGetTid(t *testing.T, serviceType int32, tidParams string) *TidRes {
  42. params := url.Values{}
  43. params.Set("type", fmt.Sprintf("%d", serviceType))
  44. params.Set("params", tidParams)
  45. req, _ := client.NewRequest("POST", _getTidURL, "127.0.0.1", params)
  46. var res TidRes
  47. err := client.Do(context.TODO(), req, &res)
  48. if err != nil {
  49. t.Errorf("client.Do() error(%v)", err)
  50. t.FailNow()
  51. }
  52. return &res
  53. }
  54. func TestGetTid(t *testing.T) {
  55. Convey("normal", t, testWith(func() {
  56. serviceType := getTestRandServiceType()
  57. paramsJson := getTestParamsJson()
  58. res := queryGetTid(t, serviceType, paramsJson)
  59. So(res.Code, ShouldEqual, 0)
  60. So(res.Resp.TransactionId, ShouldNotEqual, "")
  61. }))
  62. Convey("Twice Same params 同样的参数调用getTid 得到的tid应该不一样", t, testWith(func() {
  63. serviceType := getTestRandServiceType()
  64. paramsJson := getTestParamsJson()
  65. res := queryGetTid(t, serviceType, paramsJson)
  66. So(res.Code, ShouldEqual, 0)
  67. So(res.Resp.TransactionId, ShouldNotEqual, "")
  68. res1 := queryGetTid(t, serviceType, paramsJson)
  69. So(res1.Code, ShouldEqual, 0)
  70. So(res1.Resp.TransactionId, ShouldNotEqual, res.Resp.TransactionId)
  71. }))
  72. Convey("Test multi Same params", t, testWith(func() {
  73. serviceType := getTestRandServiceType()
  74. paramsJson := getTestParamsJson()
  75. resMap := make(map[int]*TidRes)
  76. wg := sync.WaitGroup{}
  77. var mutex sync.Mutex
  78. times := 10
  79. for i := 0; i < times; i++ {
  80. wg.Add(1)
  81. go func(i int) {
  82. mutex.Lock()
  83. resMap[i] = queryGetTid(t, serviceType, paramsJson)
  84. mutex.Unlock()
  85. wg.Done()
  86. }(i)
  87. }
  88. wg.Wait()
  89. tidMap := map[string]bool{}
  90. for _, res := range resMap {
  91. So(res.Code, ShouldEqual, 0)
  92. So(res.Resp.TransactionId, ShouldNotEqual, "")
  93. _, ok := tidMap[res.Resp.TransactionId]
  94. So(ok, ShouldEqual, false) // 应该找不到 因为同样的参数应该生成不同的tid
  95. tidMap[res.Resp.TransactionId] = true
  96. }
  97. }))
  98. Convey("params error", t, func() {
  99. Convey("serviceType 应该为 0 1 2 3 ", testWith(func() {
  100. wrongServiceTypes := []int32{-1, 5}
  101. for _, v := range wrongServiceTypes {
  102. paramsJson := getTestParamsJson()
  103. res := queryGetTid(t, v, paramsJson)
  104. So(res.Code, ShouldEqual, ecode.RequestErr.Code())
  105. }
  106. }))
  107. Convey("param不为空", testWith(func() {
  108. st := getTestRandServiceType()
  109. params := ""
  110. res := queryGetTid(t, st, params)
  111. So(res.Code, ShouldEqual, ecode.RequestErr.Code())
  112. }))
  113. })
  114. }