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.

112 lines
3.1 KiB

8 years ago
8 years ago
  1. package log_test
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "strings"
  6. "testing"
  7. "github.com/tendermint/tendermint/libs/log"
  8. )
  9. func TestLoggerLogsItsErrors(t *testing.T) {
  10. var buf bytes.Buffer
  11. logger := log.NewTMLogger(&buf)
  12. logger.Info("foo", "baz baz", "bar")
  13. msg := strings.TrimSpace(buf.String())
  14. if !strings.Contains(msg, "foo") {
  15. t.Errorf("expected logger msg to contain ErrInvalidKey, got %s", msg)
  16. }
  17. }
  18. func TestInfo(t *testing.T) {
  19. var bufInfo bytes.Buffer
  20. l := log.NewTMLogger(&bufInfo)
  21. l.Info("Client initialized with old header (trusted is more recent)",
  22. "old", 42,
  23. "trustedHeight", "forty two",
  24. "trustedHash", []byte("test me"))
  25. msg := strings.TrimSpace(bufInfo.String())
  26. // Remove the timestamp information to allow
  27. // us to test against the expected message.
  28. receivedmsg := strings.Split(msg, "] ")[1]
  29. const expectedmsg = `Client initialized with old header
  30. (trusted is more recent) old=42 trustedHeight="forty two"
  31. trustedHash=74657374206D65`
  32. if strings.EqualFold(receivedmsg, expectedmsg) {
  33. t.Fatalf("received %s, expected %s", receivedmsg, expectedmsg)
  34. }
  35. }
  36. func TestDebug(t *testing.T) {
  37. var bufDebug bytes.Buffer
  38. ld := log.NewTMLogger(&bufDebug)
  39. ld.Debug("Client initialized with old header (trusted is more recent)",
  40. "old", 42,
  41. "trustedHeight", "forty two",
  42. "trustedHash", []byte("test me"))
  43. msg := strings.TrimSpace(bufDebug.String())
  44. // Remove the timestamp information to allow
  45. // us to test against the expected message.
  46. receivedmsg := strings.Split(msg, "] ")[1]
  47. const expectedmsg = `Client initialized with old header
  48. (trusted is more recent) old=42 trustedHeight="forty two"
  49. trustedHash=74657374206D65`
  50. if strings.EqualFold(receivedmsg, expectedmsg) {
  51. t.Fatalf("received %s, expected %s", receivedmsg, expectedmsg)
  52. }
  53. }
  54. func TestError(t *testing.T) {
  55. var bufErr bytes.Buffer
  56. le := log.NewTMLogger(&bufErr)
  57. le.Error("Client initialized with old header (trusted is more recent)",
  58. "old", 42,
  59. "trustedHeight", "forty two",
  60. "trustedHash", []byte("test me"))
  61. msg := strings.TrimSpace(bufErr.String())
  62. // Remove the timestamp information to allow
  63. // us to test against the expected message.
  64. receivedmsg := strings.Split(msg, "] ")[1]
  65. const expectedmsg = `Client initialized with old header
  66. (trusted is more recent) old=42 trustedHeight="forty two"
  67. trustedHash=74657374206D65`
  68. if strings.EqualFold(receivedmsg, expectedmsg) {
  69. t.Fatalf("received %s, expected %s", receivedmsg, expectedmsg)
  70. }
  71. }
  72. func BenchmarkTMLoggerSimple(b *testing.B) {
  73. benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), baseInfoMessage)
  74. }
  75. func BenchmarkTMLoggerContextual(b *testing.B) {
  76. benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), withInfoMessage)
  77. }
  78. func benchmarkRunner(b *testing.B, logger log.Logger, f func(log.Logger)) {
  79. lc := logger.With("common_key", "common_value")
  80. b.ReportAllocs()
  81. b.ResetTimer()
  82. for i := 0; i < b.N; i++ {
  83. f(lc)
  84. }
  85. }
  86. var (
  87. baseInfoMessage = func(logger log.Logger) { logger.Info("foo_message", "foo_key", "foo_value") }
  88. withInfoMessage = func(logger log.Logger) { logger.With("a", "b").Info("c", "d", "f") }
  89. )