添加自动文件压缩的实现
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package loggerx_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/yuninks/loggerx"
|
||||
)
|
||||
|
||||
// MustSync 与写入并发时不能丢日志(模拟外部定时刷盘的用法)
|
||||
func TestMustSyncConcurrentNoLoss(t *testing.T) {
|
||||
for round := 0; round < 5; round++ {
|
||||
dir := t.TempDir()
|
||||
l := loggerx.NewLogger(context.Background(), loggerx.SetDir(dir))
|
||||
ctx := context.Background()
|
||||
|
||||
const n = 2000
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
|
||||
// 持续写入
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; i < n; i++ {
|
||||
l.Infof(ctx, "MS-%d", i)
|
||||
}
|
||||
}()
|
||||
// 持续外部刷盘(这正是 MustSync 的公开用途)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; i < 300; i++ {
|
||||
_ = l.MustSync()
|
||||
}
|
||||
}()
|
||||
wg.Wait()
|
||||
|
||||
if err := l.Close(); err != nil {
|
||||
t.Fatalf("第 %d 轮 Close: %v", round, err)
|
||||
}
|
||||
|
||||
content := readAllLogs(t, dir)
|
||||
missing := 0
|
||||
for i := 0; i < n; i++ {
|
||||
if !strings.Contains(content, fmt.Sprintf(`MS-%d"`, i)) {
|
||||
missing++
|
||||
}
|
||||
}
|
||||
if missing > 0 {
|
||||
t.Errorf("第 %d 轮:写入 %d 条,丢失 %d 条", round, n, missing)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user