From 518bd56d47d00d909100219f29df15ca0c93dbba 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 --- go.mod | 4 +--- go.sum | 4 ++-- pkg/compose/watch.go | 6 +++--- pkg/compose/watch_test.go | 23 +++++++++++++++++------ 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index eb3a7fb2e18..07c86889cb9 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/Microsoft/go-winio v0.6.2 github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d github.com/buger/goterm v1.0.4 - github.com/compose-spec/compose-go/v2 v2.4.5 + github.com/compose-spec/compose-go/v2 v2.4.6-0.20241203131247-9a9cc5d9c345 github.com/containerd/containerd v1.7.24 github.com/containerd/platforms v0.2.1 github.com/davecgh/go-spew v1.1.1 @@ -196,5 +196,3 @@ require ( sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect sigs.k8s.io/yaml v1.3.0 // indirect ) - -replace github.com/compose-spec/compose-go/v2 => github.com/ndeloof/compose-go/v2 v2.0.1-0.20241127110655-b1321070b3ab diff --git a/go.sum b/go.sum index d4b2f3eef4a..f98f4b26333 100644 --- a/go.sum +++ b/go.sum @@ -85,6 +85,8 @@ github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78 h1:QVw89YDxXxEe+l8gU8E github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78/go.mod h1:W+zGtBO5Y1IgJhy4+A9GOqVhqLpfZi+vwmdNXUehLA8= github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE= github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4= +github.com/compose-spec/compose-go/v2 v2.4.6-0.20241203131247-9a9cc5d9c345 h1:oLm7hga9jjaDedg+dqsWiI1GeRrcGLBPxu8W0VfpiKA= +github.com/compose-spec/compose-go/v2 v2.4.6-0.20241203131247-9a9cc5d9c345/go.mod h1:lFN0DrMxIncJGYAXTfWuajfwj5haBJqrBkarHcnjJKc= github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM= github.com/containerd/cgroups/v3 v3.0.2 h1:f5WFqIVSgo5IZmtTT3qVBo6TzI1ON6sycSBKkymb9L0= github.com/containerd/cgroups/v3 v3.0.2/go.mod h1:JUgITrzdFqp42uI2ryGA+ge0ap/nxzYgkGmIcetmErE= @@ -357,8 +359,6 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J1GEMiLbxo1LJaP8RfCpH6pymGZus= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= -github.com/ndeloof/compose-go/v2 v2.0.1-0.20241127110655-b1321070b3ab h1:3Q4/1sAnPv4nMpak/lIzWsQJjX8X5zKZRkDd6mlf2mc= -github.com/ndeloof/compose-go/v2 v2.0.1-0.20241127110655-b1321070b3ab/go.mod h1:lFN0DrMxIncJGYAXTfWuajfwj5haBJqrBkarHcnjJKc= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.0 h1:Iw5WCbBcaAAd0fpRb1c9r5YCylv4XDoCSigm1zLevwU= 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") }