util_test.go 1019 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package mon
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/zeromicro/go-zero/core/logx/logtest"
  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. buf := logtest.NewCollector(t)
  38. buf.Reset()
  39. logDuration(context.Background(), "foo", "bar", time.Millisecond, nil)
  40. assert.Contains(t, buf.String(), "foo")
  41. assert.Contains(t, buf.String(), "bar")
  42. buf.Reset()
  43. logDuration(context.Background(), "foo", "bar", time.Millisecond, errors.New("bar"))
  44. assert.Contains(t, buf.String(), "foo")
  45. assert.Contains(t, buf.String(), "bar")
  46. assert.Contains(t, buf.String(), "fail")
  47. }