blob: 2fee233d29232423bdee9ad1f150c5cc7a8fbbc2 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Todd Wang8c4e5cc2015-04-09 11:30:52 -07005// Package timekeeper defines an interface to allow switching between real time
6// and simulated time.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07007package timekeeper
8
9import "time"
10
11// TimeKeeper is meant as a drop-in replacement for using the time package
12// directly, and allows testing code to substitute a suitable implementation.
13// The meaning of duration and current time depends on the implementation (may
14// be a simulated time).
15type TimeKeeper interface {
16 // After waits for the duration to elapse and then sends the current
17 // time on the returned channel.
18 After(d time.Duration) <-chan time.Time
19 // Sleep pauses the current goroutine for at least the duration d. A
20 // negative or zero duration causes Sleep to return immediately.
21 Sleep(d time.Duration)
22}
23
24// realTime is the default implementation of TimeKeeper, using the time package.
25type realTime struct{}
26
27var rt realTime
28
29// After implements TimeKeeper.After.
30func (t *realTime) After(d time.Duration) <-chan time.Time {
31 return time.After(d)
32}
33
34// Sleep implements TimeKeeper.Sleep.
35func (t *realTime) Sleep(d time.Duration) {
36 time.Sleep(d)
37}
38
39// RealTime returns a default instance of TimeKeeper.
40func RealTime() TimeKeeper {
41 return &rt
42}