github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/logger/jsonfilelog/read_test.go (about)

     1  package jsonfilelog // import "github.com/demonoid81/moby/daemon/logger/jsonfilelog"
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/demonoid81/moby/daemon/logger"
    10  	"gotest.tools/v3/assert"
    11  	"gotest.tools/v3/fs"
    12  )
    13  
    14  func BenchmarkJSONFileLoggerReadLogs(b *testing.B) {
    15  	tmp := fs.NewDir(b, "bench-jsonfilelog")
    16  	defer tmp.Remove()
    17  
    18  	jsonlogger, err := New(logger.Info{
    19  		ContainerID: "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657",
    20  		LogPath:     tmp.Join("container.log"),
    21  		Config: map[string]string{
    22  			"labels": "first,second",
    23  		},
    24  		ContainerLabels: map[string]string{
    25  			"first":  "label_value",
    26  			"second": "label_foo",
    27  		},
    28  	})
    29  	assert.NilError(b, err)
    30  	defer jsonlogger.Close()
    31  
    32  	msg := &logger.Message{
    33  		Line:      []byte("Line that thinks that it is log line from docker\n"),
    34  		Source:    "stderr",
    35  		Timestamp: time.Now().UTC(),
    36  	}
    37  
    38  	buf := bytes.NewBuffer(nil)
    39  	assert.NilError(b, marshalMessage(msg, nil, buf))
    40  	b.SetBytes(int64(buf.Len()))
    41  
    42  	b.ResetTimer()
    43  
    44  	chError := make(chan error, b.N+1)
    45  	go func() {
    46  		for i := 0; i < b.N; i++ {
    47  			chError <- jsonlogger.Log(msg)
    48  		}
    49  		chError <- jsonlogger.Close()
    50  	}()
    51  
    52  	lw := jsonlogger.(*JSONFileLogger).ReadLogs(logger.ReadConfig{Follow: true})
    53  	for {
    54  		select {
    55  		case <-lw.Msg:
    56  		case <-lw.WatchProducerGone():
    57  			return
    58  		case err := <-chError:
    59  			if err != nil {
    60  				b.Fatal(err)
    61  			}
    62  		}
    63  	}
    64  }
    65  
    66  func TestEncodeDecode(t *testing.T) {
    67  	t.Parallel()
    68  
    69  	m1 := &logger.Message{Line: []byte("hello 1"), Timestamp: time.Now(), Source: "stdout"}
    70  	m2 := &logger.Message{Line: []byte("hello 2"), Timestamp: time.Now(), Source: "stdout"}
    71  	m3 := &logger.Message{Line: []byte("hello 3"), Timestamp: time.Now(), Source: "stdout"}
    72  
    73  	buf := bytes.NewBuffer(nil)
    74  	assert.Assert(t, marshalMessage(m1, nil, buf))
    75  	assert.Assert(t, marshalMessage(m2, nil, buf))
    76  	assert.Assert(t, marshalMessage(m3, nil, buf))
    77  
    78  	dec := decodeFunc(buf)
    79  	defer dec.Close()
    80  
    81  	msg, err := dec.Decode()
    82  	assert.NilError(t, err)
    83  	assert.Assert(t, string(msg.Line) == "hello 1\n", string(msg.Line))
    84  
    85  	msg, err = dec.Decode()
    86  	assert.NilError(t, err)
    87  	assert.Assert(t, string(msg.Line) == "hello 2\n")
    88  
    89  	msg, err = dec.Decode()
    90  	assert.NilError(t, err)
    91  	assert.Assert(t, string(msg.Line) == "hello 3\n")
    92  
    93  	_, err = dec.Decode()
    94  	assert.Assert(t, err == io.EOF)
    95  }