tlog.app/go/tlog@v0.23.1/console_test.go (about)

     1  package tlog
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/nikandfor/assert"
     9  	"github.com/nikandfor/hacked/low"
    10  	"tlog.app/go/loc"
    11  
    12  	"tlog.app/go/tlog/tlwire"
    13  )
    14  
    15  type MultiWriter []io.Writer
    16  
    17  func TestConsoleLocations(t *testing.T) {
    18  	var buf, raw low.Buf
    19  
    20  	w := NewConsoleWriter(&buf, 0)
    21  	l := New(MultiWriter{&raw, w})
    22  
    23  	w.PadEmptyMessage = false
    24  
    25  	c := loc.Caller(-1)
    26  	cc := loc.Callers(-1, 2)
    27  
    28  	{
    29  		name, file, line := c.NameFileLine()
    30  		t.Logf("caller: %v %v %v", name, file, line)
    31  
    32  		for _, pc := range cc {
    33  			name, file, line := pc.NameFileLine()
    34  			t.Logf("callers: %v %v %v", name, file, line)
    35  		}
    36  	}
    37  
    38  	_ = l.Event("caller", c)
    39  	assert.Equal(t, "caller=location.go:24\n", string(buf))
    40  
    41  	buf = buf[:0]
    42  
    43  	_ = l.Event("callers", cc)
    44  	assert.Equal(t, "callers=[location.go:71 console_test.go:26]\n", string(buf))
    45  
    46  	t.Logf("dump:\n%v", tlwire.Dump(raw))
    47  }
    48  
    49  func (w MultiWriter) Write(p []byte) (n int, err error) {
    50  	for i, w := range w {
    51  		m, e := w.Write(p)
    52  
    53  		if i == 0 {
    54  			n = m
    55  		}
    56  
    57  		if err == nil {
    58  			err = e
    59  		}
    60  	}
    61  
    62  	return
    63  }
    64  
    65  func TestConsole(tb *testing.T) {
    66  	var e tlwire.Encoder
    67  	var b, jb low.Buf
    68  
    69  	j := NewConsoleWriter(&jb, 0)
    70  
    71  	b = e.AppendInt(b[:0], 5)
    72  	jb, _ = j.ConvertValue(jb[:0], b, 0, 0)
    73  	assert.Equal(tb, low.Buf("5"), jb)
    74  
    75  	b = e.AppendInt(b[:0], -5)
    76  	jb, _ = j.ConvertValue(jb[:0], b, 0, 0)
    77  	assert.Equal(tb, low.Buf("-5"), jb)
    78  }
    79  
    80  func TestAppendDuration(t *testing.T) {
    81  	w := NewConsoleWriter(nil, 0)
    82  
    83  	t.Logf("%s", w.AppendDuration(nil, 0))
    84  
    85  	for _, days := range []int{0, 2} {
    86  		for _, h := range []int{0, 12} {
    87  			for _, m := range []int{0, 2} {
    88  				for _, s := range []int{0, 36} {
    89  					d := time.Hour*time.Duration(24*days+h) +
    90  						time.Minute*time.Duration(m) +
    91  						time.Second*time.Duration(s)
    92  
    93  					t.Logf("%s is %v", w.AppendDuration(nil, d), d)
    94  				}
    95  			}
    96  		}
    97  	}
    98  
    99  	for d := time.Nanosecond; d < 100*time.Second; d *= 7 {
   100  		t.Logf("%s is %v", w.AppendDuration(nil, d), d)
   101  	}
   102  
   103  	for f := float64(1); f < float64(200*time.Microsecond); f *= 1.378 {
   104  		d := time.Duration(f)
   105  		t.Logf("%s is %v", w.AppendDuration(nil, d), d)
   106  	}
   107  
   108  	d := 99999 * time.Microsecond
   109  	t.Logf("%s is %v", w.AppendDuration(nil, d), d)
   110  	d = 999999 * time.Microsecond
   111  	t.Logf("%s is %v", w.AppendDuration(nil, d), d)
   112  }
   113  
   114  func BenchmarkConsolePrintw(b *testing.B) {
   115  	b.ReportAllocs()
   116  
   117  	w := NewConsoleWriter(io.Discard, LdetFlags)
   118  	l := New(w)
   119  
   120  	for i := 0; i < b.N; i++ {
   121  		l.Printw("message", "a", i+1000, "b", i+1000, "c", "str")
   122  	}
   123  }
   124  
   125  func BenchmarkConsoleStartPrintwFinish(b *testing.B) {
   126  	b.ReportAllocs()
   127  
   128  	w := NewConsoleWriter(io.Discard, LdetFlags)
   129  	l := New(w)
   130  
   131  	for i := 0; i < b.N; i++ {
   132  		tr := l.Start("span_name")
   133  		tr.Printw("message", "a", i+1000, "b", i+1000, "c", "str")
   134  		tr.Finish()
   135  	}
   136  }