| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package bundler |
| 6 |
| 7 import ( |
| 8 "sync" |
| 9 "testing" |
| 10 "time" |
| 11 |
| 12 "github.com/luci/luci-go/common/clock" |
| 13 "github.com/luci/luci-go/common/clock/testclock" |
| 14 . "github.com/smartystreets/goconvey/convey" |
| 15 ) |
| 16 |
| 17 func TestTimeoutCond(t *testing.T) { |
| 18 Convey(`A timeout Cond bound to a lock`, t, func() { |
| 19 l := sync.Mutex{} |
| 20 tc := testclock.New(time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)) |
| 21 c := newTimeoutCond(tc, &l) |
| 22 |
| 23 Convey(`Will wait without a timer if the duration is <0`, func()
{ |
| 24 didSetTimer := false |
| 25 tc.SetTimerCallback(func(time.Duration, clock.Timer) { |
| 26 didSetTimer = true |
| 27 }) |
| 28 |
| 29 l.Lock() |
| 30 defer l.Unlock() |
| 31 |
| 32 // Signal the Cond until we unblock. |
| 33 go func() { |
| 34 l.Lock() |
| 35 defer l.Unlock() |
| 36 |
| 37 c.Signal() |
| 38 }() |
| 39 |
| 40 timeout := c.waitTimeout(-1) |
| 41 So(didSetTimer, ShouldBeFalse) |
| 42 So(timeout, ShouldBeFalse) |
| 43 }) |
| 44 |
| 45 Convey(`Will timeout after the duration expires.`, func() { |
| 46 tc.SetTimerCallback(func(time.Duration, clock.Timer) { |
| 47 tc.Add(time.Second) |
| 48 }) |
| 49 |
| 50 l.Lock() |
| 51 defer l.Unlock() |
| 52 |
| 53 timeout := c.waitTimeout(time.Second) |
| 54 So(timeout, ShouldBeTrue) |
| 55 }) |
| 56 |
| 57 Convey(`Will not timeout if signalled before the timeout.`, func
() { |
| 58 tc.SetTimerCallback(func(time.Duration, clock.Timer) { |
| 59 tc.Add(time.Second) |
| 60 }) |
| 61 |
| 62 l.Lock() |
| 63 defer l.Unlock() |
| 64 |
| 65 t := tc.NewTimer() |
| 66 t.Reset(time.Second) |
| 67 go func() { |
| 68 <-t.GetC() |
| 69 c.Signal() |
| 70 }() |
| 71 |
| 72 timeout := c.waitTimeout(5 * time.Second) |
| 73 So(timeout, ShouldBeFalse) |
| 74 }) |
| 75 }) |
| 76 } |
| OLD | NEW |