OLD | NEW |
---|---|
(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 } | |
OLD | NEW |