loggers_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package logger
  17. import (
  18. "io/ioutil"
  19. "math/rand"
  20. "os"
  21. "sync"
  22. "testing"
  23. "time"
  24. )
  25. type TestLogSystem struct {
  26. mutex sync.Mutex
  27. output string
  28. level LogLevel
  29. }
  30. func (ls *TestLogSystem) LogPrint(msg LogMsg) {
  31. ls.mutex.Lock()
  32. if ls.level >= msg.Level() {
  33. ls.output += msg.String()
  34. }
  35. ls.mutex.Unlock()
  36. }
  37. func (ls *TestLogSystem) SetLogLevel(i LogLevel) {
  38. ls.mutex.Lock()
  39. ls.level = i
  40. ls.mutex.Unlock()
  41. }
  42. func (ls *TestLogSystem) GetLogLevel() LogLevel {
  43. ls.mutex.Lock()
  44. defer ls.mutex.Unlock()
  45. return ls.level
  46. }
  47. func (ls *TestLogSystem) CheckOutput(t *testing.T, expected string) {
  48. ls.mutex.Lock()
  49. output := ls.output
  50. ls.mutex.Unlock()
  51. if output != expected {
  52. t.Errorf("log output mismatch:\n got: %q\n want: %q\n", output, expected)
  53. }
  54. }
  55. type blockedLogSystem struct {
  56. LogSystem
  57. unblock chan struct{}
  58. }
  59. func (ls blockedLogSystem) LogPrint(msg LogMsg) {
  60. <-ls.unblock
  61. ls.LogSystem.LogPrint(msg)
  62. }
  63. func TestLoggerFlush(t *testing.T) {
  64. Reset()
  65. logger := NewLogger("TEST")
  66. ls := blockedLogSystem{&TestLogSystem{level: WarnLevel}, make(chan struct{})}
  67. AddLogSystem(ls)
  68. for i := 0; i < 5; i++ {
  69. // these writes shouldn't hang even though ls is blocked
  70. logger.Errorf(".")
  71. }
  72. beforeFlush := time.Now()
  73. time.AfterFunc(80*time.Millisecond, func() { close(ls.unblock) })
  74. Flush() // this should hang for approx. 80ms
  75. if blockd := time.Now().Sub(beforeFlush); blockd < 80*time.Millisecond {
  76. t.Errorf("Flush didn't block long enough, blocked for %v, should've been >= 80ms", blockd)
  77. }
  78. ls.LogSystem.(*TestLogSystem).CheckOutput(t, "[TEST] .[TEST] .[TEST] .[TEST] .[TEST] .")
  79. }
  80. func TestLoggerPrintln(t *testing.T) {
  81. Reset()
  82. logger := NewLogger("TEST")
  83. testLogSystem := &TestLogSystem{level: WarnLevel}
  84. AddLogSystem(testLogSystem)
  85. logger.Errorln("error")
  86. logger.Warnln("warn")
  87. logger.Infoln("info")
  88. logger.Debugln("debug")
  89. Flush()
  90. testLogSystem.CheckOutput(t, "[TEST] error\n[TEST] warn\n")
  91. }
  92. func TestLoggerPrintf(t *testing.T) {
  93. Reset()
  94. logger := NewLogger("TEST")
  95. testLogSystem := &TestLogSystem{level: WarnLevel}
  96. AddLogSystem(testLogSystem)
  97. logger.Errorf("error to %v\n", []int{1, 2, 3})
  98. logger.Warnf("warn %%d %d", 5)
  99. logger.Infof("info")
  100. logger.Debugf("debug")
  101. Flush()
  102. testLogSystem.CheckOutput(t, "[TEST] error to [1 2 3]\n[TEST] warn %d 5")
  103. }
  104. func TestMultipleLogSystems(t *testing.T) {
  105. Reset()
  106. logger := NewLogger("TEST")
  107. testLogSystem0 := &TestLogSystem{level: ErrorLevel}
  108. testLogSystem1 := &TestLogSystem{level: WarnLevel}
  109. AddLogSystem(testLogSystem0)
  110. AddLogSystem(testLogSystem1)
  111. logger.Errorln("error")
  112. logger.Warnln("warn")
  113. Flush()
  114. testLogSystem0.CheckOutput(t, "[TEST] error\n")
  115. testLogSystem1.CheckOutput(t, "[TEST] error\n[TEST] warn\n")
  116. }
  117. func TestFileLogSystem(t *testing.T) {
  118. Reset()
  119. logger := NewLogger("TEST")
  120. filename := "test.log"
  121. file, _ := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
  122. testLogSystem := NewStdLogSystem(file, 0, WarnLevel)
  123. AddLogSystem(testLogSystem)
  124. logger.Errorf("error to %s\n", filename)
  125. logger.Warnln("warn")
  126. Flush()
  127. contents, _ := ioutil.ReadFile(filename)
  128. output := string(contents)
  129. if output != "[TEST] error to test.log\n[TEST] warn\n" {
  130. t.Error("Expected contents of file 'test.log': '[TEST] error to test.log\\n[TEST] warn\\n', got ", output)
  131. } else {
  132. os.Remove(filename)
  133. }
  134. }
  135. func TestNoLogSystem(t *testing.T) {
  136. Reset()
  137. logger := NewLogger("TEST")
  138. logger.Warnln("warn")
  139. Flush()
  140. }
  141. func TestConcurrentAddSystem(t *testing.T) {
  142. rand.Seed(time.Now().Unix())
  143. Reset()
  144. logger := NewLogger("TEST")
  145. stop := make(chan struct{})
  146. writer := func() {
  147. select {
  148. case <-stop:
  149. return
  150. default:
  151. logger.Infoln("foo")
  152. Flush()
  153. }
  154. }
  155. go writer()
  156. go writer()
  157. stopTime := time.Now().Add(100 * time.Millisecond)
  158. for time.Now().Before(stopTime) {
  159. time.Sleep(time.Duration(rand.Intn(20)) * time.Millisecond)
  160. AddLogSystem(NewStdLogSystem(ioutil.Discard, 0, InfoLevel))
  161. }
  162. close(stop)
  163. }