github.com/portworx/docker@v1.12.1/daemon/logger/copier_test.go (about)

     1  package logger
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io"
     7  	"sync"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  type TestLoggerJSON struct {
    13  	*json.Encoder
    14  	mu    sync.Mutex
    15  	delay time.Duration
    16  }
    17  
    18  func (l *TestLoggerJSON) Log(m *Message) error {
    19  	if l.delay > 0 {
    20  		time.Sleep(l.delay)
    21  	}
    22  	l.mu.Lock()
    23  	defer l.mu.Unlock()
    24  	return l.Encode(m)
    25  }
    26  
    27  func (l *TestLoggerJSON) Close() error { return nil }
    28  
    29  func (l *TestLoggerJSON) Name() string { return "json" }
    30  
    31  func TestCopier(t *testing.T) {
    32  	stdoutLine := "Line that thinks that it is log line from docker stdout"
    33  	stderrLine := "Line that thinks that it is log line from docker stderr"
    34  	var stdout bytes.Buffer
    35  	var stderr bytes.Buffer
    36  	for i := 0; i < 30; i++ {
    37  		if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
    38  			t.Fatal(err)
    39  		}
    40  		if _, err := stderr.WriteString(stderrLine + "\n"); err != nil {
    41  			t.Fatal(err)
    42  		}
    43  	}
    44  
    45  	var jsonBuf bytes.Buffer
    46  
    47  	jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf)}
    48  
    49  	c := NewCopier(
    50  		map[string]io.Reader{
    51  			"stdout": &stdout,
    52  			"stderr": &stderr,
    53  		},
    54  		jsonLog)
    55  	c.Run()
    56  	wait := make(chan struct{})
    57  	go func() {
    58  		c.Wait()
    59  		close(wait)
    60  	}()
    61  	select {
    62  	case <-time.After(1 * time.Second):
    63  		t.Fatal("Copier failed to do its work in 1 second")
    64  	case <-wait:
    65  	}
    66  	dec := json.NewDecoder(&jsonBuf)
    67  	for {
    68  		var msg Message
    69  		if err := dec.Decode(&msg); err != nil {
    70  			if err == io.EOF {
    71  				break
    72  			}
    73  			t.Fatal(err)
    74  		}
    75  		if msg.Source != "stdout" && msg.Source != "stderr" {
    76  			t.Fatalf("Wrong Source: %q, should be %q or %q", msg.Source, "stdout", "stderr")
    77  		}
    78  		if msg.Source == "stdout" {
    79  			if string(msg.Line) != stdoutLine {
    80  				t.Fatalf("Wrong Line: %q, expected %q", msg.Line, stdoutLine)
    81  			}
    82  		}
    83  		if msg.Source == "stderr" {
    84  			if string(msg.Line) != stderrLine {
    85  				t.Fatalf("Wrong Line: %q, expected %q", msg.Line, stderrLine)
    86  			}
    87  		}
    88  	}
    89  }
    90  
    91  func TestCopierSlow(t *testing.T) {
    92  	stdoutLine := "Line that thinks that it is log line from docker stdout"
    93  	var stdout bytes.Buffer
    94  	for i := 0; i < 30; i++ {
    95  		if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
    96  			t.Fatal(err)
    97  		}
    98  	}
    99  
   100  	var jsonBuf bytes.Buffer
   101  	//encoder := &encodeCloser{Encoder: json.NewEncoder(&jsonBuf)}
   102  	jsonLog := &TestLoggerJSON{Encoder: json.NewEncoder(&jsonBuf), delay: 100 * time.Millisecond}
   103  
   104  	c := NewCopier(map[string]io.Reader{"stdout": &stdout}, jsonLog)
   105  	c.Run()
   106  	wait := make(chan struct{})
   107  	go func() {
   108  		c.Wait()
   109  		close(wait)
   110  	}()
   111  	<-time.After(150 * time.Millisecond)
   112  	c.Close()
   113  	select {
   114  	case <-time.After(200 * time.Millisecond):
   115  		t.Fatalf("failed to exit in time after the copier is closed")
   116  	case <-wait:
   117  	}
   118  }