-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_file_roll_policy_test.go
96 lines (66 loc) · 1.83 KB
/
writer_file_roll_policy_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
94
95
96
package ethwal
import (
"bytes"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFileSizeRollPolicy(t *testing.T) {
var buff = bytes.NewBuffer(nil)
p := NewFileSizeRollPolicy(10)
w := writerWrapper{buff, p}
_, err := w.Write([]byte("hello"))
require.NoError(t, err)
assert.False(t, p.ShouldRoll())
_, err = w.Write([]byte(" world"))
require.NoError(t, err)
assert.True(t, p.ShouldRoll())
p.Reset()
w = writerWrapper{buff, p}
assert.False(t, p.ShouldRoll())
_, err = w.Write([]byte("hello world"))
require.NoError(t, err)
assert.True(t, p.ShouldRoll())
p.ShouldRoll()
}
func TestLastBlockNumberRollPolicy(t *testing.T) {
p := NewLastBlockNumberRollPolicy(10)
assert.False(t, p.ShouldRoll())
p.onBlockProcessed(5)
assert.False(t, p.ShouldRoll())
p.onBlockProcessed(10)
assert.True(t, p.ShouldRoll())
p.onBlockProcessed(11)
assert.False(t, p.ShouldRoll())
}
func TestTimeBasedRollPolicy(t *testing.T) {
p := NewTimeBasedRollPolicy(1500*time.Millisecond, nil)
assert.False(t, p.ShouldRoll())
time.Sleep(1500 * time.Millisecond)
assert.True(t, p.ShouldRoll())
p.Reset()
assert.False(t, p.ShouldRoll())
time.Sleep(1500 * time.Millisecond)
assert.True(t, p.ShouldRoll())
}
func TestNewFileSizeOrLastBlockNumberRollPolicy(t *testing.T) {
var buff = bytes.NewBuffer(nil)
fol := FileRollPolicies{
NewFileSizeRollPolicy(10),
NewLastBlockNumberRollPolicy(10),
}
w := writerWrapper{buff, fol}
assert.False(t, fol.ShouldRoll())
fol.onBlockProcessed(10)
assert.True(t, fol.ShouldRoll())
fol.onBlockProcessed(11)
assert.False(t, fol.ShouldRoll())
_, err := w.Write([]byte("hello world"))
require.NoError(t, err)
assert.True(t, fol.ShouldRoll())
fol.Reset()
assert.False(t, fol.ShouldRoll())
fol.onBlockProcessed(20)
assert.True(t, fol.ShouldRoll())
}