service_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package service
  2. import (
  3. "context"
  4. "flag"
  5. . "github.com/smartystreets/goconvey/convey"
  6. "go-common/app/interface/live/push-live/conf"
  7. "go-common/library/cache/redis"
  8. "path/filepath"
  9. "testing"
  10. )
  11. var (
  12. s *Service
  13. targetID int64
  14. )
  15. func initd() {
  16. dir, _ := filepath.Abs("../cmd/push-live-test.toml")
  17. flag.Set("conf", dir)
  18. conf.Init()
  19. s = New(conf.Conf)
  20. }
  21. func TestService_ConvertStrToInt64(t *testing.T) {
  22. initd()
  23. Convey("test convert", t, func() {
  24. mStr := "1,2,3"
  25. mInt64 := []int64{
  26. int64(1), int64(2), int64(3),
  27. }
  28. mRes, err := s.convertStrToInt64(mStr)
  29. So(err, ShouldBeNil)
  30. So(mRes, ShouldResemble, mInt64)
  31. })
  32. }
  33. func TestService_limitDecreaseUnique(t *testing.T) {
  34. initd()
  35. Convey("test limit decrease request unique", t, func() {
  36. var (
  37. err error
  38. conn redis.Conn
  39. key string
  40. )
  41. Convey("test success request", func() {
  42. key = "test_request_unique"
  43. conn, err = redis.Dial(s.c.Redis.PushInterval.Proto, s.c.Redis.PushInterval.Addr, s.dao.RedisOption()...)
  44. So(err, ShouldBeNil)
  45. err = s.limitDecreaseUnique(key)
  46. So(err, ShouldBeNil)
  47. // clean
  48. conn.Do("DEL", key)
  49. conn.Close()
  50. })
  51. })
  52. }
  53. func TestService_LimitDecrease(t *testing.T) {
  54. initd()
  55. Convey("test LimitDecrease service", t, func() {
  56. var (
  57. ctx = context.Background()
  58. business, targetID, uuid, midStr string
  59. err error
  60. conn redis.Conn
  61. )
  62. Convey("test success", func() {
  63. business = "111"
  64. targetID = "123"
  65. uuid = "test"
  66. midStr = "1,2,3"
  67. conn, err = redis.Dial(s.c.Redis.PushInterval.Proto, s.c.Redis.PushInterval.Addr, s.dao.RedisOption()...)
  68. So(err, ShouldBeNil)
  69. err = s.LimitDecrease(ctx, business, targetID, uuid, midStr)
  70. So(err, ShouldBeNil)
  71. // clean
  72. key := getUniqueKey(business, targetID, uuid)
  73. conn.Do("DEL", key)
  74. conn.Close()
  75. })
  76. })
  77. }