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.

118 lines
2.8 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/tmlibs/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. }
  47. func BenchmarkTMFmtLoggerSimple(b *testing.B) {
  48. benchmarkRunnerKitlog(b, log.NewTMFmtLogger(ioutil.Discard), baseMessage)
  49. }
  50. func BenchmarkTMFmtLoggerContextual(b *testing.B) {
  51. benchmarkRunnerKitlog(b, log.NewTMFmtLogger(ioutil.Discard), withMessage)
  52. }
  53. func TestTMFmtLoggerConcurrency(t *testing.T) {
  54. t.Parallel()
  55. testConcurrency(t, log.NewTMFmtLogger(ioutil.Discard), 10000)
  56. }
  57. func benchmarkRunnerKitlog(b *testing.B, logger kitlog.Logger, f func(kitlog.Logger)) {
  58. lc := kitlog.With(logger, "common_key", "common_value")
  59. b.ReportAllocs()
  60. b.ResetTimer()
  61. for i := 0; i < b.N; i++ {
  62. f(lc)
  63. }
  64. }
  65. var (
  66. baseMessage = func(logger kitlog.Logger) { logger.Log("foo_key", "foo_value") }
  67. withMessage = func(logger kitlog.Logger) { kitlog.With(logger, "a", "b").Log("d", "f") }
  68. )
  69. // These test are designed to be run with the race detector.
  70. func testConcurrency(t *testing.T, logger kitlog.Logger, total int) {
  71. n := int(math.Sqrt(float64(total)))
  72. share := total / n
  73. errC := make(chan error, n)
  74. for i := 0; i < n; i++ {
  75. go func() {
  76. errC <- spam(logger, share)
  77. }()
  78. }
  79. for i := 0; i < n; i++ {
  80. err := <-errC
  81. if err != nil {
  82. t.Fatalf("concurrent logging error: %v", err)
  83. }
  84. }
  85. }
  86. func spam(logger kitlog.Logger, count int) error {
  87. for i := 0; i < count; i++ {
  88. err := logger.Log("key", i)
  89. if err != nil {
  90. return err
  91. }
  92. }
  93. return nil
  94. }
  95. type mymap map[int]int
  96. func (m mymap) String() string { return "special_behavior" }