service_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package service
  2. import (
  3. "container/ring"
  4. "context"
  5. "flag"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "go-common/app/job/main/sms/conf"
  10. "go-common/app/job/main/sms/model"
  11. smsmdl "go-common/app/service/main/sms/model"
  12. "go-common/library/net/trace"
  13. . "github.com/smartystreets/goconvey/convey"
  14. )
  15. var srv *Service
  16. func init() {
  17. dir, _ := filepath.Abs("../cmd/sms-job-test.toml")
  18. flag.Set("conf", dir)
  19. conf.Init()
  20. srv = New(conf.Conf)
  21. time.Sleep(time.Second)
  22. }
  23. func WithService(f func(s *Service)) func() {
  24. return func() {
  25. f(srv)
  26. }
  27. }
  28. func Test_ring(t *testing.T) {
  29. Convey("test ring", t, WithService(func(s *Service) {
  30. r := ring.New(3)
  31. r.Value = 0
  32. r = r.Next()
  33. r.Value = 1
  34. r = r.Next()
  35. r.Value = 2
  36. So(r.Len(), ShouldEqual, 3)
  37. for i := 0; i < 5; i++ {
  38. r = r.Next()
  39. t.Logf("%d", r.Value)
  40. }
  41. }))
  42. }
  43. func Test_Sms(t *testing.T) {
  44. Convey("sms", t, WithService(func(s *Service) {
  45. // http request会自动加trace header,不init trace的话,header value为空为会兴企reset
  46. trace.Init(s.c.Tracer)
  47. defer trace.Close()
  48. c := context.TODO()
  49. sl := &smsmdl.ModelSend{Mobile: "", Content: "您的账号正在哔哩哔哩2017动画角色人气大赏活动中进行领票操作,验证码为123456当日有效", Code: "whatever", Type: 1}
  50. p := s.smsp.Value.(model.Provider)
  51. _, err := p.SendSms(c, sl)
  52. So(err, ShouldBeNil)
  53. s.smsp.Ring = s.smsp.Next()
  54. p = s.smsp.Value.(model.Provider)
  55. _, err = p.SendSms(c, sl)
  56. So(err, ShouldBeNil)
  57. s.smsp.Ring = s.smsp.Next()
  58. p = s.smsp.Value.(model.Provider)
  59. _, err = p.SendSms(c, sl)
  60. So(err, ShouldBeNil)
  61. s.smsp.Ring = s.smsp.Next()
  62. p = s.smsp.Value.(model.Provider)
  63. _, err = p.SendSms(c, sl)
  64. So(err, ShouldBeNil)
  65. }))
  66. }