setting_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package service
  2. import (
  3. "context"
  4. ht "net/http"
  5. "sync"
  6. "testing"
  7. "go-common/app/interface/main/push-archive/conf"
  8. "go-common/app/interface/main/push-archive/model"
  9. pb "go-common/app/service/main/push/api/grpc/v1"
  10. "go-common/library/ecode"
  11. bm "go-common/library/net/http/blademaster"
  12. "github.com/smartystreets/goconvey/convey"
  13. )
  14. func Test_getsetting(t *testing.T) {
  15. initd()
  16. mid := int64(11111111)
  17. s.SetSetting(context.TODO(), mid, nil)
  18. convey.Convey("获取默认的用户开关设置", t, func() {
  19. st, err := s.Setting(context.TODO(), mid)
  20. convey.So(err, convey.ShouldBeNil)
  21. convey.So(st.Type, convey.ShouldEqual, model.PushTypeSpecial)
  22. })
  23. }
  24. func Test_setsetting(t *testing.T) {
  25. initd()
  26. mid := int64(11111111)
  27. convey.Convey("存储用户开关设置", t, func() {
  28. err := s.SetSetting(context.TODO(), mid, &model.Setting{Type: model.PushTypeAttention})
  29. convey.So(err, convey.ShouldBeNil)
  30. })
  31. }
  32. func Test_ps(t *testing.T) {
  33. initd()
  34. url := "http://127.0.0.1:7031/x/push-archive/setting/get?access_key=848657e31639317257ab274741c6ec7d"
  35. client := bm.NewClient(conf.Conf.HTTPClient)
  36. type _response struct {
  37. Code int `json:"code"`
  38. Data model.Setting `json:"data"`
  39. }
  40. wg := sync.WaitGroup{}
  41. all := 10
  42. busyIn := 0
  43. errIn := 0
  44. normalIn := 0
  45. mx := sync.Mutex{}
  46. for i := 0; i < all; i++ {
  47. wg.Add(1)
  48. go func(i int) {
  49. defer wg.Done()
  50. defer mx.Unlock()
  51. req, err := ht.NewRequest("GET", url, nil)
  52. if err != nil {
  53. mx.Lock()
  54. errIn++
  55. return
  56. }
  57. r := &_response{}
  58. err = client.Do(context.TODO(), req, r)
  59. mx.Lock()
  60. if err != nil {
  61. errIn++
  62. return
  63. }
  64. if r.Code == ecode.ServiceUnavailable.Code() {
  65. busyIn++
  66. return
  67. }
  68. normalIn++
  69. }(i)
  70. }
  71. wg.Wait()
  72. convey.Convey("并行访问推送开关,有频率限制", t, func() {
  73. convey.So(errIn, convey.ShouldEqual, 0)
  74. convey.So(busyIn, convey.ShouldBeGreaterThan, 0)
  75. convey.So(errIn+busyIn+normalIn, convey.ShouldEqual, all)
  76. })
  77. t.Logf("the errin(%d), busyin(%d), normalin(%d)", errIn, busyIn, normalIn)
  78. }
  79. func Test_pushrpc(t *testing.T) {
  80. initd()
  81. convey.Convey("推送平台rpc设置推送开关", t, func() {
  82. set := &pb.SetSettingRequest{
  83. Mid: int64(111111111),
  84. Type: 1,
  85. Value: 3,
  86. }
  87. res, err := s.pushRPC.Setting(context.TODO(), &pb.SettingRequest{Mid: set.Mid})
  88. convey.So(err, convey.ShouldBeNil)
  89. t.Logf("setting(%+v)", res)
  90. _, err = s.pushRPC.SetSetting(context.TODO(), set)
  91. convey.So(err, convey.ShouldBeNil)
  92. res, err = s.pushRPC.Setting(context.TODO(), &pb.SettingRequest{Mid: set.Mid})
  93. convey.So(err, convey.ShouldBeNil)
  94. t.Logf("setting(%+v)", res)
  95. })
  96. }