Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(550)

Side by Side Diff: common/clock/testclock/testclock_test.go

Issue 1679023005: Add Context cancellation to clock. (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Actually upload the patch. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « common/clock/testclock/testclock.go ('k') | common/clock/testclock/testtimer.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 }
OLDNEW
« no previous file with comments | « common/clock/testclock/testclock.go ('k') | common/clock/testclock/testtimer.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698