mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-04 00:44:27 +00:00
1f21e196b6
* Merge variable declaration and assignment * Use result of type assertion to simplify cases * Replace call to bytes.Compare with bytes.Equal * Drop unnecessary use of the blank identifier * Replace x.Sub(time.Now()) with time.Until(x) * Function literal can be simplified * Use a single append to concatenate two slices * Replace time.Now().Sub(x) with time.Since(x) * Omit comparison with boolean constant * Omit redundant nil check on slices * Nested if can be replaced with else-if * Function call can be replaced with helper function * Omit redundant control flow * Use plain channel send or receive * Simplify returning boolean expression * Merge branch 'origin-master' into fix-antipatterns * Merge branch 'master' into fix-antipatterns
140 lines
3.0 KiB
Go
140 lines
3.0 KiB
Go
package slotutil
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var _ = Ticker(&SlotTicker{})
|
|
|
|
func TestSlotTicker(t *testing.T) {
|
|
ticker := &SlotTicker{
|
|
c: make(chan uint64),
|
|
done: make(chan struct{}),
|
|
}
|
|
defer ticker.Done()
|
|
|
|
var sinceDuration time.Duration
|
|
since := func(time.Time) time.Duration {
|
|
return sinceDuration
|
|
}
|
|
|
|
var untilDuration time.Duration
|
|
until := func(time.Time) time.Duration {
|
|
return untilDuration
|
|
}
|
|
|
|
var tick chan time.Time
|
|
after := func(time.Duration) <-chan time.Time {
|
|
return tick
|
|
}
|
|
|
|
genesisTime := time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
secondsPerSlot := uint64(8)
|
|
|
|
// Test when the ticker starts immediately after genesis time.
|
|
sinceDuration = 1 * time.Second
|
|
untilDuration = 7 * time.Second
|
|
// Make this a buffered channel to prevent a deadlock since
|
|
// the other goroutine calls a function in this goroutine.
|
|
tick = make(chan time.Time, 2)
|
|
ticker.start(genesisTime, secondsPerSlot, since, until, after)
|
|
|
|
// Tick once.
|
|
tick <- time.Now()
|
|
slot := <-ticker.C()
|
|
if slot != 0 {
|
|
t.Fatalf("Expected %d, got %d", 0, slot)
|
|
}
|
|
|
|
// Tick twice.
|
|
tick <- time.Now()
|
|
slot = <-ticker.C()
|
|
if slot != 1 {
|
|
t.Fatalf("Expected %d, got %d", 1, slot)
|
|
}
|
|
|
|
// Tick thrice.
|
|
tick <- time.Now()
|
|
slot = <-ticker.C()
|
|
if slot != 2 {
|
|
t.Fatalf("Expected %d, got %d", 2, slot)
|
|
}
|
|
}
|
|
|
|
func TestSlotTickerGenesis(t *testing.T) {
|
|
ticker := &SlotTicker{
|
|
c: make(chan uint64),
|
|
done: make(chan struct{}),
|
|
}
|
|
defer ticker.Done()
|
|
|
|
var sinceDuration time.Duration
|
|
since := func(time.Time) time.Duration {
|
|
return sinceDuration
|
|
}
|
|
|
|
var untilDuration time.Duration
|
|
until := func(time.Time) time.Duration {
|
|
return untilDuration
|
|
}
|
|
|
|
var tick chan time.Time
|
|
after := func(time.Duration) <-chan time.Time {
|
|
return tick
|
|
}
|
|
|
|
genesisTime := time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
secondsPerSlot := uint64(8)
|
|
|
|
// Test when the ticker starts before genesis time.
|
|
sinceDuration = -1 * time.Second
|
|
untilDuration = 1 * time.Second
|
|
// Make this a buffered channel to prevent a deadlock since
|
|
// the other goroutine calls a function in this goroutine.
|
|
tick = make(chan time.Time, 2)
|
|
ticker.start(genesisTime, secondsPerSlot, since, until, after)
|
|
|
|
// Tick once.
|
|
tick <- time.Now()
|
|
slot := <-ticker.C()
|
|
if slot != 0 {
|
|
t.Fatalf("Expected %d, got %d", 0, slot)
|
|
}
|
|
|
|
// Tick twice.
|
|
tick <- time.Now()
|
|
slot = <-ticker.C()
|
|
if slot != 1 {
|
|
t.Fatalf("Expected %d, got %d", 1, slot)
|
|
}
|
|
}
|
|
|
|
func TestGetSlotTickerWithOffset_OK(t *testing.T) {
|
|
genesisTime := time.Now()
|
|
secondsPerSlot := uint64(4)
|
|
offset := time.Duration(secondsPerSlot/2) * time.Second
|
|
|
|
offsetTicker := GetSlotTickerWithOffset(genesisTime, offset, secondsPerSlot)
|
|
normalTicker := GetSlotTicker(genesisTime, secondsPerSlot)
|
|
|
|
firstTicked := 0
|
|
for {
|
|
select {
|
|
case <-offsetTicker.C():
|
|
if firstTicked != 1 {
|
|
t.Fatal("Expected other ticker to tick first")
|
|
}
|
|
firstTicked = 2
|
|
return
|
|
case <-normalTicker.C():
|
|
if firstTicked != 0 {
|
|
t.Fatal("Expected normal ticker to tick first")
|
|
}
|
|
firstTicked = 1
|
|
break
|
|
}
|
|
}
|
|
|
|
}
|