loggers_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package logger
  2. import (
  3. "io/ioutil"
  4. "math/rand"
  5. "os"
  6. "sync"
  7. "testing"
  8. "time"
  9. )
  10. type TestLogSystem struct {
  11. mutex sync.Mutex
  12. output string
  13. level LogLevel
  14. }
  15. func (ls *TestLogSystem) LogPrint(level LogLevel, msg string) {
  16. ls.mutex.Lock()
  17. ls.output += msg
  18. ls.mutex.Unlock()
  19. }
  20. func (ls *TestLogSystem) SetLogLevel(i LogLevel) {
  21. ls.mutex.Lock()
  22. ls.level = i
  23. ls.mutex.Unlock()
  24. }
  25. func (ls *TestLogSystem) GetLogLevel() LogLevel {
  26. ls.mutex.Lock()
  27. defer ls.mutex.Unlock()
  28. return ls.level
  29. }
  30. func (ls *TestLogSystem) CheckOutput(t *testing.T, expected string) {
  31. ls.mutex.Lock()
  32. output := ls.output
  33. ls.mutex.Unlock()
  34. if output != expected {
  35. t.Errorf("log output mismatch:\n got: %q\n want: %q\n", output, expected)
  36. }
  37. }
  38. type blockedLogSystem struct {
  39. LogSystem
  40. unblock chan struct{}
  41. }
  42. func (ls blockedLogSystem) LogPrint(level LogLevel, msg string) {
  43. <-ls.unblock
  44. ls.LogSystem.LogPrint(level, msg)
  45. }
  46. func TestLoggerFlush(t *testing.T) {
  47. Reset()
  48. logger := NewLogger("TEST")
  49. ls := blockedLogSystem{&TestLogSystem{level: WarnLevel}, make(chan struct{})}
  50. AddLogSystem(ls)
  51. for i := 0; i < 5; i++ {
  52. // these writes shouldn't hang even though ls is blocked
  53. logger.Errorf(".")
  54. }
  55. beforeFlush := time.Now()
  56. time.AfterFunc(80*time.Millisecond, func() { close(ls.unblock) })
  57. Flush() // this should hang for approx. 80ms
  58. if blockd := time.Now().Sub(beforeFlush); blockd < 80*time.Millisecond {
  59. t.Errorf("Flush didn't block long enough, blocked for %v, should've been >= 80ms", blockd)
  60. }
  61. ls.LogSystem.(*TestLogSystem).CheckOutput(t, "[TEST] .[TEST] .[TEST] .[TEST] .[TEST] .")
  62. }
  63. func TestLoggerPrintln(t *testing.T) {
  64. Reset()
  65. logger := NewLogger("TEST")
  66. testLogSystem := &TestLogSystem{level: WarnLevel}
  67. AddLogSystem(testLogSystem)
  68. logger.Errorln("error")
  69. logger.Warnln("warn")
  70. logger.Infoln("info")
  71. logger.Debugln("debug")
  72. Flush()
  73. testLogSystem.CheckOutput(t, "[TEST] error\n[TEST] warn\n")
  74. }
  75. func TestLoggerPrintf(t *testing.T) {
  76. Reset()
  77. logger := NewLogger("TEST")
  78. testLogSystem := &TestLogSystem{level: WarnLevel}
  79. AddLogSystem(testLogSystem)
  80. logger.Errorf("error to %v\n", []int{1, 2, 3})
  81. logger.Warnf("warn %%d %d", 5)
  82. logger.Infof("info")
  83. logger.Debugf("debug")
  84. Flush()
  85. testLogSystem.CheckOutput(t, "[TEST] error to [1 2 3]\n[TEST] warn %d 5")
  86. }
  87. func TestMultipleLogSystems(t *testing.T) {
  88. Reset()
  89. logger := NewLogger("TEST")
  90. testLogSystem0 := &TestLogSystem{level: ErrorLevel}
  91. testLogSystem1 := &TestLogSystem{level: WarnLevel}
  92. AddLogSystem(testLogSystem0)
  93. AddLogSystem(testLogSystem1)
  94. logger.Errorln("error")
  95. logger.Warnln("warn")
  96. Flush()
  97. testLogSystem0.CheckOutput(t, "[TEST] error\n")
  98. testLogSystem1.CheckOutput(t, "[TEST] error\n[TEST] warn\n")
  99. }
  100. func TestFileLogSystem(t *testing.T) {
  101. Reset()
  102. logger := NewLogger("TEST")
  103. filename := "test.log"
  104. file, _ := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
  105. testLogSystem := NewStdLogSystem(file, 0, WarnLevel)
  106. AddLogSystem(testLogSystem)
  107. logger.Errorf("error to %s\n", filename)
  108. logger.Warnln("warn")
  109. Flush()
  110. contents, _ := ioutil.ReadFile(filename)
  111. output := string(contents)
  112. if output != "[TEST] error to test.log\n[TEST] warn\n" {
  113. t.Error("Expected contents of file 'test.log': '[TEST] error to test.log\\n[TEST] warn\\n', got ", output)
  114. } else {
  115. os.Remove(filename)
  116. }
  117. }
  118. func TestNoLogSystem(t *testing.T) {
  119. Reset()
  120. logger := NewLogger("TEST")
  121. logger.Warnln("warn")
  122. Flush()
  123. }
  124. func TestConcurrentAddSystem(t *testing.T) {
  125. rand.Seed(time.Now().Unix())
  126. Reset()
  127. logger := NewLogger("TEST")
  128. stop := make(chan struct{})
  129. writer := func() {
  130. select {
  131. case <-stop:
  132. return
  133. default:
  134. logger.Infoln("foo")
  135. Flush()
  136. }
  137. }
  138. go writer()
  139. go writer()
  140. stopTime := time.Now().Add(100 * time.Millisecond)
  141. for time.Now().Before(stopTime) {
  142. time.Sleep(time.Duration(rand.Intn(20)) * time.Millisecond)
  143. AddLogSystem(NewStdLogSystem(ioutil.Discard, 0, InfoLevel))
  144. }
  145. close(stop)
  146. }