Skip to content

Commit

Permalink
test: test rotate by size
Browse files Browse the repository at this point in the history
  • Loading branch information
qazwsxedckll committed Mar 5, 2024
1 parent 73e9681 commit c51d124
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rotate_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
18 changes: 17 additions & 1 deletion rotate_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

0 comments on commit c51d124

Please sign in to comment.