github.com/vipernet-xyz/tm@v0.34.24/p2p/trust/ticker.go (about)

     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  
     4  package trust
     5  
     6  import (
     7  	"time"
     8  )
     9  
    10  // MetricTicker provides a single ticker interface for the trust metric
    11  type MetricTicker interface {
    12  	// GetChannel returns the receive only channel that fires at each time interval
    13  	GetChannel() <-chan time.Time
    14  
    15  	// Stop will halt further activity on the ticker channel
    16  	Stop()
    17  }
    18  
    19  // The ticker used during testing that provides manual control over time intervals
    20  type TestTicker struct {
    21  	C       chan time.Time
    22  	stopped bool
    23  }
    24  
    25  // NewTestTicker returns our ticker used within test routines
    26  func NewTestTicker() *TestTicker {
    27  	c := make(chan time.Time)
    28  	return &TestTicker{
    29  		C: c,
    30  	}
    31  }
    32  
    33  func (t *TestTicker) GetChannel() <-chan time.Time {
    34  	return t.C
    35  }
    36  
    37  func (t *TestTicker) Stop() {
    38  	t.stopped = true
    39  }
    40  
    41  // NextInterval manually sends Time on the ticker channel
    42  func (t *TestTicker) NextTick() {
    43  	if t.stopped {
    44  		return
    45  	}
    46  	t.C <- time.Now()
    47  }
    48  
    49  // Ticker is just a wrap around time.Ticker that allows it
    50  // to meet the requirements of our interface
    51  type Ticker struct {
    52  	*time.Ticker
    53  }
    54  
    55  // NewTicker returns a normal time.Ticker wrapped to meet our interface
    56  func NewTicker(d time.Duration) *Ticker {
    57  	return &Ticker{time.NewTicker(d)}
    58  }
    59  
    60  func (t *Ticker) GetChannel() <-chan time.Time {
    61  	return t.C
    62  }