tlog.app/go/tlog@v0.23.1/rotating/file_test.go (about)

     1  package rotating
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/nikandfor/assert"
    10  	"github.com/nikandfor/hacked/low"
    11  
    12  	"tlog.app/go/tlog"
    13  	"tlog.app/go/tlog/tlwire"
    14  )
    15  
    16  func TestRotation(t *testing.T) {
    17  	var f1, f2 low.Buf
    18  
    19  	f := Create("name.tlog.ez")
    20  	f.symlink = nil
    21  	f.OpenFile = func(n string, ff int, m os.FileMode) (w io.Writer, _ error) {
    22  		if f1 == nil {
    23  			w = &f1
    24  		} else {
    25  			w = &f2
    26  		}
    27  
    28  		return w, nil
    29  	}
    30  
    31  	l := tlog.New(f)
    32  
    33  	l.SetLabels("a", "b", "c", nil)
    34  
    35  	tr := l.Start("some_span")
    36  
    37  	msg(tr, 1)
    38  	msg(tr, 2)
    39  
    40  	err := f.Rotate()
    41  	if err != nil {
    42  		assert.NoError(t, err)
    43  	}
    44  
    45  	msg(tr, 3)
    46  
    47  	dumpFile(t, f1, "first")
    48  	dumpFile(t, f2, "second")
    49  
    50  	if len(f1) < 20 || len(f2) < 20 {
    51  		t.Errorf("one of files is too small")
    52  	}
    53  }
    54  
    55  func dumpFile(t *testing.T, f low.Buf, name string) {
    56  	t.Helper()
    57  
    58  	//	r := compress.NewDecoderBytes(f)
    59  	var r io.Reader = bytes.NewReader(f)
    60  
    61  	d, err := io.ReadAll(r)
    62  	assert.NoError(t, err)
    63  
    64  	t.Logf("file %q\n%s", name, tlwire.Dump(d))
    65  }
    66  
    67  //go:noinline
    68  func msg(tr tlog.Span, a int) {
    69  	tr.Printw("message", "a", a)
    70  }