-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiskwriter_test.go
More file actions
79 lines (59 loc) · 2.31 KB
/
diskwriter_test.go
File metadata and controls
79 lines (59 loc) · 2.31 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
70
71
72
73
74
75
76
77
78
79
package logmanager
import (
"os"
"path"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDiskWriter(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
marker1 := "yeahhhhh boiiiii"
marker2 := "than you, far too kind"
marker3 := "i am potato"
logPath := path.Join(t.TempDir(), "logfile.log")
writer := NewDiskWriter(logPath, DiskWriterConfig{
RotateDuration: time.Millisecond,
MaximumLogFiles: 3,
})
writer.Log(Info, ColorTheme{}, "logmanager", "diskwriter_test.go", 32, time.Now(), "whatmahnameeee")
<-time.After(time.Millisecond * 10)
_, err := os.Stat(logPath)
require.NoError(err, "log file should exist after logging")
writer.Close()
writer = NewDiskWriter(logPath, DiskWriterConfig{
RotateDuration: time.Millisecond,
MaximumLogFiles: 3,
})
<-time.After(time.Millisecond * 10)
writer.Log(Info, ColorTheme{}, "logmanager", "diskwriter_test.go", 45, time.Now(), marker1)
<-time.After(time.Millisecond * 10)
_, err = os.Stat(logPath)
assert.NoError(err, "log file should still exist after a second writer is created")
_, err = os.Stat(logPath + ".1")
require.NoError(err, "logfile should now have rotated once")
writer.Log(Info, ColorTheme{}, "logmanager", "diskwriter_test.go", 54, time.Now(), marker2)
<-time.After(time.Millisecond * 10)
_, err = os.Stat(logPath)
assert.NoError(err, "log file should still exist after a rotation")
_, err = os.Stat(logPath + ".1")
assert.NoError(err, "second log file should still exist")
_, err = os.Stat(logPath + ".2")
require.NoError(err, "third log file should now exist")
writer.Log(Info, ColorTheme{}, "logmanager", "diskwriter_test.go", 66, time.Now(), marker3)
<-time.After(time.Millisecond * 10)
_, err = os.Stat(logPath + ".3")
require.Error(err, "after the third rotation we hit max log files (3), .3 should not exist")
writer.Close()
all, err := os.ReadFile(logPath)
require.NoError(err)
assert.Contains(string(all), marker3, "logfile should contain last written message")
all, err = os.ReadFile(logPath + ".1")
require.NoError(err)
assert.Contains(string(all), marker2, "logfile.1 should contain the correct message")
all, err = os.ReadFile(logPath + ".2")
require.NoError(err)
assert.Contains(string(all), marker1, "logfile.2 should contain the correct message")
}