ipn: simplify TestLocalLogLines, defer a Shutdown of its LocalBackend

The test's LocalBackend was not shut down (Shutdown both releases
resources and waits for its various goroutines to end). This should
fix the test race we were seeing. It definitely fixes the file
descriptor leak that preventing -race -count=500 from passing before.
reviewable/pr737/r1
Brad Fitzpatrick 2020-09-04 08:36:07 -07:00
parent 7fddc33481
commit 8ecee476f6
1 changed files with 7 additions and 25 deletions

View File

@ -5,6 +5,7 @@
package ipn package ipn
import ( import (
"reflect"
"testing" "testing"
"time" "time"
@ -48,6 +49,7 @@ func TestLocalLogLines(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer lb.Shutdown()
// custom adjustments for required non-nil fields // custom adjustments for required non-nil fields
lb.prefs = NewPrefs() lb.prefs = NewPrefs()
@ -55,30 +57,10 @@ func TestLocalLogLines(t *testing.T) {
// hacky manual override of the usual log-on-change behaviour of keylogf // hacky manual override of the usual log-on-change behaviour of keylogf
lb.keyLogf = logListen.Logf lb.keyLogf = logListen.Logf
// testing infrastructure testWantRemain := func(wantRemain ...string) func(t *testing.T) {
type linesTest struct {
name string
want []string
}
tests := []linesTest{
{
name: "after prefs",
want: []string{
"peer keys: %s",
"v%v peers: %v",
},
},
{
name: "after peers",
want: []string{},
},
}
testLogs := func(want linesTest) func(t *testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
if linesLeft := logListen.Check(); len(linesLeft) != len(want.want) { if remain := logListen.Check(); !reflect.DeepEqual(remain, wantRemain) {
t.Errorf("got %v, expected %v", linesLeft, want) t.Errorf("remain %q, want %q", remain, wantRemain)
} }
} }
} }
@ -89,7 +71,7 @@ func TestLocalLogLines(t *testing.T) {
prefs.Persist = persist prefs.Persist = persist
lb.SetPrefs(prefs) lb.SetPrefs(prefs)
t.Run(tests[0].name, testLogs(tests[0])) t.Run("after_prefs", testWantRemain("peer keys: %s", "v%v peers: %v"))
// log peers, peer keys // log peers, peer keys
status := &wgengine.Status{ status := &wgengine.Status{
@ -103,5 +85,5 @@ func TestLocalLogLines(t *testing.T) {
} }
lb.parseWgStatus(status) lb.parseWgStatus(status)
t.Run(tests[1].name, testLogs(tests[1])) t.Run("after_peers", testWantRemain())
} }