mc_wallet_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package dao
  2. import (
  3. . "github.com/smartystreets/goconvey/convey"
  4. "testing"
  5. "go-common/app/service/live/wallet/model"
  6. mc "go-common/library/cache/memcache"
  7. "math/rand"
  8. "time"
  9. )
  10. func TestDao_WalletCache(t *testing.T) {
  11. once.Do(startService)
  12. Convey("Get Nil Cache", t, func() {
  13. r = rand.New(rand.NewSource(time.Now().UnixNano()))
  14. uid := r.Int63n(1000000)
  15. _, err := d.WalletCache(ctx, uid)
  16. So(err, ShouldEqual, mc.ErrNotFound)
  17. })
  18. Convey("Set And Get And Del", t, func() {
  19. r = rand.New(rand.NewSource(time.Now().UnixNano()))
  20. uid := r.Int63n(1000000)
  21. detail := &model.Detail{Uid: uid, Gold: 1, IapGold: 1, Silver: 1, GoldPayCnt: 1, GoldRechargeCnt: 1, SilverPayCnt: 1}
  22. mcDetail := &model.McDetail{Detail: detail, Exist: true, Version: d.CacheVersion(ctx)}
  23. err := d.SetWalletCache(ctx, mcDetail, d.cacheExpire)
  24. So(err, ShouldBeNil)
  25. nd, err := d.WalletCache(ctx, uid)
  26. So(err, ShouldBeNil)
  27. So(nd.Exist, ShouldEqual, true)
  28. So(nd.Detail.Gold, ShouldEqual, 1)
  29. So(nd.Detail.IapGold, ShouldEqual, 1)
  30. So(nd.Detail.Silver, ShouldEqual, 1)
  31. So(nd.Detail.GoldPayCnt, ShouldEqual, 1)
  32. So(nd.Detail.GoldRechargeCnt, ShouldEqual, 1)
  33. So(nd.Detail.SilverPayCnt, ShouldEqual, 1)
  34. So(nd.Version, ShouldEqual, d.CacheVersion(ctx))
  35. err = d.DelWalletCache(ctx, uid)
  36. So(err, ShouldBeNil)
  37. nnd, err := d.WalletCache(ctx, uid)
  38. So(err, ShouldEqual, mc.ErrNotFound)
  39. So(nnd, ShouldBeNil)
  40. })
  41. Convey("version", t, func() {
  42. Convey("just set", func() {
  43. uid := int64(1)
  44. d.DelWalletCache(ctx, uid)
  45. d.GetDetailByCache(ctx, uid)
  46. detail, err := d.WalletCache(ctx, uid)
  47. So(err, ShouldBeNil)
  48. So(detail.Version, ShouldEqual, 1)
  49. })
  50. Convey("old to new", func() {
  51. uid := int64(1)
  52. od, err := d.Detail(ctx, uid)
  53. So(err, ShouldBeNil)
  54. d.DelWalletCache(ctx, uid)
  55. detail := &model.Detail{Uid: uid, Gold: od.Gold + 1, IapGold: 1, Silver: 1, GoldPayCnt: 1, GoldRechargeCnt: 1, SilverPayCnt: 1} // fake data
  56. mcDetail := &model.McDetail{Detail: detail, Exist: true} // old version
  57. err = d.SetWalletCache(ctx, mcDetail, d.cacheExpire)
  58. So(err, ShouldBeNil)
  59. So(mcDetail.Version, ShouldEqual, 0)
  60. So(mcDetail.Detail.Gold, ShouldEqual, od.Gold+1)
  61. So(mcDetail.Detail.IapGold, ShouldEqual, 1)
  62. So(mcDetail.Detail.Silver, ShouldEqual, 1)
  63. So(mcDetail.Detail.GoldPayCnt, ShouldEqual, 1)
  64. mcDetail1, err := d.WalletCache(ctx, uid) //
  65. So(err, ShouldBeNil)
  66. So(mcDetail1.Detail.Gold, ShouldEqual, od.Gold+1)
  67. fd, err := d.GetDetailByCache(ctx, uid) // check version if it is old update cache
  68. So(err, ShouldBeNil)
  69. So(fd.Gold, ShouldEqual, od.Gold)
  70. So(fd.Silver, ShouldEqual, od.Silver)
  71. So(fd.GoldPayCnt, ShouldEqual, od.GoldPayCnt)
  72. mcDetail2, err := d.WalletCache(ctx, uid)
  73. So(err, ShouldBeNil)
  74. So(mcDetail2.Version, ShouldEqual, d.CacheVersion(ctx))
  75. So(mcDetail2.Detail.Gold, ShouldEqual, od.Gold)
  76. })
  77. })
  78. }