Skip to content

Commit d5a870b

Browse files
committed
wgengine/monitor: add --monitor-duration flag to opt-in TestMonitorMode
TestMonitorMode skips by default, without the --monitor flag, and then it previously ran forever. This adds an option --monitor-duration flag that defaults to zero (run forever) but if non-zero bounds how long the tests runs. This means you can then also use e.g. `go test --cpuprofile` and capture a CPU/mem profile for a minute or two. Updates tailscale#7621 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
1 parent 162488a commit d5a870b

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

wgengine/monitor/monitor_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package monitor
55

66
import (
77
"flag"
8+
"sync/atomic"
89
"testing"
910
"time"
1011

@@ -55,7 +56,10 @@ func TestMonitorInjectEvent(t *testing.T) {
5556
}
5657
}
5758

58-
var monitor = flag.String("monitor", "", `go into monitor mode like 'route monitor'; test never terminates. Value can be either "raw" or "callback"`)
59+
var (
60+
monitor = flag.String("monitor", "", `go into monitor mode like 'route monitor'; test never terminates. Value can be either "raw" or "callback"`)
61+
monitorDuration = flag.Duration("monitor-duration", 0, "if non-zero, how long to run TestMonitorMode. Zero means forever.")
62+
)
5963

6064
func TestMonitorMode(t *testing.T) {
6165
switch *monitor {
@@ -71,18 +75,38 @@ func TestMonitorMode(t *testing.T) {
7175
}
7276
switch *monitor {
7377
case "raw":
78+
var closed atomic.Bool
79+
if *monitorDuration != 0 {
80+
t := time.AfterFunc(*monitorDuration, func() {
81+
closed.Store(true)
82+
mon.Close()
83+
})
84+
defer t.Stop()
85+
}
7486
for {
7587
msg, err := mon.om.Receive()
88+
if closed.Load() {
89+
return
90+
}
7691
if err != nil {
7792
t.Fatal(err)
7893
}
7994
t.Logf("msg: %#v", msg)
8095
}
8196
case "callback":
97+
var done <-chan time.Time
98+
if *monitorDuration != 0 {
99+
t := time.NewTimer(*monitorDuration)
100+
defer t.Stop()
101+
done = t.C
102+
}
103+
n := 0
82104
mon.RegisterChangeCallback(func(changed bool, st *interfaces.State) {
105+
n++
83106
t.Logf("cb: changed=%v, ifSt=%v", changed, st)
84107
})
85108
mon.Start()
86-
select {}
109+
<-done
110+
t.Logf("%v callbacks", n)
87111
}
88112
}

0 commit comments

Comments
 (0)