github.com/prysmaticlabs/prysm@v1.4.4/shared/slotutil/slotticker_test.go (about)

     1  package slotutil
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	types "github.com/prysmaticlabs/eth2-types"
     8  )
     9  
    10  var _ Ticker = (*SlotTicker)(nil)
    11  
    12  func TestSlotTicker(t *testing.T) {
    13  	ticker := &SlotTicker{
    14  		c:    make(chan types.Slot),
    15  		done: make(chan struct{}),
    16  	}
    17  	defer ticker.Done()
    18  
    19  	var sinceDuration time.Duration
    20  	since := func(time.Time) time.Duration {
    21  		return sinceDuration
    22  	}
    23  
    24  	var untilDuration time.Duration
    25  	until := func(time.Time) time.Duration {
    26  		return untilDuration
    27  	}
    28  
    29  	var tick chan time.Time
    30  	after := func(time.Duration) <-chan time.Time {
    31  		return tick
    32  	}
    33  
    34  	genesisTime := time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)
    35  	secondsPerSlot := uint64(8)
    36  
    37  	// Test when the ticker starts immediately after genesis time.
    38  	sinceDuration = 1 * time.Second
    39  	untilDuration = 7 * time.Second
    40  	// Make this a buffered channel to prevent a deadlock since
    41  	// the other goroutine calls a function in this goroutine.
    42  	tick = make(chan time.Time, 2)
    43  	ticker.start(genesisTime, secondsPerSlot, since, until, after)
    44  
    45  	// Tick once.
    46  	tick <- time.Now()
    47  	slot := <-ticker.C()
    48  	if slot != 0 {
    49  		t.Fatalf("Expected %d, got %d", 0, slot)
    50  	}
    51  
    52  	// Tick twice.
    53  	tick <- time.Now()
    54  	slot = <-ticker.C()
    55  	if slot != 1 {
    56  		t.Fatalf("Expected %d, got %d", 1, slot)
    57  	}
    58  
    59  	// Tick thrice.
    60  	tick <- time.Now()
    61  	slot = <-ticker.C()
    62  	if slot != 2 {
    63  		t.Fatalf("Expected %d, got %d", 2, slot)
    64  	}
    65  }
    66  
    67  func TestSlotTickerGenesis(t *testing.T) {
    68  	ticker := &SlotTicker{
    69  		c:    make(chan types.Slot),
    70  		done: make(chan struct{}),
    71  	}
    72  	defer ticker.Done()
    73  
    74  	var sinceDuration time.Duration
    75  	since := func(time.Time) time.Duration {
    76  		return sinceDuration
    77  	}
    78  
    79  	var untilDuration time.Duration
    80  	until := func(time.Time) time.Duration {
    81  		return untilDuration
    82  	}
    83  
    84  	var tick chan time.Time
    85  	after := func(time.Duration) <-chan time.Time {
    86  		return tick
    87  	}
    88  
    89  	genesisTime := time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)
    90  	secondsPerSlot := uint64(8)
    91  
    92  	// Test when the ticker starts before genesis time.
    93  	sinceDuration = -1 * time.Second
    94  	untilDuration = 1 * time.Second
    95  	// Make this a buffered channel to prevent a deadlock since
    96  	// the other goroutine calls a function in this goroutine.
    97  	tick = make(chan time.Time, 2)
    98  	ticker.start(genesisTime, secondsPerSlot, since, until, after)
    99  
   100  	// Tick once.
   101  	tick <- time.Now()
   102  	slot := <-ticker.C()
   103  	if slot != 0 {
   104  		t.Fatalf("Expected %d, got %d", 0, slot)
   105  	}
   106  
   107  	// Tick twice.
   108  	tick <- time.Now()
   109  	slot = <-ticker.C()
   110  	if slot != 1 {
   111  		t.Fatalf("Expected %d, got %d", 1, slot)
   112  	}
   113  }
   114  
   115  func TestGetSlotTickerWithOffset_OK(t *testing.T) {
   116  	genesisTime := time.Now()
   117  	secondsPerSlot := uint64(4)
   118  	offset := time.Duration(secondsPerSlot/2) * time.Second
   119  
   120  	offsetTicker := NewSlotTickerWithOffset(genesisTime, offset, secondsPerSlot)
   121  	normalTicker := NewSlotTicker(genesisTime, secondsPerSlot)
   122  
   123  	firstTicked := 0
   124  	for {
   125  		select {
   126  		case <-offsetTicker.C():
   127  			if firstTicked != 1 {
   128  				t.Fatal("Expected other ticker to tick first")
   129  			}
   130  			return
   131  		case <-normalTicker.C():
   132  			if firstTicked != 0 {
   133  				t.Fatal("Expected normal ticker to tick first")
   134  			}
   135  			firstTicked = 1
   136  		}
   137  	}
   138  }