Skip to content

Commit

Permalink
Explain synchronization logic in more detail
Browse files Browse the repository at this point in the history
Signed-off-by: Paschalis Tsilias <paschalis.tsilias@grafana.com>
  • Loading branch information
tpaschalis committed Aug 14, 2024
1 parent 273d37b commit 76e1358
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 8 additions & 4 deletions client/internal/httpsender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ func TestPackageUpdatesInParallel(t *testing.T) {
ch := make(chan struct{})
doneCh := make([]<-chan struct{}, 0)

// Use this to simulate blocking behavior on the first call to Sync().
// Use `ch` to simulate blocking behavior on the first call to Sync().
// This will allow both Sync() calls to be called in parallel; we will
// first make sure that both are inflight before manually releaseing the
// channel so that both go through in sequence.
localPackageState.onAllPackagesHash = func() {
if localPackageState.lastReportedStatuses != nil {
<-ch
Expand Down Expand Up @@ -273,9 +276,10 @@ func TestPackageUpdatesInParallel(t *testing.T) {
},
})

// Make sure that both Sync calls have gone through _before_ releasing the first.
// This means that they're both called in parallel, but locking makes sure
// that this is not a race condition.
// Make sure that both Sync calls have gone through _before_ releasing the first one.
// This means that they're both called in parallel, and that the race
// detector would always report a race condition, but proper locking makes
// sure that's not the case.
assert.Eventually(t, func() bool {
return messages.Load() == 2
}, 2*time.Second, 100*time.Millisecond, "both messages must have been processed successfully")
Expand Down
12 changes: 8 additions & 4 deletions client/internal/wsreceiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ func TestWSPackageUpdatesInParallel(t *testing.T) {
doneCh := make([]<-chan struct{}, 0)
localPackageState := NewInMemPackagesStore()

// Use this to simulate blocking behavior on the first call to Sync().
// Use `ch` to simulate blocking behavior on the first call to Sync().
// This will allow both Sync() calls to be called in parallel; we will
// first make sure that both are inflight before manually releaseing the
// channel so that both go through in sequence.
localPackageState.onAllPackagesHash = func() {
if localPackageState.lastReportedStatuses != nil {
<-ch
Expand Down Expand Up @@ -281,9 +284,10 @@ func TestWSPackageUpdatesInParallel(t *testing.T) {
},
})

// Make sure that both Sync calls have gone through _before_ releasing the first.
// This means that they're both called in parallel, but locking makes sure
// that this is not a race condition.
// Make sure that both Sync calls have gone through _before_ releasing the first one.
// This means that they're both called in parallel, and that the race
// detector would always report a race condition, but proper locking makes
// sure that's not the case.
assert.Eventually(t, func() bool {
return messages.Load() == 2
}, 2*time.Second, 100*time.Millisecond, "both messages must have been processed successfully")
Expand Down

0 comments on commit 76e1358

Please sign in to comment.