-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: reserve wg caused data race in tests (#4230)
- Loading branch information
Showing
4 changed files
with
111 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2023 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package util | ||
|
||
import ( | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
// A WaitingCounter waits for a counter to go to zero. | ||
type WaitingCounter atomic.Int32 | ||
|
||
// Add increments the counter. | ||
// As opposed to sync.WaitGroup, it is safe to be called after Wait has been called. | ||
func (r *WaitingCounter) Add(c int32) { | ||
(*atomic.Int32)(r).Add(c) | ||
} | ||
|
||
// Done decrements the counter. | ||
// If the counter goes bellow zero a panic will be raised. | ||
func (r *WaitingCounter) Done() { | ||
if nv := (*atomic.Int32)(r).Add(-1); nv < 0 { | ||
panic("negative counter value") | ||
} | ||
} | ||
|
||
// Wait blocks waiting for the counter value to reach zero or for the timeout to be reached. | ||
// Is guaranteed to wait for at least a hundred milliseconds regardless of given duration if the counter is positive value. | ||
func (r *WaitingCounter) Wait(waitFor time.Duration) int { | ||
deadline := time.Now().Add(waitFor) | ||
for { | ||
c := int((*atomic.Int32)(r).Load()) | ||
|
||
if c <= 0 { // we're done | ||
return c | ||
} | ||
|
||
if time.Now().After(deadline) { // timeout | ||
return c | ||
} | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2023 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package util_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/ethersphere/bee/pkg/util" | ||
) | ||
|
||
func TestEarlyDone(t *testing.T) { | ||
wc := new(util.WaitingCounter) | ||
|
||
wc.Add(1) | ||
|
||
s := time.Now() | ||
|
||
go func() { | ||
time.Sleep(200 * time.Millisecond) | ||
wc.Done() | ||
}() | ||
|
||
r := wc.Wait(time.Second) | ||
|
||
if r != 0 { | ||
t.Fatal("counter not zero") | ||
} | ||
|
||
if time.Since(s) >= time.Second { | ||
t.Fatal("async done not detected") | ||
} | ||
} | ||
|
||
func TestDeadline(t *testing.T) { | ||
wc := new(util.WaitingCounter) | ||
|
||
wc.Add(1) | ||
|
||
s := time.Now() | ||
|
||
r := wc.Wait(200 * time.Millisecond) | ||
|
||
if r != 1 { | ||
t.Fatal("counter is zero, expected 1") | ||
} | ||
|
||
if time.Since(s) < 200*time.Millisecond { | ||
t.Fatal("expected to wait at least given duration") | ||
} | ||
} |