5
log "gopkg.in/inconshreveable/log15.v2"
10
func testHandler() (log.Handler, *log.Record) {
11
rec := new(log.Record)
12
return log.FuncHandler(func(r *log.Record) error {
18
func TestHotSwapHandler(t *testing.T) {
21
h1, r1 := testHandler()
24
h := HotSwapHandler(h1)
28
if r1.Msg != "to h1" {
29
t.Fatalf("didn't get expected message to h1")
32
h2, r2 := testHandler()
35
if r2.Msg != "to h2" {
36
t.Fatalf("didn't get expected message to h2")
40
func TestSpeculativeHandler(t *testing.T) {
43
// test with an even multiple of the buffer size, less than full buffer size
44
// and not a multiple of the buffer size
45
for _, count := range []int{10000, 50, 432} {
46
recs := make(chan *log.Record)
47
done := make(chan int)
48
spec := SpeculativeHandler(100, log.ChannelHandler(recs))
52
expectedCount := int(math.Min(float64(count), float64(100)))
53
expectedIdx := count - expectedCount
55
if r.Ctx[1] != expectedIdx {
56
t.Errorf("Bad ctx 'i', got %d expected %d", r.Ctx[1], expectedIdx)
62
if expectedCount == 0 {
63
// got everything we expected
70
t.Errorf("got an extra record we shouldn't have!")
77
for i := 0; i < count; i++ {
78
lg.Debug("test speculative", "i", i)
83
// wait for the go routine to finish
88
func TestErrorHandler(t *testing.T) {
93
lg.SetHandler(EscalateErrHandler(
94
log.LvlFilterHandler(log.LvlError, h)))
96
lg.Debug("some function result", "err", nil)
98
t.Fatalf("Expected debug level message to be filtered")
101
lg.Debug("some function result", "err", errors.New("failed operation"))
102
if r.Msg != "some function result" {
103
t.Fatalf("Expected debug level message to be escalated and pass lvlfilter")
106
if r.Lvl != log.LvlError {
107
t.Fatalf("Expected debug level message to be escalated to LvlError")