MCPcopy
hub / github.com/tailscale/tailscale / syncTestLinkChangeLogLimiter

Function syncTestLinkChangeLogLimiter

net/netmon/loghelper_test.go:19–93  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

17func TestLinkChangeLogLimiter(t *testing.T) { synctest.Test(t, syncTestLinkChangeLogLimiter) }
18
19func syncTestLinkChangeLogLimiter(t *testing.T) {
20 bus := eventbus.New()
21 defer bus.Close()
22 mon, err := New(bus, t.Logf)
23 if err != nil {
24 t.Fatal(err)
25 }
26 defer mon.Close()
27
28 var logBuffer bytes.Buffer
29 logf := func(format string, args ...any) {
30 t.Logf("captured log: "+format, args...)
31
32 if format[len(format)-1] != '\n' {
33 format += "\n"
34 }
35 fmt.Fprintf(&logBuffer, format, args...)
36 }
37
38 ctx, cancel := context.WithCancel(t.Context())
39 defer cancel()
40
41 logf = LinkChangeLogLimiter(ctx, logf, mon)
42
43 // Log once, which should write to our log buffer.
44 logf("hello %s", "world")
45 if got := logBuffer.String(); got != "hello world\n" {
46 t.Errorf("unexpected log buffer contents: %q", got)
47 }
48
49 // Log again, which should not write to our log buffer.
50 logf("hello %s", "andrew")
51 if got := logBuffer.String(); got != "hello world\n" {
52 t.Errorf("unexpected log buffer contents: %q", got)
53 }
54
55 // Log a different message, which should write to our log buffer.
56 logf("other message")
57 if got := logBuffer.String(); got != "hello world\nother message\n" {
58 t.Errorf("unexpected log buffer contents: %q", got)
59 }
60
61 // Synthesize a fake major change event, which should clear the format
62 // string cache and allow the next log to write to our log buffer.
63 //
64 // InjectEvent doesn't work because it's not a major event, so we
65 // instead inject the event ourselves.
66 injector := eventbustest.NewInjector(t, bus)
67 cd, err := NewChangeDelta(nil, &State{}, 0, true)
68 if err != nil {
69 t.Fatal(err)
70 }
71 if cd.RebindLikelyRequired != true {
72 t.Fatalf("expected RebindLikelyRequired to be true, got false")
73 }
74 eventbustest.Inject(injector, cd)
75 synctest.Wait()
76

Callers

nothing calls this directly

Calls 15

NewFunction · 0.92
NewInjectorFunction · 0.92
InjectFunction · 0.92
LinkChangeLogLimiterFunction · 0.85
NewChangeDeltaFunction · 0.85
NewFunction · 0.70
CloseMethod · 0.65
FatalMethod · 0.65
LogfMethod · 0.65
ContextMethod · 0.65
StringMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…