| Index: go/src/infra/libs/clock/testtimer.go
|
| diff --git a/go/src/infra/libs/clock/testtimer.go b/go/src/infra/libs/clock/testtimer.go
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..d8e98b3df8e2438ff9a5f806f54f09397dcac535
|
| --- /dev/null
|
| +++ b/go/src/infra/libs/clock/testtimer.go
|
| @@ -0,0 +1,52 @@
|
| +// Copyright (c) 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package clock
|
| +
|
| +import (
|
| + "time"
|
| +)
|
| +
|
| +// TestTimer is a Timer implementation that can be used for testing. The timer can be
|
| +// signalled via its Signal method, which writes an an underlying buffered channel.
|
| +//
|
| +// The channel is buffered so it can be used without requiring a separate signalling
|
| +// goroutine.
|
| +type TestTimer interface {
|
| + Timer
|
| + Signal(time.Time) // Signal the test timer.
|
| +}
|
| +
|
| +type testTimer struct {
|
| + signalC chan time.Time // Underlying signal channel.
|
| + active bool // If true, the timer is active.
|
| +}
|
| +
|
| +// NewTestTimer returns a new, instantiated TestTimer.
|
| +func NewTestTimer() TestTimer {
|
| + return &testTimer{
|
| + signalC: make(chan time.Time, 1),
|
| + }
|
| +}
|
| +
|
| +// Signals the timer.
|
| +func (t *testTimer) Signal(tm time.Time) {
|
| + t.signalC <- tm
|
| +}
|
| +
|
| +// Implements Timer.
|
| +func (t *testTimer) GetC() (c <-chan time.Time) {
|
| + return t.signalC
|
| +}
|
| +
|
| +// Implements Timer.
|
| +func (t *testTimer) Reset(d time.Duration) bool {
|
| + return t.active
|
| +}
|
| +
|
| +// Implements Timer.
|
| +func (t *testTimer) Stop() (active bool) {
|
| + active, t.active = t.active, false
|
| + return
|
| +}
|
|
|