invite_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "go-common/library/ecode"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestService_Generate(t *testing.T) {
  10. Convey("Generate 10 codes in batch", t, func() {
  11. mid := int64(88888970)
  12. num := int64(10)
  13. expireDay := int64(30)
  14. res, err := s.Generate(context.Background(), mid, num, expireDay)
  15. So(err, ShouldBeNil)
  16. So(len(res), ShouldEqual, num)
  17. })
  18. }
  19. func TestService_List(t *testing.T) {
  20. Convey("List when account's invite codes is not empty", t, func() {
  21. mid := int64(88888970)
  22. now := time.Now().Unix()
  23. start, end := now-86400, now+86400
  24. res, err := s.List(context.Background(), mid, start, end)
  25. So(err, ShouldBeNil)
  26. So(len(res), ShouldBeGreaterThan, 0)
  27. })
  28. }
  29. func TestService_ConcurrentGeneInviteCode(t *testing.T) {
  30. Convey("Generate 1000 codes in concurrency", t, func() {
  31. num := 1000
  32. mid := int64(88888970)
  33. ts := time.Now().Unix()
  34. m, err := concurrentGenerateCode(mid, ts, num, _geneSubCount)
  35. So(err, ShouldBeNil)
  36. So(len(m), ShouldEqual, num)
  37. })
  38. }
  39. func TestService_FetchMultiInfo(t *testing.T) {
  40. time.Sleep(time.Second * 2)
  41. Convey("Fetch multi info", t, func() {
  42. mids := []int64{88888970}
  43. Convey("when not timeout", func() {
  44. res, err := s.fetchInfos(context.Background(), mids, time.Second)
  45. So(err, ShouldBeNil)
  46. So(len(res), ShouldEqual, len(mids))
  47. })
  48. Convey("when timeout", func() {
  49. _, err := s.fetchInfos(context.Background(), mids, time.Millisecond)
  50. So(err, ShouldEqual, ecode.Deadline.Error())
  51. })
  52. })
  53. }