You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package log_test
  2. import (
  3. "bytes"
  4. "errors"
  5. "io/ioutil"
  6. "math"
  7. "regexp"
  8. "testing"
  9. kitlog "github.com/go-kit/kit/log"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/tendermint/tendermint/libs/log"
  12. )
  13. func TestTMFmtLogger(t *testing.T) {
  14. t.Parallel()
  15. buf := &bytes.Buffer{}
  16. logger := log.NewTMFmtLogger(buf)
  17. if err := logger.Log("hello", "world"); err != nil {
  18. t.Fatal(err)
  19. }
  20. assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+ hello=world\n$`), buf.String())
  21. buf.Reset()
  22. if err := logger.Log("a", 1, "err", errors.New("error")); err != nil {
  23. t.Fatal(err)
  24. }
  25. assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+ a=1 err=error\n$`), buf.String())
  26. buf.Reset()
  27. if err := logger.Log("std_map", map[int]int{1: 2}, "my_map", mymap{0: 0}); err != nil {
  28. t.Fatal(err)
  29. }
  30. assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+ std_map=map\[1:2\] my_map=special_behavior\n$`), buf.String())
  31. buf.Reset()
  32. if err := logger.Log("level", "error"); err != nil {
  33. t.Fatal(err)
  34. }
  35. assert.Regexp(t, regexp.MustCompile(`E\[.+\] unknown \s+\n$`), buf.String())
  36. buf.Reset()
  37. if err := logger.Log("_msg", "Hello"); err != nil {
  38. t.Fatal(err)
  39. }
  40. assert.Regexp(t, regexp.MustCompile(`N\[.+\] Hello \s+\n$`), buf.String())
  41. buf.Reset()
  42. if err := logger.Log("module", "main", "module", "crypto", "module", "wire"); err != nil {
  43. t.Fatal(err)
  44. }
  45. assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+module=wire\s+\n$`), buf.String())
  46. buf.Reset()
  47. if err := logger.Log("hash", []byte("test me")); err != nil {
  48. t.Fatal(err)
  49. }
  50. assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+ hash=74657374206D65\n$`), buf.String())
  51. }
  52. func BenchmarkTMFmtLoggerSimple(b *testing.B) {
  53. benchmarkRunnerKitlog(b, log.NewTMFmtLogger(ioutil.Discard), baseMessage)
  54. }
  55. func BenchmarkTMFmtLoggerContextual(b *testing.B) {
  56. benchmarkRunnerKitlog(b, log.NewTMFmtLogger(ioutil.Discard), withMessage)
  57. }
  58. func TestTMFmtLoggerConcurrency(t *testing.T) {
  59. t.Parallel()
  60. testConcurrency(t, log.NewTMFmtLogger(ioutil.Discard), 10000)
  61. }
  62. func benchmarkRunnerKitlog(b *testing.B, logger kitlog.Logger, f func(kitlog.Logger)) {
  63. lc := kitlog.With(logger, "common_key", "common_value")
  64. b.ReportAllocs()
  65. b.ResetTimer()
  66. for i := 0; i < b.N; i++ {
  67. f(lc)
  68. }
  69. }
  70. //nolint: errcheck // ignore errors
  71. var (
  72. baseMessage = func(logger kitlog.Logger) { logger.Log("foo_key", "foo_value") }
  73. withMessage = func(logger kitlog.Logger) { kitlog.With(logger, "a", "b").Log("d", "f") }
  74. )
  75. // These test are designed to be run with the race detector.
  76. func testConcurrency(t *testing.T, logger kitlog.Logger, total int) {
  77. n := int(math.Sqrt(float64(total)))
  78. share := total / n
  79. errC := make(chan error, n)
  80. for i := 0; i < n; i++ {
  81. go func() {
  82. errC <- spam(logger, share)
  83. }()
  84. }
  85. for i := 0; i < n; i++ {
  86. err := <-errC
  87. if err != nil {
  88. t.Fatalf("concurrent logging error: %v", err)
  89. }
  90. }
  91. }
  92. func spam(logger kitlog.Logger, count int) error {
  93. for i := 0; i < count; i++ {
  94. err := logger.Log("key", i)
  95. if err != nil {
  96. return err
  97. }
  98. }
  99. return nil
  100. }
  101. type mymap map[int]int
  102. func (m mymap) String() string { return "special_behavior" }