From 907dba4a48a3508145f4579db31510af766ba90a Mon Sep 17 00:00:00 2001 From: Jason Mo Date: Thu, 22 Feb 2024 14:44:11 +0800 Subject: [PATCH] update test --- zap_log_test.go | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/zap_log_test.go b/zap_log_test.go index 4075297..296178e 100644 --- a/zap_log_test.go +++ b/zap_log_test.go @@ -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{ @@ -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) {