Merge pull request #17925 from cpuguy83/fix_locker_unit_tests
Use timeouts instead of relying on runtime.GoSched
This commit is contained in:
commit
82e1e2ee36
1 changed files with 19 additions and 18 deletions
|
@ -1,8 +1,8 @@
|
||||||
package locker
|
package locker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLockCounter(t *testing.T) {
|
func TestLockCounter(t *testing.T) {
|
||||||
|
@ -34,7 +34,21 @@ func TestLockerLock(t *testing.T) {
|
||||||
close(chDone)
|
close(chDone)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
runtime.Gosched()
|
chWaiting := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
for range time.Tick(1 * time.Millisecond) {
|
||||||
|
if ctr.count() == 1 {
|
||||||
|
close(chWaiting)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-chWaiting:
|
||||||
|
case <-time.After(3 * time.Second):
|
||||||
|
t.Fatal("timed out waiting for lock waiters to be incremented")
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-chDone:
|
case <-chDone:
|
||||||
|
@ -42,26 +56,15 @@ func TestLockerLock(t *testing.T) {
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctr.count() != 1 {
|
|
||||||
t.Fatalf("expected waiters to be 1, got: %d", ctr.count())
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := l.Unlock("test"); err != nil {
|
if err := l.Unlock("test"); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
runtime.Gosched()
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-chDone:
|
case <-chDone:
|
||||||
default:
|
case <-time.After(3 * time.Second):
|
||||||
// one more time just to be sure
|
|
||||||
runtime.Gosched()
|
|
||||||
select {
|
|
||||||
case <-chDone:
|
|
||||||
default:
|
|
||||||
t.Fatalf("lock should have completed")
|
t.Fatalf("lock should have completed")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ctr.count() != 0 {
|
if ctr.count() != 0 {
|
||||||
t.Fatalf("expected waiters to be 0, got: %d", ctr.count())
|
t.Fatalf("expected waiters to be 0, got: %d", ctr.count())
|
||||||
|
@ -80,11 +83,9 @@ func TestLockerUnlock(t *testing.T) {
|
||||||
close(chDone)
|
close(chDone)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
runtime.Gosched()
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-chDone:
|
case <-chDone:
|
||||||
default:
|
case <-time.After(3 * time.Second):
|
||||||
t.Fatalf("lock should not be blocked")
|
t.Fatalf("lock should not be blocked")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue