Skip to content

Commit

Permalink
the race conditions in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
2403905 committed Jan 4, 2024
1 parent 0f9f996 commit fd38865
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/fix-race-in-tests.md
Original file line number Diff line number Diff line change
@@ -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
22 changes: 14 additions & 8 deletions services/search/pkg/search/debouncer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package search_test

import (
"sync/atomic"
"time"

user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
Expand All @@ -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",
Expand All @@ -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)
}
})
})

Expand All @@ -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))
})

Expand All @@ -49,24 +53,26 @@ 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)
time.Sleep(100 * time.Millisecond) // Let it trigger once

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))
})
})
7 changes: 4 additions & 3 deletions services/search/pkg/search/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand All @@ -32,15 +33,15 @@ 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)
})
}

err := events.Publish(context.Background(), bus, e)

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),
Expand Down

0 comments on commit fd38865

Please sign in to comment.