From 3237600f1cdc6cc99bd7bdcb824a1d5120514800 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 28 Nov 2024 09:35:45 +0100 Subject: [PATCH] revisit TestDebounceBatching Signed-off-by: Nicolas De Loof --- pkg/compose/watch.go | 6 +++--- pkg/compose/watch_test.go | 23 +++++++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/pkg/compose/watch.go b/pkg/compose/watch.go index 191cf585cb7..8761db126c9 100644 --- a/pkg/compose/watch.go +++ b/pkg/compose/watch.go @@ -355,7 +355,7 @@ func batchDebounceEvents(ctx context.Context, clock clockwork.Clock, delay time. out := make(chan []fileEvent) go func() { defer close(out) - seen := make(map[sync.PathMapping]fileEvent) + seen := make(map[string]fileEvent) flushEvents := func() { if len(seen) == 0 { return @@ -365,7 +365,7 @@ func batchDebounceEvents(ctx context.Context, clock clockwork.Clock, delay time. events = append(events, e) } out <- events - seen = make(map[sync.PathMapping]fileEvent) + seen = make(map[string]fileEvent) } t := clock.NewTicker(delay) @@ -382,7 +382,7 @@ func batchDebounceEvents(ctx context.Context, clock clockwork.Clock, delay time. flushEvents() return } - seen[e.PathMapping] = e + seen[e.HostPath] = e t.Reset(delay) } } diff --git a/pkg/compose/watch_test.go b/pkg/compose/watch_test.go index cfe4095cfc0..0bd1ee16f22 100644 --- a/pkg/compose/watch_test.go +++ b/pkg/compose/watch_test.go @@ -18,6 +18,8 @@ import ( "context" "fmt" "os" + "slices" + "strings" "testing" "time" @@ -42,23 +44,32 @@ func TestDebounceBatching(t *testing.T) { ctx, stop := context.WithCancel(context.Background()) t.Cleanup(stop) + trigger := types.Trigger{ + Path: "/", + } + matcher := watch.EmptyMatcher{} eventBatchCh := batchDebounceEvents(ctx, clock, quietPeriod, ch) for i := 0; i < 100; i++ { - var action types.WatchAction = "a" + var path = "/a" if i%2 == 0 { - action = "b" + path = "/b" } - ch <- fileEvent{Trigger: types.Trigger{Action: action}} + + event := maybeFileEvent(trigger, path, matcher) + require.NotNil(t, event) + ch <- *event } // we sent 100 events + the debouncer clock.BlockUntil(101) clock.Advance(quietPeriod) select { case batch := <-eventBatchCh: - require.ElementsMatch(t, batch, []fileEvent{ - {Trigger: types.Trigger{Action: "a"}}, - {Trigger: types.Trigger{Action: "b"}}, + slices.SortFunc(batch, func(a, b fileEvent) int { + return strings.Compare(a.HostPath, b.HostPath) }) + assert.Equal(t, len(batch), 2) + assert.Equal(t, batch[0].HostPath, "/a") + assert.Equal(t, batch[1].HostPath, "/b") case <-time.After(50 * time.Millisecond): t.Fatal("timed out waiting for events") }