Skip to content

Commit

Permalink
Use timeouts instead of relying on runtime.GoSched
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
  • Loading branch information
cpuguy83 committed Nov 12, 2015
1 parent 98054a8 commit fd672d5
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions pkg/locker/locker_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package locker

import (
"runtime"
"testing"
"time"
)

func TestLockCounter(t *testing.T) {
Expand Down Expand Up @@ -34,33 +34,36 @@ func TestLockerLock(t *testing.T) {
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 {
case <-chDone:
t.Fatal("lock should not have returned while it was still held")
default:
}

if ctr.count() != 1 {
t.Fatalf("expected waiters to be 1, got: %d", ctr.count())
}

if err := l.Unlock("test"); err != nil {
t.Fatal(err)
}
runtime.Gosched()

select {
case <-chDone:
default:
// one more time just to be sure
runtime.Gosched()
select {
case <-chDone:
default:
t.Fatalf("lock should have completed")
}
case <-time.After(3 * time.Second):
t.Fatalf("lock should have completed")
}

if ctr.count() != 0 {
Expand All @@ -80,11 +83,9 @@ func TestLockerUnlock(t *testing.T) {
close(chDone)
}()

runtime.Gosched()

select {
case <-chDone:
default:
case <-time.After(3 * time.Second):
t.Fatalf("lock should not be blocked")
}
}

0 comments on commit fd672d5

Please sign in to comment.