You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB

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