github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/countlog/output/rotation/trigger_by_size_test.go (about)

     1  package rotation
     2  
     3  import (
     4  	"testing"
     5  	"github.com/v2pro/plz/test"
     6  	"github.com/v2pro/plz/countlog"
     7  	"os"
     8  	"github.com/v2pro/plz/test/must"
     9  	"io/ioutil"
    10  )
    11  
    12  func Test_TriggerBySize(t *testing.T) {
    13  	t.Run("init size below threshold", test.Case(func(ctx *countlog.Context) {
    14  		os.Remove("/tmp/test.log")
    15  		file := must.Call(os.OpenFile, "/tmp/test.log",
    16  			os.O_CREATE|os.O_RDWR, os.FileMode(0644))[0].(*os.File)
    17  		trigger := &TriggerBySize{SizeInKB: 1}
    18  		result := must.Call(trigger.UpdateStat, nil, file, make([]byte, 24))
    19  		must.Equal(int64(0), result[0])
    20  		must.Equal(false, result[1])
    21  		result = must.Call(trigger.UpdateStat, result[0], file, make([]byte, 24))
    22  		must.Equal(int64(24), result[0])
    23  		must.Equal(false, result[1])
    24  		result = must.Call(trigger.UpdateStat, result[0], file, make([]byte, 1000))
    25  		must.Equal(int64(1024), result[0])
    26  		must.Equal(true, result[1])
    27  	}))
    28  	t.Run("init size equal threshold", test.Case(func(ctx *countlog.Context) {
    29  		os.Remove("/tmp/test.log")
    30  		ioutil.WriteFile("/tmp/test.log", make([]byte, 1024), 0644)
    31  		file := must.Call(os.OpenFile, "/tmp/test.log",
    32  			os.O_CREATE|os.O_RDWR, os.FileMode(0644))[0].(*os.File)
    33  		trigger := &TriggerBySize{SizeInKB: 1}
    34  		result := must.Call(trigger.UpdateStat, nil, file, make([]byte, 24))
    35  		must.Equal(int64(1024), result[0])
    36  		must.Equal(true, result[1])
    37  	}))
    38  }