mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
We see number of failed test in Github Action, mostly on MacOS or Windows due to the fact that goroutines are scheduled to be run consequently. This commit improves the test, ensuring at least 2 goroutines were started before increasing the counting.
43 lines
709 B
Go
43 lines
709 B
Go
package cli
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
)
|
|
|
|
func Test_loopGuard(t *testing.T) {
|
|
lg := newLoopGuard()
|
|
key := "foo"
|
|
|
|
var i atomic.Int64
|
|
var started atomic.Int64
|
|
n := 1000
|
|
do := func() {
|
|
locked := lg.TryLock(key)
|
|
defer lg.Unlock(key)
|
|
started.Add(1)
|
|
for started.Load() < 2 {
|
|
// Wait until at least 2 goroutines started, otherwise, on system with heavy load,
|
|
// or having only 1 CPU, all goroutines can be scheduled to run consequently.
|
|
}
|
|
if locked {
|
|
i.Add(1)
|
|
}
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(n)
|
|
for i := 0; i < n; i++ {
|
|
go func() {
|
|
defer wg.Done()
|
|
do()
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
|
|
if i.Load() == int64(n) {
|
|
t.Fatalf("i must not be increased %d times", n)
|
|
}
|
|
}
|