forked from natefinch/lumberjack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotate_test.go
71 lines (57 loc) · 1.27 KB
/
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
66
67
68
69
70
71
package woodcutter
import (
"io/fs"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
)
// Example of how to rotate in response to SIGHUP.
func TestRotate_RotateOnSigHup(t *testing.T) {
currentTime = fakeTime
newUUID = fakeUUID
cwd := t.TempDir()
logfilepath := logFile(cwd)
l := &Logger{
Filename: logfilepath,
}
log.SetOutput(l)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
doneRotating := make(chan bool, 1)
go func() {
for {
<-c
err := l.Rotate()
assert.Nil(t, err)
doneRotating <- true
}
}()
logger := log.Default()
content := "this is a test for rotate on sighup"
logger.Printf(content)
fileContainsContent(t, logfilepath, []byte(content))
c <- syscall.SIGHUP
for {
if <-doneRotating {
break
}
}
var logfiles []string
walkErr := filepath.WalkDir(cwd, func(path string, entry fs.DirEntry, err error) error {
assert.Nil(t, err)
if entry.Type().IsDir() {
return nil
}
logfiles = append(logfiles, path)
return nil
})
assert.Nil(t, walkErr)
assert.Equal(t, 2, len(logfiles)) // the main log file and the rotated log file
rotatedLogfile := backupFile(cwd)
assert.FileExists(t, rotatedLogfile)
fileContainsContent(t, rotatedLogfile, []byte(content))
}