Chromium Code Reviews| 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 |
| 16 // trashTimer is a useless implementation of clock.Timer specifically designed | |
| 17 // to exist and not be a test timer type. | |
|
iannucci
2016/02/10 22:30:28
move into testTimer_test.go.test.tester.go
dnj (Google)
2016/02/11 01:26:54
Done.
| |
| 18 type trashTimer struct { | |
| 19 clock.Timer | |
| 20 } | |
| 21 | |
| 15 func TestTestClock(t *testing.T) { | 22 func TestTestClock(t *testing.T) { |
| 16 t.Parallel() | 23 t.Parallel() |
| 17 | 24 |
| 18 Convey(`A testing clock instance`, t, func() { | 25 Convey(`A testing clock instance`, t, func() { |
| 19 now := time.Date(2015, 01, 01, 00, 00, 00, 00, time.UTC) | 26 now := time.Date(2015, 01, 01, 00, 00, 00, 00, time.UTC) |
| 20 » » c := New(now) | 27 » » ctx, clk := UseTime(context.Background(), now) |
| 21 | 28 |
| 22 Convey(`Returns the current time.`, func() { | 29 Convey(`Returns the current time.`, func() { |
| 23 » » » So(c.Now(), ShouldResemble, now) | 30 » » » So(clk.Now(), ShouldResemble, now) |
| 24 }) | 31 }) |
| 25 | 32 |
| 26 Convey(`When sleeping with a time of zero, immediately awakens.` , func() { | 33 Convey(`When sleeping with a time of zero, immediately awakens.` , func() { |
| 27 » » » c.Sleep(0) | 34 » » » clk.Sleep(ctx, 0) |
| 28 » » » So(c.Now(), ShouldResemble, now) | 35 » » » So(clk.Now(), ShouldResemble, now) |
| 36 » » }) | |
| 37 | |
| 38 » » Convey(`Will panic if going backwards in time.`, func() { | |
| 39 » » » So(func() { clk.Add(-1 * time.Second) }, ShouldPanic) | |
| 29 }) | 40 }) |
| 30 | 41 |
| 31 Convey(`When sleeping for a period of time, awakens when signall ed.`, func() { | 42 Convey(`When sleeping for a period of time, awakens when signall ed.`, func() { |
| 32 sleepingC := make(chan struct{}) | 43 sleepingC := make(chan struct{}) |
| 33 » » » c.SetTimerCallback(func(_ time.Duration, _ clock.Timer) { | 44 » » » clk.SetTimerCallback(func(_ time.Duration, _ clock.Timer ) { |
| 34 close(sleepingC) | 45 close(sleepingC) |
| 35 }) | 46 }) |
| 36 | 47 |
| 37 awakeC := make(chan time.Time) | 48 awakeC := make(chan time.Time) |
| 38 go func() { | 49 go func() { |
| 39 » » » » c.Sleep(2 * time.Second) | 50 » » » » clk.Sleep(ctx, 2*time.Second) |
| 40 » » » » awakeC <- c.Now() | 51 » » » » awakeC <- clk.Now() |
| 41 }() | 52 }() |
| 42 | 53 |
| 43 <-sleepingC | 54 <-sleepingC |
| 44 » » » c.Set(now.Add(1 * time.Second)) | 55 » » » clk.Set(now.Add(1 * time.Second)) |
| 45 » » » c.Set(now.Add(2 * time.Second)) | 56 » » » clk.Set(now.Add(2 * time.Second)) |
| 46 So(<-awakeC, ShouldResemble, now.Add(2*time.Second)) | 57 So(<-awakeC, ShouldResemble, now.Add(2*time.Second)) |
| 47 }) | 58 }) |
| 48 | 59 |
| 49 Convey(`Awakens after a period of time.`, func() { | 60 Convey(`Awakens after a period of time.`, func() { |
| 50 » » » afterC := c.After(2 * time.Second) | 61 » » » afterC := clk.After(ctx, 2*time.Second) |
| 51 | 62 |
| 52 » » » c.Set(now.Add(1 * time.Second)) | 63 » » » clk.Set(now.Add(1 * time.Second)) |
| 53 » » » c.Set(now.Add(2 * time.Second)) | 64 » » » clk.Set(now.Add(2 * time.Second)) |
| 54 » » » So(<-afterC, ShouldResemble, now.Add(2*time.Second)) | 65 » » » So(<-afterC, ShouldResemble, clock.TimerResult{now.Add(2 * time.Second), nil}) |
| 66 » » }) | |
| 67 | |
| 68 » » Convey(`When sleeping, awakens if canceled.`, func() { | |
| 69 » » » ctx, cancelFunc := context.WithCancel(ctx) | |
| 70 | |
| 71 » » » clk.SetTimerCallback(func(_ time.Duration, _ clock.Timer ) { | |
| 72 » » » » cancelFunc() | |
| 73 » » » }) | |
| 74 | |
| 75 » » » So(clk.Sleep(ctx, time.Second), ShouldEqual, context.Can celed) | |
| 55 }) | 76 }) |
| 56 }) | 77 }) |
| 57 } | 78 } |
| OLD | NEW |