-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel_atomic_test.go
93 lines (85 loc) · 2.51 KB
/
level_atomic_test.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package lager
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLevelEnablerFunc(t *testing.T) {
enabled := LevelEnablerFunc(func(l Level) bool { return l == InfoLevel })
testCases := []struct {
level Level
enabled bool
}{
{DebugLevel, false},
{InfoLevel, true},
{WarnLevel, false},
{ErrorLevel, false},
{NoneLevel, false},
{FatalLevel, false},
}
for _, tt := range testCases {
assert.Equal(t, tt.enabled, enabled.Enabled(tt.level), "unexpected result applying LevelEnablerFunc to %s", tt.level)
}
}
func TestNewAtomicLevel(t *testing.T) {
lvl := NewAtomicLevel()
assert.Equal(t, InfoLevel, lvl.Level(), "unexpected initial level.")
lvl.SetLevel(ErrorLevel)
assert.Equal(t, ErrorLevel, lvl.Level(), "unexpected level after SetLevel.")
lvl = NewAtomicLevelAt(WarnLevel)
assert.Equal(t, WarnLevel, lvl.Level(), "unexpected level after SetLevel.")
}
func TestAtomicLevelMutation(t *testing.T) {
lvl := NewAtomicLevel()
lvl.SetLevel(WarnLevel)
// Trigger races for non-atomic level mutations.
proceed := make(chan struct{})
wg := &sync.WaitGroup{}
runConcurrently(10, 100, wg, func() {
<-proceed
assert.Equal(t, WarnLevel, lvl.Level())
})
runConcurrently(10, 100, wg, func() {
<-proceed
lvl.SetLevel(WarnLevel)
})
close(proceed)
wg.Wait()
}
func TestAtomicLevelText(t *testing.T) {
testCases := []struct {
text string
expect Level
err bool
}{
{"debug", DebugLevel, false},
{"info", InfoLevel, false},
{"", InfoLevel, false},
{"warn", WarnLevel, false},
{"error", ErrorLevel, false},
{"none", NoneLevel, false},
{"fatal", FatalLevel, false},
{"foobar", NoneLevel, true},
}
for _, tt := range testCases {
var lvl AtomicLevel
// Test both initial unmarshalling and overwriting existing value.
for i := 0; i < 2; i++ {
if tt.err {
assert.Error(t, lvl.UnmarshalText([]byte(tt.text)), "expected unmarshalling %q to fail.", tt.text)
} else {
assert.NoError(t, lvl.UnmarshalText([]byte(tt.text)), "expected unmarshalling %q to succeed.", tt.text)
}
assert.Equal(t, tt.expect, lvl.Level(), "unexpected level after unmarshalling.")
lvl.SetLevel(NoneLevel)
}
// Test marshalling
if tt.text != "" && !tt.err {
lvl.SetLevel(tt.expect)
marshaled, err := lvl.MarshalText()
assert.NoError(t, err, `unexpected error marshalling level "%v" to text.`, tt.expect)
assert.Equal(t, tt.text, string(marshaled), "expected marshaled text to match")
assert.Equal(t, tt.text, lvl.String(), "expected Stringer call to match")
}
}
}