2015-02-03 18:41:26 -05:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2016-06-06 11:50:09 -04:00
|
|
|
"fmt"
|
2015-02-03 18:41:26 -05:00
|
|
|
"io"
|
2016-06-06 11:50:09 -04:00
|
|
|
"os"
|
2016-05-02 07:50:08 -04:00
|
|
|
"strings"
|
2016-05-25 00:19:01 -04:00
|
|
|
"sync"
|
2015-02-03 18:41:26 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestLoggerJSON struct {
|
|
|
|
*json.Encoder
|
2016-05-25 00:19:01 -04:00
|
|
|
mu sync.Mutex
|
2016-01-15 08:42:23 -05:00
|
|
|
delay time.Duration
|
2015-02-03 18:41:26 -05:00
|
|
|
}
|
|
|
|
|
2016-01-15 08:42:23 -05:00
|
|
|
func (l *TestLoggerJSON) Log(m *Message) error {
|
|
|
|
if l.delay > 0 {
|
|
|
|
time.Sleep(l.delay)
|
|
|
|
}
|
2016-05-25 00:19:01 -04:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
2016-01-15 08:42:23 -05:00
|
|
|
return l.Encode(m)
|
|
|
|
}
|
2015-02-03 18:41:26 -05:00
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
func (l *TestLoggerJSON) Close() error { return nil }
|
|
|
|
|
|
|
|
func (l *TestLoggerJSON) Name() string { return "json" }
|
2015-02-03 18:41:26 -05:00
|
|
|
|
|
|
|
func TestCopier(t *testing.T) {
|
|
|
|
stdoutLine := "Line that thinks that it is log line from docker stdout"
|
|
|
|
stderrLine := "Line that thinks that it is log line from docker stderr"
|
2016-05-02 07:50:08 -04:00
|
|
|
stdoutTrailingLine := "stdout trailing line"
|
|
|
|
stderrTrailingLine := "stderr trailing line"
|
|
|
|
|
2015-02-03 18:41:26 -05:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := stderr.WriteString(stderrLine + "\n"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 07:50:08 -04:00
|
|
|
// Test remaining lines without line-endings
|
|
|
|
if _, err := stdout.WriteString(stdoutTrailingLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := stderr.WriteString(stderrTrailingLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonBuf bytes.Buffer
|
|
|
|
|
|
|
|
jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf)}
|
|
|
|
|
|
|
|
c := NewCopier(
|
|
|
|
map[string]io.Reader{
|
|
|
|
"stdout": &stdout,
|
|
|
|
"stderr": &stderr,
|
|
|
|
},
|
|
|
|
jsonLog)
|
|
|
|
c.Run()
|
|
|
|
wait := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
c.Wait()
|
|
|
|
close(wait)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("Copier failed to do its work in 1 second")
|
|
|
|
case <-wait:
|
|
|
|
}
|
|
|
|
dec := json.NewDecoder(&jsonBuf)
|
|
|
|
for {
|
|
|
|
var msg Message
|
|
|
|
if err := dec.Decode(&msg); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if msg.Source != "stdout" && msg.Source != "stderr" {
|
|
|
|
t.Fatalf("Wrong Source: %q, should be %q or %q", msg.Source, "stdout", "stderr")
|
|
|
|
}
|
|
|
|
if msg.Source == "stdout" {
|
|
|
|
if string(msg.Line) != stdoutLine && string(msg.Line) != stdoutTrailingLine {
|
|
|
|
t.Fatalf("Wrong Line: %q, expected %q or %q", msg.Line, stdoutLine, stdoutTrailingLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if msg.Source == "stderr" {
|
|
|
|
if string(msg.Line) != stderrLine && string(msg.Line) != stderrTrailingLine {
|
|
|
|
t.Fatalf("Wrong Line: %q, expected %q or %q", msg.Line, stderrLine, stderrTrailingLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCopierLongLines tests long lines without line breaks
|
|
|
|
func TestCopierLongLines(t *testing.T) {
|
|
|
|
// Long lines (should be split at "bufSize")
|
|
|
|
const bufSize = 16 * 1024
|
|
|
|
stdoutLongLine := strings.Repeat("a", bufSize)
|
|
|
|
stderrLongLine := strings.Repeat("b", bufSize)
|
|
|
|
stdoutTrailingLine := "stdout trailing line"
|
|
|
|
stderrTrailingLine := "stderr trailing line"
|
|
|
|
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
if _, err := stdout.WriteString(stdoutLongLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := stderr.WriteString(stderrLongLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := stdout.WriteString(stdoutTrailingLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := stderr.WriteString(stderrTrailingLine); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:41:26 -05:00
|
|
|
var jsonBuf bytes.Buffer
|
|
|
|
|
|
|
|
jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf)}
|
|
|
|
|
2016-05-31 15:46:55 -04:00
|
|
|
c := NewCopier(
|
2015-02-03 18:41:26 -05:00
|
|
|
map[string]io.Reader{
|
|
|
|
"stdout": &stdout,
|
|
|
|
"stderr": &stderr,
|
|
|
|
},
|
|
|
|
jsonLog)
|
|
|
|
c.Run()
|
2015-03-18 14:44:14 -04:00
|
|
|
wait := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
c.Wait()
|
|
|
|
close(wait)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("Copier failed to do its work in 1 second")
|
|
|
|
case <-wait:
|
|
|
|
}
|
2015-02-03 18:41:26 -05:00
|
|
|
dec := json.NewDecoder(&jsonBuf)
|
|
|
|
for {
|
|
|
|
var msg Message
|
|
|
|
if err := dec.Decode(&msg); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if msg.Source != "stdout" && msg.Source != "stderr" {
|
|
|
|
t.Fatalf("Wrong Source: %q, should be %q or %q", msg.Source, "stdout", "stderr")
|
|
|
|
}
|
|
|
|
if msg.Source == "stdout" {
|
2016-05-02 07:50:08 -04:00
|
|
|
if string(msg.Line) != stdoutLongLine && string(msg.Line) != stdoutTrailingLine {
|
|
|
|
t.Fatalf("Wrong Line: %q, expected 'stdoutLongLine' or 'stdoutTrailingLine'", msg.Line)
|
2015-02-03 18:41:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if msg.Source == "stderr" {
|
2016-05-02 07:50:08 -04:00
|
|
|
if string(msg.Line) != stderrLongLine && string(msg.Line) != stderrTrailingLine {
|
|
|
|
t.Fatalf("Wrong Line: %q, expected 'stderrLongLine' or 'stderrTrailingLine'", msg.Line)
|
2015-02-03 18:41:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-15 08:42:23 -05:00
|
|
|
|
|
|
|
func TestCopierSlow(t *testing.T) {
|
|
|
|
stdoutLine := "Line that thinks that it is log line from docker stdout"
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonBuf bytes.Buffer
|
|
|
|
//encoder := &encodeCloser{Encoder: json.NewEncoder(&jsonBuf)}
|
|
|
|
jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf), delay: 100 * time.Millisecond}
|
|
|
|
|
2016-05-31 15:46:55 -04:00
|
|
|
c := NewCopier(map[string]io.Reader{"stdout": &stdout}, jsonLog)
|
2016-01-15 08:42:23 -05:00
|
|
|
c.Run()
|
|
|
|
wait := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
c.Wait()
|
|
|
|
close(wait)
|
|
|
|
}()
|
|
|
|
<-time.After(150 * time.Millisecond)
|
|
|
|
c.Close()
|
|
|
|
select {
|
|
|
|
case <-time.After(200 * time.Millisecond):
|
|
|
|
t.Fatalf("failed to exit in time after the copier is closed")
|
|
|
|
case <-wait:
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 11:50:09 -04:00
|
|
|
|
|
|
|
type BenchmarkLoggerDummy struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *BenchmarkLoggerDummy) Log(m *Message) error { return nil }
|
|
|
|
|
|
|
|
func (l *BenchmarkLoggerDummy) Close() error { return nil }
|
|
|
|
|
|
|
|
func (l *BenchmarkLoggerDummy) Name() string { return "dummy" }
|
|
|
|
|
|
|
|
func BenchmarkCopier64(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<6)
|
|
|
|
}
|
|
|
|
func BenchmarkCopier128(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<7)
|
|
|
|
}
|
|
|
|
func BenchmarkCopier256(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<8)
|
|
|
|
}
|
|
|
|
func BenchmarkCopier512(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<9)
|
|
|
|
}
|
|
|
|
func BenchmarkCopier1K(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<10)
|
|
|
|
}
|
|
|
|
func BenchmarkCopier2K(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<11)
|
|
|
|
}
|
|
|
|
func BenchmarkCopier4K(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<12)
|
|
|
|
}
|
|
|
|
func BenchmarkCopier8K(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<13)
|
|
|
|
}
|
|
|
|
func BenchmarkCopier16K(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<14)
|
|
|
|
}
|
|
|
|
func BenchmarkCopier32K(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<15)
|
|
|
|
}
|
|
|
|
func BenchmarkCopier64K(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<16)
|
|
|
|
}
|
|
|
|
func BenchmarkCopier128K(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<17)
|
|
|
|
}
|
|
|
|
func BenchmarkCopier256K(b *testing.B) {
|
|
|
|
benchmarkCopier(b, 1<<18)
|
|
|
|
}
|
|
|
|
|
|
|
|
func piped(b *testing.B, iterations int, delay time.Duration, buf []byte) io.Reader {
|
|
|
|
r, w, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < iterations; i++ {
|
|
|
|
time.Sleep(delay)
|
|
|
|
if n, err := w.Write(buf); err != nil || n != len(buf) {
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.Fatal(fmt.Errorf("short write"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
}()
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkCopier(b *testing.B, length int) {
|
|
|
|
b.StopTimer()
|
|
|
|
buf := []byte{'A'}
|
|
|
|
for len(buf) < length {
|
|
|
|
buf = append(buf, buf...)
|
|
|
|
}
|
|
|
|
buf = append(buf[:length-1], []byte{'\n'}...)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
c := NewCopier(
|
|
|
|
map[string]io.Reader{
|
|
|
|
"buffer": piped(b, 10, time.Nanosecond, buf),
|
|
|
|
},
|
|
|
|
&BenchmarkLoggerDummy{})
|
|
|
|
c.Run()
|
|
|
|
c.Wait()
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}
|