loggers_test.go 3.9 KB

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