(t *testing.T)
| 17 | func TestLinkChangeLogLimiter(t *testing.T) { synctest.Test(t, syncTestLinkChangeLogLimiter) } |
| 18 | |
| 19 | func 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…