~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/utils/clock/clock.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package clock
 
5
 
 
6
import "time"
 
7
 
 
8
// Clock provides an interface for dealing with clocks.
 
9
type Clock interface {
 
10
 
 
11
        // Now returns the current clock time.
 
12
        Now() time.Time
 
13
 
 
14
        // After waits for the duration to elapse and then sends the
 
15
        // current time on the returned channel.
 
16
        After(time.Duration) <-chan time.Time
 
17
 
 
18
        // AfterFunc waits for the duration to elapse and then calls f in its own goroutine.
 
19
        // It returns a Timer that can be used to cancel the call using its Stop method.
 
20
        AfterFunc(time.Duration, func()) Timer
 
21
}
 
22
 
 
23
// Alarm returns a channel that will have the time sent on it at some point
 
24
// after the supplied time occurs.
 
25
//
 
26
// This is short for c.After(t.Sub(c.Now())).
 
27
func Alarm(c Clock, t time.Time) <-chan time.Time {
 
28
        return c.After(t.Sub(c.Now()))
 
29
}
 
30
 
 
31
// The Timer type represents a single event.
 
32
// A Timer must be created with AfterFunc.
 
33
// This interface follows time.Timer's methods but provides easier mocking.
 
34
type Timer interface {
 
35
 
 
36
        // Reset changes the timer to expire after duration d.
 
37
        // It returns true if the timer had been active, false if
 
38
        // the timer had expired or been stopped.
 
39
        Reset(time.Duration) bool
 
40
 
 
41
        // Stop prevents the Timer from firing. It returns true if
 
42
        // the call stops the timer, false if the timer has already expired or been stopped.
 
43
        // Stop does not close the channel, to prevent a read
 
44
        // from the channel succeeding incorrectly.
 
45
        Stop() bool
 
46
}