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