1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/daemon/logger/local/read_test.go
Paweł Gronowski 7493342926 daemon/logger: Share buffers by sync.Pool
Marshalling log messages by json-file and local drivers involved
serializing the message into a shared buffer. This caused a regression
resulting in log corruption with recent changes where Log may be called
from multiple goroutines at the same time.

Solution is to use a sync.Pool to manage the buffers used for the
serialization. Also removed the MarshalFunc, which the driver had to
expose to the LogFile so that it can marshal the message. This is now
moved entirely to the driver.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2022-05-27 16:44:06 +02:00

51 lines
955 B
Go

package local
import (
"io"
"os"
"testing"
"github.com/docker/docker/daemon/logger"
"github.com/pkg/errors"
"gotest.tools/v3/assert"
)
func TestDecode(t *testing.T) {
buf := make([]byte, 0)
err := marshal(&logger.Message{Line: []byte("hello")}, &buf)
assert.NilError(t, err)
for i := 0; i < len(buf); i++ {
testDecode(t, buf, i)
}
}
func testDecode(t *testing.T, buf []byte, split int) {
fw, err := os.CreateTemp("", t.Name())
assert.NilError(t, err)
defer os.Remove(fw.Name())
fr, err := os.Open(fw.Name())
assert.NilError(t, err)
d := &decoder{rdr: fr}
if split > 0 {
_, err = fw.Write(buf[0:split])
assert.NilError(t, err)
_, err = d.Decode()
assert.Assert(t, errors.Is(err, io.EOF))
_, err = fw.Write(buf[split:])
assert.NilError(t, err)
} else {
_, err = fw.Write(buf)
assert.NilError(t, err)
}
message, err := d.Decode()
assert.NilError(t, err)
assert.Equal(t, "hello\n", string(message.Line))
}