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.

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