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

     1  package slotutil
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/prysmaticlabs/prysm/shared/params"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    10  	"github.com/prysmaticlabs/prysm/shared/timeutils"
    11  	logTest "github.com/sirupsen/logrus/hooks/test"
    12  )
    13  
    14  func TestCountdownToGenesis(t *testing.T) {
    15  	hook := logTest.NewGlobal()
    16  	params.SetupTestConfigCleanup(t)
    17  	config := params.BeaconConfig()
    18  	config.GenesisCountdownInterval = time.Millisecond * 500
    19  	params.OverrideBeaconConfig(config)
    20  
    21  	t.Run("normal countdown", func(t *testing.T) {
    22  		defer hook.Reset()
    23  		firstStringResult := "1s until chain genesis"
    24  		genesisReached := "Chain genesis time reached"
    25  		CountdownToGenesis(
    26  			context.Background(),
    27  			timeutils.Now().Add(2*time.Second),
    28  			params.BeaconConfig().MinGenesisActiveValidatorCount,
    29  			[32]byte{},
    30  		)
    31  		require.LogsContain(t, hook, firstStringResult)
    32  		require.LogsContain(t, hook, genesisReached)
    33  	})
    34  
    35  	t.Run("close context", func(t *testing.T) {
    36  		defer hook.Reset()
    37  		ctx, cancel := context.WithCancel(context.Background())
    38  		go func() {
    39  			time.AfterFunc(1500*time.Millisecond, func() {
    40  				cancel()
    41  			})
    42  		}()
    43  		CountdownToGenesis(
    44  			ctx,
    45  			timeutils.Now().Add(5*time.Second),
    46  			params.BeaconConfig().MinGenesisActiveValidatorCount,
    47  			[32]byte{},
    48  		)
    49  		require.LogsContain(t, hook, "4s until chain genesis")
    50  		require.LogsContain(t, hook, "Context closed, exiting routine")
    51  		require.LogsDoNotContain(t, hook, "Chain genesis time reached")
    52  	})
    53  }