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.

116 lines
2.7 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. err := logger.Log("std_map", map[int]int{1: 2}, "my_map", mymap{0: 0})
  28. assert.NotNil(t, err)
  29. buf.Reset()
  30. if err := logger.Log("level", "error"); err != nil {
  31. t.Fatal(err)
  32. }
  33. assert.Regexp(t, regexp.MustCompile(`E\[.+\] unknown \s+\n$`), buf.String())
  34. buf.Reset()
  35. if err := logger.Log("_msg", "Hello"); err != nil {
  36. t.Fatal(err)
  37. }
  38. assert.Regexp(t, regexp.MustCompile(`N\[.+\] Hello \s+\n$`), buf.String())
  39. buf.Reset()
  40. if err := logger.Log("module", "main", "module", "crypto", "module", "wire"); err != nil {
  41. t.Fatal(err)
  42. }
  43. assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+module=wire\s+\n$`), buf.String())
  44. }
  45. func BenchmarkTMFmtLoggerSimple(b *testing.B) {
  46. benchmarkRunnerKitlog(b, log.NewTMFmtLogger(ioutil.Discard), baseMessage)
  47. }
  48. func BenchmarkTMFmtLoggerContextual(b *testing.B) {
  49. benchmarkRunnerKitlog(b, log.NewTMFmtLogger(ioutil.Discard), withMessage)
  50. }
  51. func TestTMFmtLoggerConcurrency(t *testing.T) {
  52. t.Parallel()
  53. testConcurrency(t, log.NewTMFmtLogger(ioutil.Discard), 10000)
  54. }
  55. func benchmarkRunnerKitlog(b *testing.B, logger kitlog.Logger, f func(kitlog.Logger)) {
  56. lc := kitlog.With(logger, "common_key", "common_value")
  57. b.ReportAllocs()
  58. b.ResetTimer()
  59. for i := 0; i < b.N; i++ {
  60. f(lc)
  61. }
  62. }
  63. var (
  64. baseMessage = func(logger kitlog.Logger) { logger.Log("foo_key", "foo_value") }
  65. withMessage = func(logger kitlog.Logger) { kitlog.With(logger, "a", "b").Log("d", "f") }
  66. )
  67. // These test are designed to be run with the race detector.
  68. func testConcurrency(t *testing.T, logger kitlog.Logger, total int) {
  69. n := int(math.Sqrt(float64(total)))
  70. share := total / n
  71. errC := make(chan error, n)
  72. for i := 0; i < n; i++ {
  73. go func() {
  74. errC <- spam(logger, share)
  75. }()
  76. }
  77. for i := 0; i < n; i++ {
  78. err := <-errC
  79. if err != nil {
  80. t.Fatalf("concurrent logging error: %v", err)
  81. }
  82. }
  83. }
  84. func spam(logger kitlog.Logger, count int) error {
  85. for i := 0; i < count; i++ {
  86. err := logger.Log("key", i)
  87. if err != nil {
  88. return err
  89. }
  90. }
  91. return nil
  92. }
  93. type mymap map[int]int
  94. func (m mymap) String() string { return "special_behavior" }