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

Side by Side Diff: go/src/infra/libs/clock/testclock.go

Issue 1154213012: Add context-aware "time" library wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 5 years, 6 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
OLDNEW
(Empty)
1 // Copyright (c) 2014 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 clock
6
7 import (
8 "time"
9 )
10
11 // TestClock is a test-oriented implementation of the 'Clock' interface.
12 //
13 // This implementation's Clock responses are configurable by modifying its membe r
14 // variables. Time-based events are explicitly triggered by sending on a Timer
15 // instance's channel.
16 type TestClock struct {
17 T time.Time // The current clock time.
18 C chan time.Time // Channel to unblock sleeping and After.
19 Timer func() Timer // Timer generator function. Must be set to use New Timer() and After().
20 }
21
iannucci 2015/06/02 06:15:34 Add a line here var _ Clock = (*TestClock)(nil)
dnj 2015/06/02 17:03:12 Done.
22 // Now implements Clock.
23 func (c *TestClock) Now() time.Time {
24 return c.T
25 }
26
27 // Sleep implements Clock.
28 func (c *TestClock) Sleep(time.Duration) {
29 if c.C != nil {
30 // Wait for sleep signal; update current time.
31 c.T = <-c.C
32 }
33 }
34
35 // NewTimer implements Clock.
36 func (c *TestClock) NewTimer() Timer {
37 if c.Timer == nil {
38 panic("No test timer generator is configured.")
39 }
40 return c.Timer()
41 }
42
43 // After implements Clock.
44 func (c *TestClock) After(d time.Duration) <-chan time.Time {
45 t := c.NewTimer()
46 t.Reset(d)
47 return t.GetC()
48 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698