forked from tendermint/tendermint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticker.go
62 lines (50 loc) · 1.36 KB
/
ticker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2017 Tendermint. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package trust
import (
"time"
)
// MetricTicker provides a single ticker interface for the trust metric
type MetricTicker interface {
// GetChannel returns the receive only channel that fires at each time interval
GetChannel() <-chan time.Time
// Stop will halt further activity on the ticker channel
Stop()
}
// The ticker used during testing that provides manual control over time intervals
type TestTicker struct {
C chan time.Time
stopped bool
}
// NewTestTicker returns our ticker used within test routines
func NewTestTicker() *TestTicker {
c := make(chan time.Time)
return &TestTicker{
C: c,
}
}
func (t *TestTicker) GetChannel() <-chan time.Time {
return t.C
}
func (t *TestTicker) Stop() {
t.stopped = true
}
// NextInterval manually sends Time on the ticker channel
func (t *TestTicker) NextTick() {
if t.stopped {
return
}
t.C <- time.Now()
}
// Ticker is just a wrap around time.Ticker that allows it
// to meet the requirements of our interface
type Ticker struct {
*time.Ticker
}
// NewTicker returns a normal time.Ticker wrapped to meet our interface
func NewTicker(d time.Duration) *Ticker {
return &Ticker{time.NewTicker(d)}
}
func (t *Ticker) GetChannel() <-chan time.Time {
return t.C
}