ethlog: improve TestLogSystem
It's now safe for concurrent access. Output checking looks better.
This commit is contained in:
parent
45d1052229
commit
ec132749aa
@ -5,29 +5,50 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TestLogSystem struct {
|
type TestLogSystem struct {
|
||||||
Output string
|
mutex sync.Mutex
|
||||||
|
output string
|
||||||
level LogLevel
|
level LogLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TestLogSystem) Println(v ...interface{}) {
|
func (ls *TestLogSystem) Println(v ...interface{}) {
|
||||||
t.Output += fmt.Sprintln(v...)
|
ls.mutex.Lock()
|
||||||
|
ls.output += fmt.Sprintln(v...)
|
||||||
|
ls.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TestLogSystem) Printf(format string, v ...interface{}) {
|
func (ls *TestLogSystem) Printf(format string, v ...interface{}) {
|
||||||
t.Output += fmt.Sprintf(format, v...)
|
ls.mutex.Lock()
|
||||||
|
ls.output += fmt.Sprintf(format, v...)
|
||||||
|
ls.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TestLogSystem) SetLogLevel(i LogLevel) {
|
func (ls *TestLogSystem) SetLogLevel(i LogLevel) {
|
||||||
t.level = i
|
ls.mutex.Lock()
|
||||||
|
ls.level = i
|
||||||
|
ls.mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ls *TestLogSystem) GetLogLevel() LogLevel {
|
||||||
|
ls.mutex.Lock()
|
||||||
|
defer ls.mutex.Unlock()
|
||||||
|
return ls.level
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ls *TestLogSystem) CheckOutput(t *testing.T, expected string) {
|
||||||
|
ls.mutex.Lock()
|
||||||
|
output := ls.output
|
||||||
|
ls.mutex.Unlock()
|
||||||
|
if output != expected {
|
||||||
|
t.Errorf("log output mismatch:\n got: %q\n want: %q\n", output, expected)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TestLogSystem) GetLogLevel() LogLevel {
|
|
||||||
return t.level
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoggerFlush(t *testing.T) {
|
func TestLoggerFlush(t *testing.T) {
|
||||||
@ -57,10 +78,8 @@ func TestLoggerPrintln(t *testing.T) {
|
|||||||
logger.Infoln("info")
|
logger.Infoln("info")
|
||||||
logger.Debugln("debug")
|
logger.Debugln("debug")
|
||||||
Flush()
|
Flush()
|
||||||
output := testLogSystem.Output
|
|
||||||
if output != "[TEST] error\n[TEST] warn\n" {
|
testLogSystem.CheckOutput(t, "[TEST] error\n[TEST] warn\n")
|
||||||
t.Error("Expected logger output '[TEST] error\\n[TEST] warn\\n', got ", output)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoggerPrintf(t *testing.T) {
|
func TestLoggerPrintf(t *testing.T) {
|
||||||
@ -69,15 +88,12 @@ func TestLoggerPrintf(t *testing.T) {
|
|||||||
logger := NewLogger("TEST")
|
logger := NewLogger("TEST")
|
||||||
testLogSystem := &TestLogSystem{level: WarnLevel}
|
testLogSystem := &TestLogSystem{level: WarnLevel}
|
||||||
AddLogSystem(testLogSystem)
|
AddLogSystem(testLogSystem)
|
||||||
logger.Errorf("error to %v\n", *testLogSystem)
|
logger.Errorf("error to %v\n", []int{1, 2, 3})
|
||||||
logger.Warnf("warn")
|
logger.Warnf("warn")
|
||||||
logger.Infof("info")
|
logger.Infof("info")
|
||||||
logger.Debugf("debug")
|
logger.Debugf("debug")
|
||||||
Flush()
|
Flush()
|
||||||
output := testLogSystem.Output
|
testLogSystem.CheckOutput(t, "[TEST] error to [1 2 3]\n[TEST] warn")
|
||||||
if output != "[TEST] error to { 2}\n[TEST] warn" {
|
|
||||||
t.Error("Expected logger output '[TEST] error to { 2}\\n[TEST] warn', got ", output)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMultipleLogSystems(t *testing.T) {
|
func TestMultipleLogSystems(t *testing.T) {
|
||||||
@ -91,14 +107,9 @@ func TestMultipleLogSystems(t *testing.T) {
|
|||||||
logger.Errorln("error")
|
logger.Errorln("error")
|
||||||
logger.Warnln("warn")
|
logger.Warnln("warn")
|
||||||
Flush()
|
Flush()
|
||||||
output0 := testLogSystem0.Output
|
|
||||||
output1 := testLogSystem1.Output
|
testLogSystem0.CheckOutput(t, "[TEST] error\n")
|
||||||
if output0 != "[TEST] error\n" {
|
testLogSystem1.CheckOutput(t, "[TEST] error\n[TEST] warn\n")
|
||||||
t.Error("Expected logger 0 output '[TEST] error\\n', got ", output0)
|
|
||||||
}
|
|
||||||
if output1 != "[TEST] error\n[TEST] warn\n" {
|
|
||||||
t.Error("Expected logger 1 output '[TEST] error\\n[TEST] warn\\n', got ", output1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFileLogSystem(t *testing.T) {
|
func TestFileLogSystem(t *testing.T) {
|
||||||
@ -140,7 +151,7 @@ func TestConcurrentAddSystem(t *testing.T) {
|
|||||||
case <-stop:
|
case <-stop:
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
logger.Infof("foo")
|
logger.Infoln("foo")
|
||||||
Flush()
|
Flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user