From c51d124ecd098e3b7a2cb4ba269ed1a9b6717ce0 Mon Sep 17 00:00:00 2001 From: evilolipop Date: Tue, 5 Mar 2024 17:44:45 +0800 Subject: [PATCH] test: test rotate by size --- rotate_file.go | 2 +- rotate_file_test.go | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/rotate_file.go b/rotate_file.go index 084395a..69e6df5 100644 --- a/rotate_file.go +++ b/rotate_file.go @@ -66,7 +66,7 @@ func (r *RotateFile) logFileName() (string, time.Time) { hostname = "unknownhost" } now := time.Now() - return r.filepath + "." + now.Format("20060102-150405") + "." + hostname + "." + fmt.Sprint(os.Getpid()) + ".log", now + return r.filepath + "." + now.Format("20060102-150405.000000000") + "." + hostname + "." + fmt.Sprint(os.Getpid()) + ".log", now } func (r *RotateFile) Rotate() { diff --git a/rotate_file_test.go b/rotate_file_test.go index d64f838..b0f6e3d 100644 --- a/rotate_file_test.go +++ b/rotate_file_test.go @@ -10,6 +10,7 @@ import ( func TestNewRotateFile(t *testing.T) { path, err := os.MkdirTemp("", "loghtest") require.NoError(t, err) + defer os.RemoveAll(path) path += "/TestNewRotateFile" file, err := NewRotateFile(path, "test", 10) @@ -19,7 +20,22 @@ func TestNewRotateFile(t *testing.T) { dir, err := os.ReadDir(path) require.NoError(t, err) require.Len(t, dir, 1) +} + +func TestRotateSize(t *testing.T) { + path, err := os.MkdirTemp("", "loghtest-TestWrite") + require.NoError(t, err) + defer os.RemoveAll(path) + + file, err := NewRotateFile(path, "test", 10) + require.NoError(t, err) - err = os.RemoveAll(path) + _, err = file.Write([]byte("12345678901234567890")) + require.NoError(t, err) + _, err = file.Write([]byte("12345678901234567890")) + require.NoError(t, err) + + dir, err := os.ReadDir(path) require.NoError(t, err) + require.Len(t, dir, 3) }