From fd388653a98e2945f52d5d9c3ad731cf76016aa7 Mon Sep 17 00:00:00 2001 From: Roman Perekhod Date: Wed, 29 Nov 2023 13:44:36 +0100 Subject: [PATCH] the race conditions in tests --- changelog/unreleased/fix-race-in-tests.md | 6 ++++++ services/search/pkg/search/debouncer_test.go | 22 +++++++++++++------- services/search/pkg/search/events_test.go | 7 ++++--- 3 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 changelog/unreleased/fix-race-in-tests.md diff --git a/changelog/unreleased/fix-race-in-tests.md b/changelog/unreleased/fix-race-in-tests.md new file mode 100644 index 0000000000..46cc972987 --- /dev/null +++ b/changelog/unreleased/fix-race-in-tests.md @@ -0,0 +1,6 @@ +Bugfix: the race conditions in tests + +We fixed the race conditions in tests. + +https://github.com/owncloud/ocis/pull/7847 +https://github.com/owncloud/ocis/issues/7846 diff --git a/services/search/pkg/search/debouncer_test.go b/services/search/pkg/search/debouncer_test.go index f4f9693275..901d2293b1 100644 --- a/services/search/pkg/search/debouncer_test.go +++ b/services/search/pkg/search/debouncer_test.go @@ -1,6 +1,7 @@ package search_test import ( + "sync/atomic" "time" user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" @@ -13,7 +14,8 @@ import ( var _ = Describe("SpaceDebouncer", func() { var ( debouncer *search.SpaceDebouncer - callCount map[string]int + + callCount atomic.Int32 userId = &user.UserId{ OpaqueId: "user", @@ -24,9 +26,11 @@ var _ = Describe("SpaceDebouncer", func() { ) BeforeEach(func() { - callCount = map[string]int{} + callCount = atomic.Int32{} debouncer = search.NewSpaceDebouncer(50*time.Millisecond, func(id *sprovider.StorageSpaceId, _ *user.UserId) { - callCount[id.OpaqueId] += 1 + if id.OpaqueId == "spaceid" { + callCount.Add(1) + } }) }) @@ -35,7 +39,7 @@ var _ = Describe("SpaceDebouncer", func() { debouncer.Debounce(spaceid, userId) debouncer.Debounce(spaceid, userId) Eventually(func() int { - return callCount["spaceid"] + return int(callCount.Load()) }, "200ms").Should(Equal(1)) }) @@ -49,13 +53,15 @@ var _ = Describe("SpaceDebouncer", func() { debouncer.Debounce(spaceid, userId) Eventually(func() int { - return callCount["spaceid"] + return int(callCount.Load()) }, "200ms").Should(Equal(2)) }) It("doesn't trigger twice simultaneously", func() { debouncer = search.NewSpaceDebouncer(50*time.Millisecond, func(id *sprovider.StorageSpaceId, _ *user.UserId) { - callCount[id.OpaqueId] += 1 + if id.OpaqueId == "spaceid" { + callCount.Add(1) + } time.Sleep(300 * time.Millisecond) }) debouncer.Debounce(spaceid, userId) @@ -63,10 +69,10 @@ var _ = Describe("SpaceDebouncer", func() { debouncer.Debounce(spaceid, userId) time.Sleep(100 * time.Millisecond) // shouldn't trigger as the other run is still in progress - Expect(callCount["spaceid"]).To(Equal(1)) + Expect(int(callCount.Load())).To(Equal(1)) Eventually(func() int { - return callCount["spaceid"] + return int(callCount.Load()) }, "500ms").Should(Equal(2)) }) }) diff --git a/services/search/pkg/search/events_test.go b/services/search/pkg/search/events_test.go index bd010cb1f4..db5f56552a 100644 --- a/services/search/pkg/search/events_test.go +++ b/services/search/pkg/search/events_test.go @@ -2,6 +2,7 @@ package search_test import ( "context" + "sync/atomic" userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" "github.com/cs3org/reva/v2/pkg/events" @@ -19,7 +20,7 @@ var _ = DescribeTable("events", func(mcks []string, e interface{}, asyncUploads bool) { var ( s = &searchMocks.Searcher{} - calls int + calls atomic.Int32 ) bus, _ := mEvents.NewStream() @@ -32,7 +33,7 @@ var _ = DescribeTable("events", for _, mck := range mcks { s.On(mck, mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) { - calls += 1 + calls.Add(1) }) } @@ -40,7 +41,7 @@ var _ = DescribeTable("events", Expect(err).To(BeNil()) Eventually(func() int { - return calls + return int(calls.Load()) }, "2s").Should(Equal(len(mcks))) }, Entry("ItemTrashed", []string{"TrashItem", "IndexSpace"}, events.ItemTrashed{}, false),