prysm-pulse/shared/slotutil/slotticker_test.go
terence tsao 8c04ced1a6 Name Changes to Align with V0.3 (#1621)
* fixed epoch_processing

* penalize->slash

* exit -> voluntary_exit

*  SEED_LOOKAHEAD -> MIN_SEED_LOOKAHED

* ENTRY_EXIT_DELAY -> ACTIVATION_EXIT_DELAY

* `INCLUDER_REWARD_QUOTIENT` -> `ATTESTATION_INCLUSION_REWARD_QUOTIEN`

* LatestIndexRoots -> LatestActiveIndexRoots

* `MIN_VALIDATOR_WITHDRAWAL_EPOCHS` -> `MIN_VALIDATOR_WITHDRAWAL_DELAY`

* MAX_WITHDRAWALS_PER_EPOCH -> MAX_EXIT_DEQUEUES_PER_EPOCH

* ETH1_DATA_VOTING_PERIOD -> EPOCHS_PER_ETH1_VOTING_PERIOD

* SLOT_DURATION -> SECONDS_PER_SLOT

* EPOCH_LENGTH -> SLOTS_PER_EPOCH

* SLOT_DURATION -> SECONDS_PER_SLOT take 2

* rest of the misc fixes for config name changes

* remove tools/bootnode/.!74296!bootnode.go

* `current_epoch_start_shard` -> `current_shuffling_start_shard`, `current_shuffling_epoch`, `current_shuffling_see`

* go fmt

* fixed comment

* updated pseudocode comments

* merged master
2019-02-18 10:52:16 -06:00

122 lines
3.2 KiB
Go

package slotutil
import (
"testing"
"time"
"github.com/prysmaticlabs/prysm/shared/params"
)
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)
slotDuration := 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, slotDuration, since, until, after)
// Tick once.
tick <- time.Now()
slot := <-ticker.C()
if slot != 1+params.BeaconConfig().GenesisSlot {
t.Fatalf("Expected %d, got %d", params.BeaconConfig().GenesisSlot+1, slot)
}
// Tick twice.
tick <- time.Now()
slot = <-ticker.C()
if slot != 2+params.BeaconConfig().GenesisSlot {
t.Fatalf("Expected %d, got %d", params.BeaconConfig().GenesisSlot+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)
slotDuration := 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, slotDuration, since, until, after)
// Tick once.
tick <- time.Now()
slot := <-ticker.C()
if slot != params.BeaconConfig().GenesisSlot {
t.Fatalf("Expected %d, got %d", params.BeaconConfig().GenesisSlot, slot)
}
// Tick twice.
tick <- time.Now()
slot = <-ticker.C()
if slot != 1+params.BeaconConfig().GenesisSlot {
t.Fatalf("Expected %d, got %d", params.BeaconConfig().GenesisSlot+1, slot)
}
}
func TestCurrentSlot(t *testing.T) {
// Test genesis slot
genesisTime := time.Now()
slotDurationSeconds := time.Second * time.Duration(params.BeaconConfig().SecondsPerSlot)
slot := CurrentSlot(genesisTime, params.BeaconConfig().SecondsPerSlot, time.Since)
if slot != params.BeaconConfig().GenesisSlot {
t.Errorf("Expected %d, got: %d", params.BeaconConfig().GenesisSlot, slot)
}
// Test slot 3 after genesis.
genesisTime = genesisTime.Add(slotDurationSeconds * 3)
slot = CurrentSlot(genesisTime, params.BeaconConfig().SecondsPerSlot, time.Since)
if slot != 3*params.BeaconConfig().GenesisSlot {
t.Errorf("Expected %d, got: %d", params.BeaconConfig().GenesisSlot*3, slot)
}
}