util/eventbus: allow test expectations reporting only an error (#17146)

Extend the Expect method of a Watcher to allow filter functions that report
only an error value, and which "pass" when the reported error is nil.

Updates #15160

Change-Id: I582d804554bd1066a9e499c1f3992d068c9e8148
Signed-off-by: M. J. Fromberger <fromberger@tailscale.com>
This commit is contained in:
M. J. Fromberger
2025-09-17 07:20:34 -07:00
committed by GitHub
parent db0b9a361c
commit 48029a897d
2 changed files with 39 additions and 6 deletions
@@ -54,6 +54,27 @@ func TestExpectFilter(t *testing.T) {
},
wantErr: false,
},
{
name: "filter-with-nil-error",
events: []int{1, 2, 3},
expectFunc: func(event EventFoo) error {
if event.Value > 10 {
return fmt.Errorf("value > 10: %d", event.Value)
}
return nil
},
},
{
name: "filter-with-non-nil-error",
events: []int{100, 200, 300},
expectFunc: func(event EventFoo) error {
if event.Value > 10 {
return fmt.Errorf("value > 10: %d", event.Value)
}
return nil
},
wantErr: true,
},
{
name: "first event has to be func",
events: []int{24, 42},