limit_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package service
  2. import (
  3. "testing"
  4. "time"
  5. "go-common/app/interface/main/push-archive/dao"
  6. "github.com/smartystreets/goconvey/convey"
  7. )
  8. func Test_limit(t *testing.T) {
  9. initd2()
  10. upper := int64(113)
  11. convey.Convey("upper主次数限制", t, func() {
  12. convey.So(s.limit(upper), convey.ShouldEqual, false)
  13. convey.So(s.limit(upper), convey.ShouldEqual, true)
  14. convey.So(s.limit(upper), convey.ShouldEqual, true)
  15. time.Sleep(time.Second * 2)
  16. convey.So(s.limit(upper), convey.ShouldEqual, false)
  17. convey.So(s.limit(upper), convey.ShouldEqual, true)
  18. })
  19. fan := int64(121)
  20. g := &dao.FanGroup{
  21. Limit: 2,
  22. PerUpperLimit: 0,
  23. LimitExpire: 2,
  24. }
  25. noLimitFans := &map[int64]int{}
  26. convey.Convey("粉丝推送次数限制", t, func() {
  27. convey.So(s.pushLimit(fan, upper, g, noLimitFans), convey.ShouldEqual, true)
  28. convey.So(s.pushLimit(fan, upper, g, noLimitFans), convey.ShouldEqual, true)
  29. convey.So(s.pushLimit(fan, upper, g, noLimitFans), convey.ShouldEqual, false)
  30. time.Sleep(time.Second * 2)
  31. convey.So(s.pushLimit(fan, upper, g, noLimitFans), convey.ShouldEqual, true)
  32. })
  33. g.PerUpperLimit = 1
  34. fan = int64(333)
  35. convey.Convey("粉丝推送upper主的次数限制", t, func() {
  36. convey.So(s.pushLimit(fan, upper, g, noLimitFans), convey.ShouldEqual, true)
  37. convey.So(s.pushLimit(fan, upper, g, noLimitFans), convey.ShouldEqual, false)
  38. time.Sleep(time.Second * 2)
  39. convey.So(s.pushLimit(fan, upper, g, noLimitFans), convey.ShouldEqual, true)
  40. })
  41. convey.Convey("粉丝不受pushlimit限制", t, func() {
  42. convey.So(s.pushLimit(fan, upper, g, noLimitFans), convey.ShouldEqual, false)
  43. (*noLimitFans)[fan] = 1
  44. convey.So(s.pushLimit(fan, upper, g, noLimitFans), convey.ShouldEqual, true)
  45. })
  46. }