github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/command/agent/log_writer_test.go (about)

     1  package agent
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  type MockLogHandler struct {
     8  	logs []string
     9  }
    10  
    11  func (m *MockLogHandler) HandleLog(l string) {
    12  	m.logs = append(m.logs, l)
    13  }
    14  
    15  func TestLogWriter(t *testing.T) {
    16  	t.Parallel()
    17  	h := &MockLogHandler{}
    18  	w := NewLogWriter(4)
    19  
    20  	// Write some logs
    21  	w.Write([]byte("one")) // Gets dropped!
    22  	w.Write([]byte("two"))
    23  	w.Write([]byte("three"))
    24  	w.Write([]byte("four"))
    25  	w.Write([]byte("five"))
    26  
    27  	// Register a handler, sends old!
    28  	w.RegisterHandler(h)
    29  
    30  	w.Write([]byte("six"))
    31  	w.Write([]byte("seven"))
    32  
    33  	// Deregister
    34  	w.DeregisterHandler(h)
    35  
    36  	w.Write([]byte("eight"))
    37  	w.Write([]byte("nine"))
    38  
    39  	out := []string{
    40  		"two",
    41  		"three",
    42  		"four",
    43  		"five",
    44  		"six",
    45  		"seven",
    46  	}
    47  	for idx := range out {
    48  		if out[idx] != h.logs[idx] {
    49  			t.Fatalf("mismatch %v", h.logs)
    50  		}
    51  	}
    52  }