github.com/dim4egster/coreth@v0.10.2/plugin/evm/block_builder_test.go (about)

     1  // (c) 2019-2021, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package evm
     5  
     6  import (
     7  	"math/big"
     8  	"sync"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/dim4egster/coreth/params"
    13  
    14  	"github.com/dim4egster/qmallgo/snow"
    15  )
    16  
    17  func TestBlockBuilderShutsDown(t *testing.T) {
    18  	shutdownChan := make(chan struct{})
    19  	wg := &sync.WaitGroup{}
    20  	config := *params.TestChainConfig
    21  	// Set ApricotPhase4BlockTime one hour in the future so that it will
    22  	// create a goroutine waiting for an hour before shutting down the
    23  	// buildBlocktimer.
    24  	config.ApricotPhase4BlockTimestamp = big.NewInt(time.Now().Add(time.Hour).Unix())
    25  	builder := &blockBuilder{
    26  		ctx:          snow.DefaultContextTest(),
    27  		chainConfig:  &config,
    28  		shutdownChan: shutdownChan,
    29  		shutdownWg:   wg,
    30  	}
    31  
    32  	builder.handleBlockBuilding()
    33  	// Close [shutdownChan] and ensure that the wait group finishes in a reasonable
    34  	// amount of time.
    35  	close(shutdownChan)
    36  	attemptAwait(t, wg, 5*time.Second)
    37  }
    38  
    39  func TestBlockBuilderSkipsTimerInitialization(t *testing.T) {
    40  	shutdownChan := make(chan struct{})
    41  	wg := &sync.WaitGroup{}
    42  	builder := &blockBuilder{
    43  		ctx:          snow.DefaultContextTest(),
    44  		chainConfig:  params.TestChainConfig,
    45  		shutdownChan: shutdownChan,
    46  		shutdownWg:   wg,
    47  	}
    48  
    49  	builder.handleBlockBuilding()
    50  
    51  	if builder.buildBlockTimer == nil {
    52  		t.Fatal("expected block timer to be non-nil")
    53  	}
    54  
    55  	// The wait group should finish immediately since no goroutine
    56  	// should be created when all prices should be set from the start
    57  	attemptAwait(t, wg, time.Millisecond)
    58  }