OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package testclock | 5 package testclock |
6 | 6 |
7 import ( | 7 import ( |
8 "testing" | 8 "testing" |
9 "time" | 9 "time" |
10 | 10 |
11 "github.com/luci/luci-go/common/clock" | 11 "github.com/luci/luci-go/common/clock" |
12 . "github.com/smartystreets/goconvey/convey" | 12 . "github.com/smartystreets/goconvey/convey" |
| 13 "golang.org/x/net/context" |
13 ) | 14 ) |
14 | 15 |
15 func TestTestClock(t *testing.T) { | 16 func TestTestClock(t *testing.T) { |
16 t.Parallel() | 17 t.Parallel() |
17 | 18 |
18 Convey(`A testing clock instance`, t, func() { | 19 Convey(`A testing clock instance`, t, func() { |
19 now := time.Date(2015, 01, 01, 00, 00, 00, 00, time.UTC) | 20 now := time.Date(2015, 01, 01, 00, 00, 00, 00, time.UTC) |
20 » » c := New(now) | 21 » » ctx, clk := UseTime(context.Background(), now) |
21 | 22 |
22 Convey(`Returns the current time.`, func() { | 23 Convey(`Returns the current time.`, func() { |
23 » » » So(c.Now(), ShouldResemble, now) | 24 » » » So(clk.Now(), ShouldResemble, now) |
24 }) | 25 }) |
25 | 26 |
26 Convey(`When sleeping with a time of zero, immediately awakens.`
, func() { | 27 Convey(`When sleeping with a time of zero, immediately awakens.`
, func() { |
27 » » » c.Sleep(0) | 28 » » » clk.Sleep(ctx, 0) |
28 » » » So(c.Now(), ShouldResemble, now) | 29 » » » So(clk.Now(), ShouldResemble, now) |
| 30 » » }) |
| 31 |
| 32 » » Convey(`Will panic if going backwards in time.`, func() { |
| 33 » » » So(func() { clk.Add(-1 * time.Second) }, ShouldPanic) |
29 }) | 34 }) |
30 | 35 |
31 Convey(`When sleeping for a period of time, awakens when signall
ed.`, func() { | 36 Convey(`When sleeping for a period of time, awakens when signall
ed.`, func() { |
32 sleepingC := make(chan struct{}) | 37 sleepingC := make(chan struct{}) |
33 » » » c.SetTimerCallback(func(_ time.Duration, _ clock.Timer)
{ | 38 » » » clk.SetTimerCallback(func(_ time.Duration, _ clock.Timer
) { |
34 close(sleepingC) | 39 close(sleepingC) |
35 }) | 40 }) |
36 | 41 |
37 awakeC := make(chan time.Time) | 42 awakeC := make(chan time.Time) |
38 go func() { | 43 go func() { |
39 » » » » c.Sleep(2 * time.Second) | 44 » » » » clk.Sleep(ctx, 2*time.Second) |
40 » » » » awakeC <- c.Now() | 45 » » » » awakeC <- clk.Now() |
41 }() | 46 }() |
42 | 47 |
43 <-sleepingC | 48 <-sleepingC |
44 » » » c.Set(now.Add(1 * time.Second)) | 49 » » » clk.Set(now.Add(1 * time.Second)) |
45 » » » c.Set(now.Add(2 * time.Second)) | 50 » » » clk.Set(now.Add(2 * time.Second)) |
46 So(<-awakeC, ShouldResemble, now.Add(2*time.Second)) | 51 So(<-awakeC, ShouldResemble, now.Add(2*time.Second)) |
47 }) | 52 }) |
48 | 53 |
49 Convey(`Awakens after a period of time.`, func() { | 54 Convey(`Awakens after a period of time.`, func() { |
50 » » » afterC := c.After(2 * time.Second) | 55 » » » afterC := clk.After(ctx, 2*time.Second) |
51 | 56 |
52 » » » c.Set(now.Add(1 * time.Second)) | 57 » » » clk.Set(now.Add(1 * time.Second)) |
53 » » » c.Set(now.Add(2 * time.Second)) | 58 » » » clk.Set(now.Add(2 * time.Second)) |
54 » » » So(<-afterC, ShouldResemble, now.Add(2*time.Second)) | 59 » » » So(<-afterC, ShouldResemble, clock.TimerResult{now.Add(2
* time.Second), nil}) |
| 60 » » }) |
| 61 |
| 62 » » Convey(`When sleeping, awakens if canceled.`, func() { |
| 63 » » » ctx, cancelFunc := context.WithCancel(ctx) |
| 64 |
| 65 » » » clk.SetTimerCallback(func(_ time.Duration, _ clock.Timer
) { |
| 66 » » » » cancelFunc() |
| 67 » » » }) |
| 68 |
| 69 » » » So(clk.Sleep(ctx, time.Second), ShouldEqual, context.Can
celed) |
55 }) | 70 }) |
56 }) | 71 }) |
57 } | 72 } |
OLD | NEW |