-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathlog_hook_test.go
More file actions
69 lines (60 loc) · 1.55 KB
/
log_hook_test.go
File metadata and controls
69 lines (60 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package pgengine
import (
"context"
"errors"
"testing"
"time"
"github.com/pashagolub/pgxmock/v5"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestLogHook(t *testing.T) {
var h LogHook
mockPool, err := pgxmock.NewPool()
assert.NoError(t, err)
func() { //fake NewHook
h = LogHook{ctx: context.Background(),
db: mockPool,
cacheLimit: 2,
cacheTimeout: time.Second,
input: make(chan logrus.Entry, 2),
level: "debug",
}
go h.poll(h.input)
}()
entries := []logrus.Entry{ //2 entries by cacheLimit and 1 by cacheTimeout
{Level: logrus.DebugLevel},
{Level: logrus.InfoLevel},
{Level: logrus.ErrorLevel},
{Level: logrus.FatalLevel},
{Level: 42},
}
for _, e := range entries {
_ = h.Fire(&e)
_ = adaptEntryLevel(e.Level)
}
levels := []string{"debug", "info", "warn", "fatal", "foobar"}
for _, level := range levels {
h.level = level
assert.NotEmpty(t, h.Levels())
}
h.level = "none"
assert.Empty(t, h.Levels())
<-time.After(time.Second)
}
func TestCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
h := NewHook(ctx, &PgEngine{}, "debug")
assert.Equal(t, h.Levels(), logrus.AllLevels)
assert.NoError(t, h.Fire(&logrus.Entry{}))
}
func TestFireError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h := NewHook(ctx, &PgEngine{}, "debug")
err := errors.New("fire error")
go func() { h.lastError <- err }()
<-time.After(time.Second)
assert.Equal(t, err, h.Fire(&logrus.Entry{}))
}