net/netmon, wgengine/magicsock: simplify LinkChangeLogLimiter signature

Remove the need for the caller to hold on to and call an unregister
function. Both two callers (one real, one test) already have a context
they can use. Use context.AfterFunc instead. There are no observable
side effects from scheduling too late if the goroutine doesn't run sync.

Updates #17148

Change-Id: Ie697dae0e797494fa8ef27fbafa193bfe5ceb307
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2025-09-15 15:49:56 -07:00
committed by Brad Fitzpatrick
parent 5c24f0ed80
commit 8b48f3847d
3 changed files with 25 additions and 18 deletions

View File

@@ -5,13 +5,17 @@ package netmon
import (
"bytes"
"context"
"fmt"
"testing"
"testing/synctest"
"tailscale.com/util/eventbus"
)
func TestLinkChangeLogLimiter(t *testing.T) {
func TestLinkChangeLogLimiter(t *testing.T) { synctest.Test(t, syncTestLinkChangeLogLimiter) }
func syncTestLinkChangeLogLimiter(t *testing.T) {
bus := eventbus.New()
defer bus.Close()
mon, err := New(bus, t.Logf)
@@ -30,8 +34,10 @@ func TestLinkChangeLogLimiter(t *testing.T) {
fmt.Fprintf(&logBuffer, format, args...)
}
logf, unregister := LinkChangeLogLimiter(logf, mon)
defer unregister()
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
logf = LinkChangeLogLimiter(ctx, logf, mon)
// Log once, which should write to our log buffer.
logf("hello %s", "world")
@@ -72,8 +78,11 @@ func TestLinkChangeLogLimiter(t *testing.T) {
t.Errorf("unexpected log buffer contents: %q", got)
}
// Unregistering the callback should clear our 'cbs' set.
unregister()
// Canceling the context we passed to LinkChangeLogLimiter should
// unregister the callback from the netmon.
cancel()
synctest.Wait()
mon.mu.Lock()
if len(mon.cbs) != 0 {
t.Errorf("expected no callbacks, got %v", mon.cbs)