github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/p2p/trust/ticker.go (about)

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