util_test.go 1.1 KB

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