debug_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package debug
  2. import "testing"
  3. import "strings"
  4. import "bytes"
  5. import "time"
  6. func assertContains(t *testing.T, str, substr string) {
  7. if !strings.Contains(str, substr) {
  8. t.Fatalf("expected %q to contain %q", str, substr)
  9. }
  10. }
  11. func assertNotContains(t *testing.T, str, substr string) {
  12. if strings.Contains(str, substr) {
  13. t.Fatalf("expected %q to not contain %q", str, substr)
  14. }
  15. }
  16. func TestDefault(t *testing.T) {
  17. var b []byte
  18. buf := bytes.NewBuffer(b)
  19. SetWriter(buf)
  20. debug := Debug("foo")
  21. debug("something")
  22. debug("here")
  23. debug("whoop")
  24. if buf.Len() != 0 {
  25. t.Fatalf("buffer should be empty")
  26. }
  27. }
  28. func TestEnable(t *testing.T) {
  29. var b []byte
  30. buf := bytes.NewBuffer(b)
  31. SetWriter(buf)
  32. Enable("foo")
  33. debug := Debug("foo")
  34. debug("something")
  35. debug("here")
  36. debug("whoop")
  37. if buf.Len() == 0 {
  38. t.Fatalf("buffer should have output")
  39. }
  40. str := string(buf.Bytes())
  41. assertContains(t, str, "something")
  42. assertContains(t, str, "here")
  43. assertContains(t, str, "whoop")
  44. }
  45. func TestMultipleOneEnabled(t *testing.T) {
  46. var b []byte
  47. buf := bytes.NewBuffer(b)
  48. SetWriter(buf)
  49. Enable("foo")
  50. foo := Debug("foo")
  51. foo("foo")
  52. bar := Debug("bar")
  53. bar("bar")
  54. if buf.Len() == 0 {
  55. t.Fatalf("buffer should have output")
  56. }
  57. str := string(buf.Bytes())
  58. assertContains(t, str, "foo")
  59. assertNotContains(t, str, "bar")
  60. }
  61. func TestMultipleEnabled(t *testing.T) {
  62. var b []byte
  63. buf := bytes.NewBuffer(b)
  64. SetWriter(buf)
  65. Enable("foo,bar")
  66. foo := Debug("foo")
  67. foo("foo")
  68. bar := Debug("bar")
  69. bar("bar")
  70. if buf.Len() == 0 {
  71. t.Fatalf("buffer should have output")
  72. }
  73. str := string(buf.Bytes())
  74. assertContains(t, str, "foo")
  75. assertContains(t, str, "bar")
  76. }
  77. func TestEnableDisable(t *testing.T) {
  78. var b []byte
  79. buf := bytes.NewBuffer(b)
  80. SetWriter(buf)
  81. Enable("foo,bar")
  82. Disable()
  83. foo := Debug("foo")
  84. foo("foo")
  85. bar := Debug("bar")
  86. bar("bar")
  87. if buf.Len() != 0 {
  88. t.Fatalf("buffer should not have output")
  89. }
  90. }
  91. func ExampleEnable() {
  92. Enable("mongo:connection")
  93. Enable("mongo:*")
  94. Enable("foo,bar,baz")
  95. Enable("*")
  96. }
  97. func ExampleDebug() {
  98. var debug = Debug("single")
  99. for {
  100. debug("sending mail")
  101. debug("send email to %s", "tobi@segment.io")
  102. debug("send email to %s", "loki@segment.io")
  103. debug("send email to %s", "jane@segment.io")
  104. time.Sleep(500 * time.Millisecond)
  105. }
  106. }
  107. func BenchmarkDisabled(b *testing.B) {
  108. debug := Debug("something")
  109. for i := 0; i < b.N; i++ {
  110. debug("stuff")
  111. }
  112. }
  113. func BenchmarkNonMatch(b *testing.B) {
  114. debug := Debug("something")
  115. Enable("nonmatch")
  116. for i := 0; i < b.N; i++ {
  117. debug("stuff")
  118. }
  119. }