From 68c42530e9e2554d60386ce6986bded6fd9c789d Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 4 Sep 2020 08:09:56 -0700 Subject: [PATCH] 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. --- ipn/loglines_test.go | 2 +- tstest/log.go | 47 ++++++++++++++++++++++---------------------- tstest/log_test.go | 42 ++++++++++++++++++++------------------- 3 files changed, 47 insertions(+), 44 deletions(-) diff --git a/ipn/loglines_test.go b/ipn/loglines_test.go index 4cec82b4f..5f317e5de 100644 --- a/ipn/loglines_test.go +++ b/ipn/loglines_test.go @@ -20,7 +20,7 @@ import ( // being logged by the expected functions. Update these tests if moving log lines between // functions. func TestLocalLogLines(t *testing.T) { - logListen := tstest.ListenFor(t.Logf, []string{ + logListen := tstest.NewLogLineTracker(t.Logf, []string{ "SetPrefs: %v", "peer keys: %s", "v%v peers: %v", diff --git a/tstest/log.go b/tstest/log.go index 887521793..f766c4fe9 100644 --- a/tstest/log.go +++ b/tstest/log.go @@ -45,46 +45,47 @@ func PanicOnLog() { log.SetOutput(panicLogWriter{}) } -// ListenFor produces a LogListener wrapping a given logf with the given logStrings -func ListenFor(logf logger.Logf, logStrings []string) *LogListener { - ret := LogListener{ +// NewLogLineTracker produces a LogLineTracker wrapping a given logf that tracks whether expectedFormatStrings were seen. +func NewLogLineTracker(logf logger.Logf, expectedFormatStrings []string) *LogLineTracker { + ret := &LogLineTracker{ logf: logf, - listenFor: logStrings, + listenFor: expectedFormatStrings, seen: make(map[string]bool), } - for _, line := range logStrings { + for _, line := range expectedFormatStrings { ret.seen[line] = false } - return &ret + return ret } -// LogListener takes a list of log lines to listen for -type LogListener struct { +// LogLineTracker is a logger that tracks which log format patterns it's +// seen and can report which expected ones were not seen later. +type LogLineTracker struct { logf logger.Logf listenFor []string 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 -func (ll *LogListener) Logf(format string, args ...interface{}) { - ll.mu.Lock() - if _, ok := ll.seen[format]; ok { - ll.seen[format] = true +// Logf logs to its underlying logger and also tracks that the given format pattern has been seen. +func (lt *LogLineTracker) Logf(format string, args ...interface{}) { + lt.mu.Lock() + if v, ok := lt.seen[format]; ok && !v { + lt.seen[format] = true } - ll.mu.Unlock() - ll.logf(format, args) + lt.mu.Unlock() + lt.logf(format, args) } -// Check returns which lines haven't been logged yet -func (ll *LogListener) Check() []string { - ll.mu.Lock() - defer ll.mu.Unlock() +// Check returns which format strings haven't been logged yet. +func (lt *LogLineTracker) Check() []string { + lt.mu.Lock() + defer lt.mu.Unlock() var notSeen []string - for _, line := range ll.listenFor { - if !ll.seen[line] { - notSeen = append(notSeen, line) + for _, format := range lt.listenFor { + if !lt.seen[format] { + notSeen = append(notSeen, format) } } return notSeen diff --git a/tstest/log_test.go b/tstest/log_test.go index d864b9b9a..9ba3d4176 100644 --- a/tstest/log_test.go +++ b/tstest/log_test.go @@ -4,43 +4,45 @@ package tstest -import "testing" +import ( + "reflect" + "testing" +) -func TestLogListener(t *testing.T) { - var ( +func TestLogLineTracker(t *testing.T) { + const ( l1 = "line 1: %s" l2 = "line 2: %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) { - t.Errorf("expected %v, got %v", lineList, ll.Check()) + if got, want := lt.Check(), []string{l1, l2}; !reflect.DeepEqual(got, want) { + t.Errorf("Check = %q; want %q", got, want) } - ll.Logf(l3, "hi") + lt.Logf(l3, "hi") - if len(ll.Check()) != len(lineList) { - t.Errorf("expected %v, got %v", lineList, ll.Check()) + if got, want := lt.Check(), []string{l1, l2}; !reflect.DeepEqual(got, want) { + t.Errorf("Check = %q; want %q", got, want) } - ll.Logf(l1, "hi") + lt.Logf(l1, "hi") - if len(ll.Check()) != len(lineList)-1 { - t.Errorf("expected %v, got %v", lineList, ll.Check()) + if got, want := lt.Check(), []string{l2}; !reflect.DeepEqual(got, want) { + t.Errorf("Check = %q; want %q", got, want) } - ll.Logf(l1, "bye") + lt.Logf(l1, "bye") - if len(ll.Check()) != len(lineList)-1 { - t.Errorf("expected %v, got %v", lineList, ll.Check()) + if got, want := lt.Check(), []string{l2}; !reflect.DeepEqual(got, want) { + t.Errorf("Check = %q; want %q", got, want) } - ll.Logf(l2, "hi") - if ll.Check() != nil { - t.Errorf("expected empty list, got ll.Check()") + lt.Logf(l2, "hi") + + if got, want := lt.Check(), []string(nil); !reflect.DeepEqual(got, want) { + t.Errorf("Check = %q; want %q", got, want) } }