util_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package mon
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. func TestFormatAddrs(t *testing.T) {
  12. tests := []struct {
  13. addrs []string
  14. expect string
  15. }{
  16. {
  17. addrs: []string{"a", "b"},
  18. expect: "a,b",
  19. },
  20. {
  21. addrs: []string{"a", "b", "c"},
  22. expect: "a,b,c",
  23. },
  24. {
  25. addrs: []string{},
  26. expect: "",
  27. },
  28. {
  29. addrs: nil,
  30. expect: "",
  31. },
  32. }
  33. for _, test := range tests {
  34. assert.Equal(t, test.expect, FormatAddr(test.addrs))
  35. }
  36. }
  37. func Test_logDuration(t *testing.T) {
  38. var buf strings.Builder
  39. w := logx.NewWriter(&buf)
  40. o := logx.Reset()
  41. logx.SetWriter(w)
  42. defer func() {
  43. logx.Reset()
  44. logx.SetWriter(o)
  45. }()
  46. buf.Reset()
  47. logDuration(context.Background(), "foo", "bar", time.Millisecond, nil)
  48. assert.Contains(t, buf.String(), "foo")
  49. assert.Contains(t, buf.String(), "bar")
  50. buf.Reset()
  51. logDuration(context.Background(), "foo", "bar", time.Millisecond, errors.New("bar"))
  52. assert.Contains(t, buf.String(), "foo")
  53. assert.Contains(t, buf.String(), "bar")
  54. assert.Contains(t, buf.String(), "fail")
  55. }