github.com/judwhite/consul@v1.4.4-0.20190315202039-6ef970a191d3/logger/log_writer_test.go (about)

     1  package logger
     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  	h := &MockLogHandler{}
    17  	w := NewLogWriter(4)
    18  
    19  	// Write some logs
    20  	w.Write([]byte("one")) // Gets dropped!
    21  	w.Write([]byte("two"))
    22  	w.Write([]byte("three"))
    23  	w.Write([]byte("four"))
    24  	w.Write([]byte("five"))
    25  
    26  	// Register a handler, sends old!
    27  	w.RegisterHandler(h)
    28  
    29  	w.Write([]byte("six"))
    30  	w.Write([]byte("seven"))
    31  
    32  	// Deregister
    33  	w.DeregisterHandler(h)
    34  
    35  	w.Write([]byte("eight"))
    36  	w.Write([]byte("nine"))
    37  
    38  	out := []string{
    39  		"two",
    40  		"three",
    41  		"four",
    42  		"five",
    43  		"six",
    44  		"seven",
    45  	}
    46  	for idx := range out {
    47  		if out[idx] != h.logs[idx] {
    48  			t.Fatalf("mismatch %v", h.logs)
    49  		}
    50  	}
    51  }