mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-24 12:27:18 +00:00
3e9bf58d81
* Fix validator acting upon first slot * Change log to debug * Fix roles at slot 0 * Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into fix-val * Add regression test * Formatting * Add slot ticker regression test
112 lines
2.4 KiB
Go
112 lines
2.4 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)
|
|
}
|
|
}
|