github.com/decred/dcrlnd@v0.7.6/ticker/ticker_test.go (about)

     1  package ticker_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/decred/dcrlnd/ticker"
     8  )
     9  
    10  const interval = 50 * time.Millisecond
    11  const numActiveTicks = 3
    12  
    13  var tickers = []struct {
    14  	name   string
    15  	ticker ticker.Ticker
    16  }{
    17  	{
    18  		"default ticker",
    19  		ticker.New(interval),
    20  	},
    21  	{
    22  		"mock ticker",
    23  		ticker.NewForce(interval),
    24  	},
    25  }
    26  
    27  // TestTickers verifies that both our production and mock tickers exhibit the
    28  // same principle behaviors when accessed via the ticker.Ticker interface
    29  // methods.
    30  func TestInterfaceTickers(t *testing.T) {
    31  	for _, test := range tickers {
    32  		t.Run(test.name, func(t *testing.T) {
    33  			testTicker(t, test.ticker)
    34  		})
    35  	}
    36  }
    37  
    38  // testTicker asserts the behavior of a freshly initialized ticker.Ticker.
    39  func testTicker(t *testing.T, ticker ticker.Ticker) {
    40  	// Newly initialized ticker should start off inactive.
    41  	select {
    42  	case <-ticker.Ticks():
    43  		t.Fatalf("ticker should not have ticked before calling Resume")
    44  	case <-time.After(2 * interval):
    45  	}
    46  
    47  	// Resume, ticker should be active and start sending ticks.
    48  	ticker.Resume()
    49  
    50  	for i := 0; i < numActiveTicks; i++ {
    51  		select {
    52  		case <-ticker.Ticks():
    53  		case <-time.After(2 * interval):
    54  			t.Fatalf(
    55  				"ticker should have ticked after calling Resume",
    56  			)
    57  		}
    58  	}
    59  
    60  	// Pause, check that ticker is inactive and sends no ticks.
    61  	ticker.Pause()
    62  
    63  	select {
    64  	case <-ticker.Ticks():
    65  		t.Fatalf("ticker should not have ticked after calling Pause")
    66  	case <-time.After(2 * interval):
    67  	}
    68  
    69  	// Pause again, expect same behavior as after first invocation.
    70  	ticker.Pause()
    71  
    72  	select {
    73  	case <-ticker.Ticks():
    74  		t.Fatalf("ticker should not have ticked after calling Pause again")
    75  	case <-time.After(2 * interval):
    76  	}
    77  
    78  	// Resume again, should result in normal active behavior.
    79  	ticker.Resume()
    80  
    81  	for i := 0; i < numActiveTicks; i++ {
    82  		select {
    83  		case <-ticker.Ticks():
    84  		case <-time.After(2 * interval):
    85  			t.Fatalf(
    86  				"ticker should have ticked after calling Resume",
    87  			)
    88  		}
    89  	}
    90  
    91  	// Stop the ticker altogether, should render it inactive.
    92  	ticker.Stop()
    93  
    94  	select {
    95  	case <-ticker.Ticks():
    96  		t.Fatalf("ticker should not have ticked after calling Stop")
    97  	case <-time.After(2 * interval):
    98  	}
    99  }