Skip to content

Commit

Permalink
test: add test rotate interval
Browse files Browse the repository at this point in the history
  • Loading branch information
qazwsxedckll committed Mar 6, 2024
1 parent ce19768 commit 731b5d2
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion rotate_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logh
import (
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand All @@ -23,7 +24,7 @@ func TestNewRotateFile(t *testing.T) {
}

func TestRotateSize(t *testing.T) {
path, err := os.MkdirTemp("", "loghtest-TestWrite")
path, err := os.MkdirTemp("", "loghtest-TestRotateSize")
require.NoError(t, err)
defer os.RemoveAll(path)

Expand All @@ -39,3 +40,31 @@ func TestRotateSize(t *testing.T) {
require.NoError(t, err)
require.Len(t, dir, 3)
}

func TestRotateInterval(t *testing.T) {
path, err := os.MkdirTemp("", "loghtest-TestRotateInterval")
require.NoError(t, err)
defer os.RemoveAll(path)

file, err := NewRotateFile(path, "test", 1024, WithCheckEveryN(2), WithRotateInterval(1*time.Second))
require.NoError(t, err)

_, err = file.Write([]byte("1"))
require.NoError(t, err)
_, err = file.Write([]byte("2"))
require.NoError(t, err)

dir, err := os.ReadDir(path)
require.NoError(t, err)
require.Len(t, dir, 1)

time.Sleep(1 * time.Second)
_, err = file.Write([]byte("1"))
require.NoError(t, err)
_, err = file.Write([]byte("2"))
require.NoError(t, err)

dir, err = os.ReadDir(path)
require.NoError(t, err)
require.Len(t, dir, 2)
}

0 comments on commit 731b5d2

Please sign in to comment.