forked from netdata/go.d.plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
countwatcher_test.go
59 lines (44 loc) · 1.08 KB
/
countwatcher_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
// SPDX-License-Identifier: GPL-3.0-or-later
package logger
import (
"io"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMsgCountWatcher_Register(t *testing.T) {
cw := newMsgCountWatcher(time.Second)
defer cw.stop()
require.Len(t, cw.items, 0)
logger := New("", "")
cw.Register(logger)
require.Len(t, cw.items, 1)
require.Equal(t, logger, cw.items[logger.id])
}
func TestMsgCountWatcher_Unregister(t *testing.T) {
cw := newMsgCountWatcher(time.Second)
defer cw.stop()
require.Len(t, cw.items, 0)
logger := New("", "")
cw.items[logger.id] = logger
cw.Unregister(logger)
require.Len(t, cw.items, 0)
}
func TestMsgCountWatcher(t *testing.T) {
reset := time.Millisecond * 500
cw := newMsgCountWatcher(reset)
defer cw.stop()
logger := New("", "")
logger.limited = true
logger.formatter.SetOutput(io.Discard)
cw.Register(logger)
for i := 0; i < 3; i++ {
for m := 0; m < 100; m++ {
logger.Info()
}
time.Sleep(reset * 2)
assert.Equal(t, int64(0), atomic.LoadInt64(&logger.msgCount))
}
}