loggers_test.go 3.87 KB
Newer Older
obscuren's avatar
obscuren committed
1
package logger
zelig's avatar
zelig committed
2 3

import (
zelig's avatar
zelig committed
4
	"io/ioutil"
5
	"math/rand"
zelig's avatar
zelig committed
6
	"os"
7
	"sync"
zelig's avatar
zelig committed
8
	"testing"
9
	"time"
zelig's avatar
zelig committed
10 11 12
)

type TestLogSystem struct {
13 14
	mutex  sync.Mutex
	output string
zelig's avatar
zelig committed
15
	level  LogLevel
zelig's avatar
zelig committed
16 17
}

18
func (ls *TestLogSystem) LogPrint(msg LogMsg) {
19
	ls.mutex.Lock()
20 21 22
	if ls.level >= msg.Level() {
		ls.output += msg.String()
	}
23
	ls.mutex.Unlock()
zelig's avatar
zelig committed
24 25
}

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
func (ls *TestLogSystem) SetLogLevel(i LogLevel) {
	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)
	}
zelig's avatar
zelig committed
45 46
}

47 48 49 50 51
type blockedLogSystem struct {
	LogSystem
	unblock chan struct{}
}

52
func (ls blockedLogSystem) LogPrint(msg LogMsg) {
53
	<-ls.unblock
54
	ls.LogSystem.LogPrint(msg)
zelig's avatar
zelig committed
55 56
}

zelig's avatar
zelig committed
57
func TestLoggerFlush(t *testing.T) {
58 59
	Reset()

zelig's avatar
zelig committed
60
	logger := NewLogger("TEST")
61 62
	ls := blockedLogSystem{&TestLogSystem{level: WarnLevel}, make(chan struct{})}
	AddLogSystem(ls)
zelig's avatar
zelig committed
63
	for i := 0; i < 5; i++ {
64
		// these writes shouldn't hang even though ls is blocked
zelig's avatar
zelig committed
65 66
		logger.Errorf(".")
	}
67 68 69 70 71 72

	beforeFlush := time.Now()
	time.AfterFunc(80*time.Millisecond, func() { close(ls.unblock) })
	Flush() // this should hang for approx. 80ms
	if blockd := time.Now().Sub(beforeFlush); blockd < 80*time.Millisecond {
		t.Errorf("Flush didn't block long enough, blocked for %v, should've been >= 80ms", blockd)
zelig's avatar
zelig committed
73
	}
74 75

	ls.LogSystem.(*TestLogSystem).CheckOutput(t, "[TEST] .[TEST] .[TEST] .[TEST] .[TEST] .")
zelig's avatar
zelig committed
76 77
}

zelig's avatar
zelig committed
78
func TestLoggerPrintln(t *testing.T) {
79 80
	Reset()

zelig's avatar
zelig committed
81 82 83 84 85 86 87 88
	logger := NewLogger("TEST")
	testLogSystem := &TestLogSystem{level: WarnLevel}
	AddLogSystem(testLogSystem)
	logger.Errorln("error")
	logger.Warnln("warn")
	logger.Infoln("info")
	logger.Debugln("debug")
	Flush()
89 90

	testLogSystem.CheckOutput(t, "[TEST] error\n[TEST] warn\n")
zelig's avatar
zelig committed
91 92 93
}

func TestLoggerPrintf(t *testing.T) {
94 95
	Reset()

zelig's avatar
zelig committed
96 97 98
	logger := NewLogger("TEST")
	testLogSystem := &TestLogSystem{level: WarnLevel}
	AddLogSystem(testLogSystem)
99
	logger.Errorf("error to %v\n", []int{1, 2, 3})
100
	logger.Warnf("warn %%d %d", 5)
zelig's avatar
zelig committed
101 102 103
	logger.Infof("info")
	logger.Debugf("debug")
	Flush()
104
	testLogSystem.CheckOutput(t, "[TEST] error to [1 2 3]\n[TEST] warn %d 5")
zelig's avatar
zelig committed
105 106 107
}

func TestMultipleLogSystems(t *testing.T) {
108 109
	Reset()

zelig's avatar
zelig committed
110 111 112 113 114 115 116 117
	logger := NewLogger("TEST")
	testLogSystem0 := &TestLogSystem{level: ErrorLevel}
	testLogSystem1 := &TestLogSystem{level: WarnLevel}
	AddLogSystem(testLogSystem0)
	AddLogSystem(testLogSystem1)
	logger.Errorln("error")
	logger.Warnln("warn")
	Flush()
118 119 120

	testLogSystem0.CheckOutput(t, "[TEST] error\n")
	testLogSystem1.CheckOutput(t, "[TEST] error\n[TEST] warn\n")
zelig's avatar
zelig committed
121 122 123
}

func TestFileLogSystem(t *testing.T) {
124 125
	Reset()

zelig's avatar
zelig committed
126 127 128 129 130 131 132 133 134 135 136
	logger := NewLogger("TEST")
	filename := "test.log"
	file, _ := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
	testLogSystem := NewStdLogSystem(file, 0, WarnLevel)
	AddLogSystem(testLogSystem)
	logger.Errorf("error to %s\n", filename)
	logger.Warnln("warn")
	Flush()
	contents, _ := ioutil.ReadFile(filename)
	output := string(contents)
	if output != "[TEST] error to test.log\n[TEST] warn\n" {
zelig's avatar
zelig committed
137
		t.Error("Expected contents of file 'test.log': '[TEST] error to test.log\\n[TEST] warn\\n', got ", output)
zelig's avatar
zelig committed
138 139 140
	} else {
		os.Remove(filename)
	}
zelig's avatar
zelig committed
141 142 143
}

func TestNoLogSystem(t *testing.T) {
144 145
	Reset()

zelig's avatar
zelig committed
146 147 148
	logger := NewLogger("TEST")
	logger.Warnln("warn")
	Flush()
zelig's avatar
zelig committed
149
}
150 151 152 153 154 155 156 157 158 159 160 161

func TestConcurrentAddSystem(t *testing.T) {
	rand.Seed(time.Now().Unix())
	Reset()

	logger := NewLogger("TEST")
	stop := make(chan struct{})
	writer := func() {
		select {
		case <-stop:
			return
		default:
162
			logger.Infoln("foo")
163 164 165 166 167 168 169 170 171 172
			Flush()
		}
	}

	go writer()
	go writer()

	stopTime := time.Now().Add(100 * time.Millisecond)
	for time.Now().Before(stopTime) {
		time.Sleep(time.Duration(rand.Intn(20)) * time.Millisecond)
173
		AddLogSystem(NewStdLogSystem(ioutil.Discard, 0, InfoLevel))
174 175 176
	}
	close(stop)
}