From 44c8d6f26995f401e163c4e68934719498c3b837 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 30 May 2023 23:27:27 +0800 Subject: [PATCH] chore: add more tests (#3304) --- core/logx/rotatelogger_test.go | 190 ++++++++++++++++++++++++++++----- 1 file changed, 162 insertions(+), 28 deletions(-) diff --git a/core/logx/rotatelogger_test.go b/core/logx/rotatelogger_test.go index ab0bb2a0..6079c3b1 100644 --- a/core/logx/rotatelogger_test.go +++ b/core/logx/rotatelogger_test.go @@ -2,6 +2,7 @@ package logx import ( "os" + "path" "path/filepath" "syscall" "testing" @@ -13,18 +14,58 @@ import ( ) func TestDailyRotateRuleMarkRotated(t *testing.T) { - var rule DailyRotateRule - rule.MarkRotated() - assert.Equal(t, getNowDate(), rule.rotatedTime) + t.Run("daily rule", func(t *testing.T) { + var rule DailyRotateRule + rule.MarkRotated() + assert.Equal(t, getNowDate(), rule.rotatedTime) + }) + + t.Run("daily rule", func(t *testing.T) { + rule := DefaultRotateRule("test", "-", 1, false) + _, ok := rule.(*DailyRotateRule) + assert.True(t, ok) + }) } func TestDailyRotateRuleOutdatedFiles(t *testing.T) { - var rule DailyRotateRule - assert.Empty(t, rule.OutdatedFiles()) - rule.days = 1 - assert.Empty(t, rule.OutdatedFiles()) - rule.gzip = true - assert.Empty(t, rule.OutdatedFiles()) + t.Run("no files", func(t *testing.T) { + var rule DailyRotateRule + assert.Empty(t, rule.OutdatedFiles()) + rule.days = 1 + assert.Empty(t, rule.OutdatedFiles()) + rule.gzip = true + assert.Empty(t, rule.OutdatedFiles()) + }) + + t.Run("bad files", func(t *testing.T) { + rule := DailyRotateRule{ + filename: "[a-z", + } + assert.Empty(t, rule.OutdatedFiles()) + rule.days = 1 + assert.Empty(t, rule.OutdatedFiles()) + rule.gzip = true + assert.Empty(t, rule.OutdatedFiles()) + }) + + t.Run("temp files", func(t *testing.T) { + boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat) + f1, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary) + assert.NoError(t, err) + _ = f1.Close() + f2, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary) + assert.NoError(t, err) + _ = f2.Close() + t.Cleanup(func() { + _ = os.Remove(f1.Name()) + _ = os.Remove(f2.Name()) + }) + rule := DailyRotateRule{ + filename: path.Join(os.TempDir(), "go-zero-test-"), + days: 1, + } + assert.NotEmpty(t, rule.OutdatedFiles()) + }) } func TestDailyRotateRuleShallRotate(t *testing.T) { @@ -34,20 +75,101 @@ func TestDailyRotateRuleShallRotate(t *testing.T) { } func TestSizeLimitRotateRuleMarkRotated(t *testing.T) { - var rule SizeLimitRotateRule - rule.MarkRotated() - assert.Equal(t, getNowDateInRFC3339Format(), rule.rotatedTime) + t.Run("size limit rule", func(t *testing.T) { + var rule SizeLimitRotateRule + rule.MarkRotated() + assert.Equal(t, getNowDateInRFC3339Format(), rule.rotatedTime) + }) + + t.Run("size limit rule", func(t *testing.T) { + rule := NewSizeLimitRotateRule("foo", "-", 1, 1, 1, false) + rule.MarkRotated() + assert.Equal(t, getNowDateInRFC3339Format(), rule.(*SizeLimitRotateRule).rotatedTime) + }) } func TestSizeLimitRotateRuleOutdatedFiles(t *testing.T) { - var rule SizeLimitRotateRule - assert.Empty(t, rule.OutdatedFiles()) - rule.days = 1 - assert.Empty(t, rule.OutdatedFiles()) - rule.gzip = true - assert.Empty(t, rule.OutdatedFiles()) - rule.maxBackups = 0 - assert.Empty(t, rule.OutdatedFiles()) + t.Run("no files", func(t *testing.T) { + var rule SizeLimitRotateRule + assert.Empty(t, rule.OutdatedFiles()) + rule.days = 1 + assert.Empty(t, rule.OutdatedFiles()) + rule.gzip = true + assert.Empty(t, rule.OutdatedFiles()) + rule.maxBackups = 0 + assert.Empty(t, rule.OutdatedFiles()) + }) + + t.Run("bad files", func(t *testing.T) { + rule := SizeLimitRotateRule{ + DailyRotateRule: DailyRotateRule{ + filename: "[a-z", + }, + } + assert.Empty(t, rule.OutdatedFiles()) + rule.days = 1 + assert.Empty(t, rule.OutdatedFiles()) + rule.gzip = true + assert.Empty(t, rule.OutdatedFiles()) + }) + + t.Run("temp files", func(t *testing.T) { + boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat) + f1, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary) + assert.NoError(t, err) + f2, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary) + assert.NoError(t, err) + boundary1 := time.Now().Add(time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat) + f3, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary1) + assert.NoError(t, err) + t.Cleanup(func() { + _ = f1.Close() + _ = os.Remove(f1.Name()) + _ = f2.Close() + _ = os.Remove(f2.Name()) + _ = f3.Close() + _ = os.Remove(f3.Name()) + }) + rule := SizeLimitRotateRule{ + DailyRotateRule: DailyRotateRule{ + filename: path.Join(os.TempDir(), "go-zero-test-"), + days: 1, + }, + maxBackups: 3, + } + assert.NotEmpty(t, rule.OutdatedFiles()) + }) + + t.Run("no backups", func(t *testing.T) { + boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat) + f1, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary) + assert.NoError(t, err) + f2, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary) + assert.NoError(t, err) + boundary1 := time.Now().Add(time.Hour * time.Duration(hoursPerDay) * 2).Format(dateFormat) + f3, err := os.CreateTemp(os.TempDir(), "go-zero-test-"+boundary1) + assert.NoError(t, err) + t.Cleanup(func() { + _ = f1.Close() + _ = os.Remove(f1.Name()) + _ = f2.Close() + _ = os.Remove(f2.Name()) + _ = f3.Close() + _ = os.Remove(f3.Name()) + }) + rule := SizeLimitRotateRule{ + DailyRotateRule: DailyRotateRule{ + filename: path.Join(os.TempDir(), "go-zero-test-"), + days: 1, + }, + } + assert.NotEmpty(t, rule.OutdatedFiles()) + + logger := new(RotateLogger) + logger.rule = &rule + logger.maybeDeleteOutdatedFiles() + assert.Empty(t, rule.OutdatedFiles()) + }) } func TestSizeLimitRotateRuleShallRotate(t *testing.T) { @@ -61,14 +183,26 @@ func TestSizeLimitRotateRuleShallRotate(t *testing.T) { } func TestRotateLoggerClose(t *testing.T) { - filename, err := fs.TempFilenameWithText("foo") - assert.Nil(t, err) - if len(filename) > 0 { - defer os.Remove(filename) - } - logger, err := NewLogger(filename, new(DailyRotateRule), false) - assert.Nil(t, err) - assert.Nil(t, logger.Close()) + t.Run("close", func(t *testing.T) { + filename, err := fs.TempFilenameWithText("foo") + assert.Nil(t, err) + if len(filename) > 0 { + defer os.Remove(filename) + } + logger, err := NewLogger(filename, new(DailyRotateRule), false) + assert.Nil(t, err) + _, err = logger.Write([]byte("foo")) + assert.Nil(t, err) + assert.Nil(t, logger.Close()) + }) + + t.Run("close and write", func(t *testing.T) { + logger := new(RotateLogger) + logger.done = make(chan struct{}) + close(logger.done) + _, err := logger.Write([]byte("foo")) + assert.ErrorIs(t, err, ErrLogFileClosed) + }) } func TestRotateLoggerGetBackupFilename(t *testing.T) {