| 1 | package lock_contest_test |
| 2 | |
| 3 | import ( |
| 4 | "sync" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | . "github.com/onsi/ginkgo/v2" |
| 9 | . "github.com/onsi/gomega" |
| 10 | |
| 11 | "github.com/onsi/ginkgo/v2/integration/_fixtures/profile_fixture/lock_contest" |
| 12 | ) |
| 13 | |
| 14 | func TestLockContest(t *testing.T) { |
| 15 | RegisterFailHandler(Fail) |
| 16 | RunSpecs(t, "LockContest Suite") |
| 17 | } |
| 18 | |
| 19 | var _ = Describe("quick lock blocks", func() { |
| 20 | for i := 0; i < 10; i++ { |
| 21 | It("reads a lock quickly", func() { |
| 22 | c := make(chan bool) |
| 23 | l := &sync.Mutex{} |
| 24 | l.Lock() |
| 25 | go func() { |
| 26 | lock_contest.WaitForLock(l) |
| 27 | close(c) |
| 28 | }() |
| 29 | time.Sleep(5 * time.Millisecond) |
| 30 | l.Unlock() |
| 31 | <-c |
| 32 | }) |
| 33 | } |
| 34 | }) |
| 35 | |
| 36 | var _ = Describe("slow lock block", func() { |
| 37 | It("gets stuck for a bit", func() { |
| 38 | c := make(chan bool) |
| 39 | l := &sync.Mutex{} |
| 40 | l.Lock() |
| 41 | go func() { |
| 42 | lock_contest.SlowWaitForLock(l) |
| 43 | close(c) |
| 44 | }() |
| 45 | time.Sleep(500 * time.Millisecond) |
| 46 | l.Unlock() |
| 47 | <-c |
| 48 | }) |
| 49 | }) |