mc_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package block
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "go-common/app/service/main/member/conf"
  7. model "go-common/app/service/main/member/model/block"
  8. "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestDaouserKey(t *testing.T) {
  11. convey.Convey("userKey", t, func(ctx convey.C) {
  12. var (
  13. mid = int64(46333)
  14. )
  15. ctx.Convey("When everything right", func(ctx convey.C) {
  16. key := userKey(mid)
  17. ctx.Convey("Then key should equal u_mid.", func(ctx convey.C) {
  18. ctx.So(key, convey.ShouldEqual, "u_46333")
  19. })
  20. })
  21. })
  22. }
  23. func TestDaoSetUserCache(t *testing.T) {
  24. convey.Convey("SetUserCache", t, func(ctx convey.C) {
  25. var (
  26. c = context.Background()
  27. mid = int64(46333)
  28. status = model.BlockStatusForever
  29. startTime = time.Now().Unix()
  30. endTime = time.Now().Add(time.Minute).Unix()
  31. )
  32. ctx.Convey("When SetUserCache", func(ctx convey.C) {
  33. err := d.SetUserCache(c, mid, status, startTime, endTime)
  34. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  35. ctx.So(err, convey.ShouldBeNil)
  36. ctx.Convey("When get UsersCache", func(ctx convey.C) {
  37. res, err := d.UsersCache(c, []int64{mid})
  38. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  39. ctx.So(err, convey.ShouldBeNil)
  40. ctx.So(res, convey.ShouldNotBeNil)
  41. })
  42. })
  43. ctx.Convey("When delete UsersCache", func(ctx convey.C) {
  44. err := d.DeleteUserCache(c, mid)
  45. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  46. ctx.So(err, convey.ShouldBeNil)
  47. })
  48. })
  49. })
  50. })
  51. })
  52. }
  53. func TestDaomcUserExpire(t *testing.T) {
  54. convey.Convey("mcUserExpire", t, func(ctx convey.C) {
  55. ctx.Convey("When everything right", func(ctx convey.C) {
  56. sec := d.mcUserExpire("ut_key")
  57. ctx.Convey("Then sec should >=mcUserExpireBase and <=mcUserExpireBase*conf.Conf.Memcache.Expire.UserMaxRate .", func(ctx convey.C) {
  58. ctx.So(d.UserTTL, convey.ShouldBeGreaterThan, 0)
  59. ctx.So(conf.Conf.BlockCacheTTL.UserMaxRate, convey.ShouldBeGreaterThan, 0)
  60. ctx.So(sec, convey.ShouldBeGreaterThanOrEqualTo, d.UserTTL)
  61. ctx.So(sec, convey.ShouldBeLessThanOrEqualTo, int32(conf.Conf.BlockCacheTTL.UserMaxRate*float64(d.UserTTL)))
  62. })
  63. })
  64. })
  65. }
  66. func TestDaoSetUserDetailCache(t *testing.T) {
  67. convey.Convey("SetUserDetailCache", t, func(ctx convey.C) {
  68. var (
  69. c = context.Background()
  70. mid = int64(46333)
  71. )
  72. ctx.Convey("When SetUserDetailCache", func(ctx convey.C) {
  73. err := d.SetUserDetailCache(c, mid, &model.MCUserDetail{BlockCount: 12})
  74. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  75. ctx.So(err, convey.ShouldBeNil)
  76. ctx.Convey("When get UserDetailCache", func(ctx convey.C) {
  77. res, err := d.UserDetailsCache(c, []int64{mid})
  78. ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
  79. ctx.So(err, convey.ShouldBeNil)
  80. ctx.So(res, convey.ShouldNotBeNil)
  81. })
  82. })
  83. ctx.Convey("When delete UsersDetailCache", func(ctx convey.C) {
  84. err := d.DeleteUserDetailCache(c, mid)
  85. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  86. ctx.So(err, convey.ShouldBeNil)
  87. })
  88. })
  89. })
  90. })
  91. })
  92. }