memcache_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "go-common/app/service/main/push/model"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func Test_PingMc(t *testing.T) {
  10. Convey("ping mc", t, WithDao(func(d *Dao) {
  11. err := d.pingMC(context.Background())
  12. So(err, ShouldBeNil)
  13. }))
  14. }
  15. func TestAddReportsCacheByMids(t *testing.T) {
  16. Convey("add reports cache by mids", t, WithDao(func(d *Dao) {
  17. var err error
  18. mrs := map[int64][]*model.Report{
  19. 910819: {{
  20. APPID: 1,
  21. PlatformID: 1,
  22. Mid: 910819,
  23. DeviceToken: "dt1",
  24. }, {
  25. APPID: 2,
  26. PlatformID: 2,
  27. Mid: 910819,
  28. DeviceToken: "dt2",
  29. }},
  30. 123456: {{
  31. APPID: 3,
  32. PlatformID: 3,
  33. Mid: 123456,
  34. DeviceToken: "dt3",
  35. }},
  36. }
  37. err = d.AddReportsCacheByMids(context.Background(), mrs)
  38. So(err, ShouldBeNil)
  39. }))
  40. }
  41. func Test_ReportCache(t *testing.T) {
  42. Convey("reports cache", t, WithDao(func(d *Dao) {
  43. var err error
  44. mrs := map[int64][]*model.Report{
  45. 910819: {{
  46. APPID: 1,
  47. PlatformID: 1,
  48. Mid: 910819,
  49. DeviceToken: "dt1",
  50. }, {
  51. APPID: 2,
  52. PlatformID: 2,
  53. Mid: 910819,
  54. DeviceToken: "dt2",
  55. }},
  56. 123456: {{
  57. APPID: 3,
  58. PlatformID: 3,
  59. Mid: 123456,
  60. DeviceToken: "dt3",
  61. }},
  62. }
  63. err = d.AddReportsCacheByMids(context.Background(), mrs)
  64. So(err, ShouldBeNil)
  65. // add report
  66. // err = d.AddReportCache(context.Background(), &model.Report{APPID: 3, PlatformID: 3, Mid: 123456, DeviceToken: "dt4"})
  67. // So(err, ShouldBeNil)
  68. // err = d.AddReportCache(context.Background(), &model.Report{APPID: 4, PlatformID: 4, Mid: 123456, DeviceToken: "dt5"})
  69. // So(err, ShouldBeNil)
  70. // delete report
  71. err = d.DelReportCache(context.Background(), 910819, 2, "dt2")
  72. So(err, ShouldBeNil)
  73. // get report
  74. rs, missed, err := d.ReportsCacheByMids(context.Background(), []int64{910819, 123456})
  75. _ = missed
  76. So(len(rs), ShouldEqual, 2)
  77. So(err, ShouldBeNil)
  78. for mid, v := range rs {
  79. for _, vv := range v {
  80. fmt.Printf("mid(%d) %+v \n", mid, vv)
  81. }
  82. }
  83. // report miss
  84. rs, misses, err := d.ReportsCacheByMids(context.Background(), []int64{1000000, 2000000})
  85. So(len(rs), ShouldEqual, 0)
  86. So(len(misses), ShouldEqual, 2)
  87. So(err, ShouldBeNil)
  88. }))
  89. }
  90. func Test_TokenCache(t *testing.T) {
  91. Convey("add token cache", t, WithDao(func(d *Dao) {
  92. token := "testtoken"
  93. r := &model.Report{
  94. APPID: 1,
  95. DeviceToken: token,
  96. }
  97. err := d.AddTokenCache(context.Background(), r.DeviceToken, r)
  98. So(err, ShouldBeNil)
  99. m := make(map[string]*model.Report, 0)
  100. m[r.DeviceToken] = r
  101. d.AddTokensCache(context.Background(), m)
  102. So(err, ShouldBeNil)
  103. Convey("token cache", func() {
  104. r, err := d.TokenCache(context.Background(), token)
  105. So(err, ShouldBeNil)
  106. t.Logf("report(%+v)", r)
  107. Convey("delete token cache", func() {
  108. err = d.DelTokenCache(context.Background(), token)
  109. So(err, ShouldBeNil)
  110. })
  111. })
  112. }))
  113. }
  114. func Test_TokensCache(t *testing.T) {
  115. Convey("tokens cache", t, WithDao(func(d *Dao) {
  116. r := &model.Report{APPID: 1, DeviceToken: "testtoken1"}
  117. err := d.AddTokenCache(context.Background(), r.DeviceToken, r)
  118. So(err, ShouldBeNil)
  119. r = &model.Report{APPID: 1, DeviceToken: "testtoken2"}
  120. err = d.AddTokenCache(context.Background(), r.DeviceToken, r)
  121. So(err, ShouldBeNil)
  122. res, missed, err := d.TokensCache(context.Background(), []string{"testtoken1", "testtoken2", "testtoken3"})
  123. So(err, ShouldBeNil)
  124. t.Logf("tokens cache missed(%v)", missed)
  125. for token, val := range res {
  126. t.Logf("token(%s) value(%+v)", token, val)
  127. }
  128. }))
  129. }
  130. func Test_ReportsCacheByMid(t *testing.T) {
  131. Convey("Test_ReportsCacheByMid", t, WithDao(func(d *Dao) {
  132. _, err := d.ReportsCacheByMid(context.Background(), 123)
  133. So(err, ShouldBeNil)
  134. }))
  135. }