-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtime_rotate_test.go
65 lines (55 loc) · 1.8 KB
/
time_rotate_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
package writer_test
import (
"errors"
"io"
"os"
"testing"
"time"
"github.com/agiledragon/gomonkey/v2"
"github.com/gol4ng/logger/mocks"
"github.com/gol4ng/logger/writer"
"github.com/stretchr/testify/assert"
)
func TestTimeRotateWriter_StartWithError(t *testing.T) {
tickerChan := make(chan time.Time, 1)
patch := gomonkey.NewPatches()
patch.ApplyFunc(time.NewTicker, func(d time.Duration) *time.Ticker {
assert.Equal(t, 50*time.Millisecond, d)
return &time.Ticker{
C: tickerChan,
}
})
defer patch.Reset()
mockRotateWriter := mocks.RotateWriter{}
mockRotateWriter.On("Rotate").Return(func() error { return errors.New("fake_rotate_error") })
tr := writer.TimeRotateWriter{
RotateWriter: &mockRotateWriter,
Interval: 50 * time.Millisecond,
PanicHandler: func(err error) {
assert.EqualError(t, err, "fake_rotate_error")
},
}
tr.Start()
mockRotateWriter.AssertNotCalled(t, "Rotate")
tickerChan <- time.Now()
time.Sleep(1 * time.Millisecond) //we have to wait a bit to be sure Rotate call has been made
mockRotateWriter.AssertCalled(t, "Rotate")
}
func TestNewTimeRotateWriter(t *testing.T) {
mockRotateWriter := mocks.RotateWriter{}
mockRotateWriter.On("Rotate").Return(func() error { return nil })
writer.NewTimeRotateWriter(&mockRotateWriter, 50*time.Millisecond)
mockRotateWriter.AssertNotCalled(t, "Rotate")
}
func TestNewTimeRotateFileWriter_WithError(t *testing.T) {
_, err := writer.NewTimeRotateFromProvider(func(io.Writer) (io.Writer, error) {
return &os.File{}, errors.New("fake_file_provider_error")
}, 1*time.Second)
assert.EqualError(t, err, "fake_file_provider_error")
}
func TestNewTimeRotateFileWriter(t *testing.T) {
_, err := writer.NewTimeRotateFromProvider(func(io.Writer) (io.Writer, error) {
return &os.File{}, nil
}, 1*time.Second)
assert.Nil(t, err)
}