tstest: rename LogListener to LogLineTracker

But mostly to rename tstest.ListenFor which has no mention of log lines in it.
It sounded like a net.Listener or something.
reviewable/pr737/r1
Brad Fitzpatrick 4 years ago
parent 95cddfcc75
commit 68c42530e9

@ -20,7 +20,7 @@ import (
// being logged by the expected functions. Update these tests if moving log lines between // being logged by the expected functions. Update these tests if moving log lines between
// functions. // functions.
func TestLocalLogLines(t *testing.T) { func TestLocalLogLines(t *testing.T) {
logListen := tstest.ListenFor(t.Logf, []string{ logListen := tstest.NewLogLineTracker(t.Logf, []string{
"SetPrefs: %v", "SetPrefs: %v",
"peer keys: %s", "peer keys: %s",
"v%v peers: %v", "v%v peers: %v",

@ -45,46 +45,47 @@ func PanicOnLog() {
log.SetOutput(panicLogWriter{}) log.SetOutput(panicLogWriter{})
} }
// ListenFor produces a LogListener wrapping a given logf with the given logStrings // NewLogLineTracker produces a LogLineTracker wrapping a given logf that tracks whether expectedFormatStrings were seen.
func ListenFor(logf logger.Logf, logStrings []string) *LogListener { func NewLogLineTracker(logf logger.Logf, expectedFormatStrings []string) *LogLineTracker {
ret := LogListener{ ret := &LogLineTracker{
logf: logf, logf: logf,
listenFor: logStrings, listenFor: expectedFormatStrings,
seen: make(map[string]bool), seen: make(map[string]bool),
} }
for _, line := range logStrings { for _, line := range expectedFormatStrings {
ret.seen[line] = false ret.seen[line] = false
} }
return &ret return ret
} }
// LogListener takes a list of log lines to listen for // LogLineTracker is a logger that tracks which log format patterns it's
type LogListener struct { // seen and can report which expected ones were not seen later.
type LogLineTracker struct {
logf logger.Logf logf logger.Logf
listenFor []string listenFor []string
mu sync.Mutex mu sync.Mutex
seen map[string]bool seen map[string]bool // format string => false (if not yet seen but wanted) or true (once seen)
} }
// Logf records and logs a given line // Logf logs to its underlying logger and also tracks that the given format pattern has been seen.
func (ll *LogListener) Logf(format string, args ...interface{}) { func (lt *LogLineTracker) Logf(format string, args ...interface{}) {
ll.mu.Lock() lt.mu.Lock()
if _, ok := ll.seen[format]; ok { if v, ok := lt.seen[format]; ok && !v {
ll.seen[format] = true lt.seen[format] = true
} }
ll.mu.Unlock() lt.mu.Unlock()
ll.logf(format, args) lt.logf(format, args)
} }
// Check returns which lines haven't been logged yet // Check returns which format strings haven't been logged yet.
func (ll *LogListener) Check() []string { func (lt *LogLineTracker) Check() []string {
ll.mu.Lock() lt.mu.Lock()
defer ll.mu.Unlock() defer lt.mu.Unlock()
var notSeen []string var notSeen []string
for _, line := range ll.listenFor { for _, format := range lt.listenFor {
if !ll.seen[line] { if !lt.seen[format] {
notSeen = append(notSeen, line) notSeen = append(notSeen, format)
} }
} }
return notSeen return notSeen

@ -4,43 +4,45 @@
package tstest package tstest
import "testing" import (
"reflect"
"testing"
)
func TestLogListener(t *testing.T) { func TestLogLineTracker(t *testing.T) {
var ( const (
l1 = "line 1: %s" l1 = "line 1: %s"
l2 = "line 2: %s" l2 = "line 2: %s"
l3 = "line 3: %s" l3 = "line 3: %s"
lineList = []string{l1, l2}
) )
ll := ListenFor(t.Logf, lineList) lt := NewLogLineTracker(t.Logf, []string{l1, l2})
if len(ll.Check()) != len(lineList) { if got, want := lt.Check(), []string{l1, l2}; !reflect.DeepEqual(got, want) {
t.Errorf("expected %v, got %v", lineList, ll.Check()) t.Errorf("Check = %q; want %q", got, want)
} }
ll.Logf(l3, "hi") lt.Logf(l3, "hi")
if len(ll.Check()) != len(lineList) { if got, want := lt.Check(), []string{l1, l2}; !reflect.DeepEqual(got, want) {
t.Errorf("expected %v, got %v", lineList, ll.Check()) t.Errorf("Check = %q; want %q", got, want)
} }
ll.Logf(l1, "hi") lt.Logf(l1, "hi")
if len(ll.Check()) != len(lineList)-1 { if got, want := lt.Check(), []string{l2}; !reflect.DeepEqual(got, want) {
t.Errorf("expected %v, got %v", lineList, ll.Check()) t.Errorf("Check = %q; want %q", got, want)
} }
ll.Logf(l1, "bye") lt.Logf(l1, "bye")
if len(ll.Check()) != len(lineList)-1 { if got, want := lt.Check(), []string{l2}; !reflect.DeepEqual(got, want) {
t.Errorf("expected %v, got %v", lineList, ll.Check()) t.Errorf("Check = %q; want %q", got, want)
} }
ll.Logf(l2, "hi") lt.Logf(l2, "hi")
if ll.Check() != nil {
t.Errorf("expected empty list, got ll.Check()") if got, want := lt.Check(), []string(nil); !reflect.DeepEqual(got, want) {
t.Errorf("Check = %q; want %q", got, want)
} }
} }

Loading…
Cancel
Save