Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
Defined2014 committed Feb 22, 2024
1 parent 78d4a3b commit 907dba4
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions zap_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func TestZapCaller(t *testing.T) {
}

func TestRotateLog(t *testing.T) {
tempDir, _ := os.MkdirTemp("/tmp", "pd-tests-log")
tempDir := t.TempDir()
conf := &Config{
Level: "info",
File: FileLogConfig{
Expand All @@ -193,7 +193,44 @@ func TestRotateLog(t *testing.T) {
}
files, _ := os.ReadDir(tempDir)
require.Len(t, files, 2)
_ = os.RemoveAll(tempDir)
}

func TestRotateLogWithCompress(t *testing.T) {
tempDir := t.TempDir()
conf := &Config{
Level: "info",
File: FileLogConfig{
Filename: tempDir + "/test.log",
MaxSize: 1,
Compress: "gzip",
},
}
logger, _, err := InitLogger(conf)
require.NoError(t, err)

var data []byte
for i := 1; i <= 1*1024*1024; i++ {
if i%1000 != 0 {
data = append(data, 'd')
continue
}
logger.Info(string(data))
data = data[:0]
}
// Waiting rotation finished, it's async
for {
files, _ := os.ReadDir(tempDir)
if len(files) == 2 {
for _, f := range files {
info, err := f.Info()
require.NoError(t, err)
// Many duplicate logs, the file size after compress should less than 512KB
require.Less(t, info.Size(), int64(512*1024))
}
break
}
time.Sleep(time.Millisecond)
}
}

func TestErrorLog(t *testing.T) {
Expand Down

0 comments on commit 907dba4

Please sign in to comment.