OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 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 // TestTimer is a Timer implementation that can be used for testing. The timer c
an be |
| 12 // signalled via its Signal method, which writes an an underlying buffered chann
el. |
| 13 // |
| 14 // The channel is buffered so it can be used without requiring a separate signal
ling |
| 15 // goroutine. |
| 16 type TestTimer interface { |
| 17 Timer |
| 18 Signal(time.Time) // Signal the test timer. |
| 19 } |
| 20 |
| 21 type testTimer struct { |
| 22 signalC chan time.Time // Underlying signal channel. |
| 23 active bool // If true, the timer is active. |
| 24 } |
| 25 |
| 26 // NewTestTimer returns a new, instantiated TestTimer. |
| 27 func NewTestTimer() TestTimer { |
| 28 return &testTimer{ |
| 29 signalC: make(chan time.Time, 1), |
| 30 } |
| 31 } |
| 32 |
| 33 // Signals the timer. |
| 34 func (t *testTimer) Signal(tm time.Time) { |
| 35 t.signalC <- tm |
| 36 } |
| 37 |
| 38 // Implements Timer. |
| 39 func (t *testTimer) GetC() (c <-chan time.Time) { |
| 40 return t.signalC |
| 41 } |
| 42 |
| 43 // Implements Timer. |
| 44 func (t *testTimer) Reset(d time.Duration) bool { |
| 45 return t.active |
| 46 } |
| 47 |
| 48 // Implements Timer. |
| 49 func (t *testTimer) Stop() (active bool) { |
| 50 active, t.active = t.active, false |
| 51 return |
| 52 } |
OLD | NEW |