service_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package service
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "go-common/app/job/main/spy/conf"
  10. "go-common/app/job/main/spy/model"
  11. "go-common/library/cache/redis"
  12. "github.com/robfig/cron"
  13. . "github.com/smartystreets/goconvey/convey"
  14. )
  15. var (
  16. c = context.Background()
  17. s *Service
  18. testCron = "*/5 * * * * ?"
  19. testCyTime = 5000
  20. )
  21. func init() {
  22. var (
  23. err error
  24. )
  25. dir, _ := filepath.Abs("../cmd/spy-job-dev.toml")
  26. flag.Set("conf", dir)
  27. if err = conf.Init(); err != nil {
  28. panic(err)
  29. }
  30. if s == nil {
  31. s = New(conf.Conf)
  32. }
  33. time.Sleep(time.Second)
  34. }
  35. func CleanCache() {
  36. pool := redis.NewPool(conf.Conf.Redis.Config)
  37. pool.Get(c).Do("FLUSHDB")
  38. }
  39. func WithService(f func(s *Service)) func() {
  40. return func() {
  41. Reset(func() { CleanCache() })
  42. f(s)
  43. }
  44. }
  45. func Test_LoadSystemConfig(t *testing.T) {
  46. Convey("Test_LoadSystemConfig had data", t, WithService(func(s *Service) {
  47. fmt.Println(s.spyConfig)
  48. So(s.spyConfig, ShouldContainKey, model.LimitBlockCount)
  49. So(s.spyConfig, ShouldContainKey, model.LessBlockScore)
  50. So(s.spyConfig, ShouldContainKey, model.AutoBlock)
  51. }))
  52. }
  53. func Test_cycleblock(t *testing.T) {
  54. Convey("Test_cycleblock cron", t, WithService(func(s *Service) {
  55. fmt.Println("Test_cycleblock start ")
  56. tx, err := s.dao.BeginTran(c)
  57. So(err, ShouldBeNil)
  58. ui := &model.UserInfo{Mid: testBlockMid, State: model.StateNormal}
  59. err = s.dao.TxUpdateUserState(c, tx, ui)
  60. So(err, ShouldBeNil)
  61. err = tx.Commit()
  62. So(err, ShouldBeNil)
  63. lastBlockNo := s.lastBlockNo(s.c.Property.Block.CycleTimes)
  64. t := cron.New()
  65. err = t.AddFunc(testCron, s.cycleblock)
  66. if err != nil {
  67. panic(err)
  68. }
  69. t.Start()
  70. Convey("Test_cycleblock user info 1", WithService(func(s *Service) {
  71. var ui *model.UserInfo
  72. ui, err = s.dao.UserInfo(context.TODO(), testBlockMid)
  73. So(err, ShouldBeNil)
  74. So(ui.State == model.StateNormal, ShouldBeTrue)
  75. }))
  76. err = s.dao.AddBlockCache(c, testBlockMid, testLowScore, lastBlockNo)
  77. So(err, ShouldBeNil)
  78. mids, err := s.blockUsers(c, lastBlockNo)
  79. So(err, ShouldBeNil)
  80. So(mids, ShouldContain, testBlockMid)
  81. time.Sleep(5000 * time.Millisecond)
  82. time.Sleep(time.Duration(s.blockWaitTick))
  83. Convey("Test_cycleblock user info 2 ", WithService(func(s *Service) {
  84. ui, err := s.dao.UserInfo(context.TODO(), testBlockMid)
  85. So(err, ShouldBeNil)
  86. So(ui.State == model.StateBlock, ShouldBeTrue)
  87. }))
  88. fmt.Println("Test_cycleblock end ")
  89. }))
  90. }
  91. // go test -test.v -test.run TestStat
  92. func TestStat(t *testing.T) {
  93. Convey(" UpdateStatData ", t, WithService(func(s *Service) {
  94. err := s.UpdateStatData(c, &model.SpyStatMessage{
  95. TargetMid: 1,
  96. TargetID: 1,
  97. EventName: "init_user_info",
  98. Type: model.IncreaseStat,
  99. Quantity: 2,
  100. Time: time.Now().Unix(),
  101. UUID: "123456789qweasdzxcccccccc",
  102. })
  103. So(err, ShouldBeNil)
  104. }))
  105. Convey(" UpdateStatData 2", t, WithService(func(s *Service) {
  106. err := s.UpdateStatData(c, &model.SpyStatMessage{
  107. TargetMid: 1,
  108. TargetID: 1,
  109. EventName: "auto_block",
  110. Type: model.ResetStat,
  111. Quantity: 2,
  112. Time: time.Now().Unix(),
  113. UUID: "123456789qweasdzxcccccccc",
  114. })
  115. So(err, ShouldBeNil)
  116. }))
  117. }