github.com/MetalBlockchain/metalgo@v1.11.9/snow/engine/common/test_bootstrap_tracker.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package common
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/MetalBlockchain/metalgo/ids"
    12  )
    13  
    14  // BootstrapTrackerTest is a test subnet
    15  type BootstrapTrackerTest struct {
    16  	T *testing.T
    17  
    18  	CantIsBootstrapped, CantBootstrapped, CantOnBootstrapCompleted bool
    19  
    20  	IsBootstrappedF func() bool
    21  	BootstrappedF   func(ids.ID)
    22  
    23  	OnBootstrapCompletedF func() chan struct{}
    24  }
    25  
    26  // Default set the default callable value to [cant]
    27  func (s *BootstrapTrackerTest) Default(cant bool) {
    28  	s.CantIsBootstrapped = cant
    29  	s.CantBootstrapped = cant
    30  	s.CantOnBootstrapCompleted = cant
    31  }
    32  
    33  // IsBootstrapped calls IsBootstrappedF if it was initialized. If it wasn't
    34  // initialized and this function shouldn't be called and testing was
    35  // initialized, then testing will fail. Defaults to returning false.
    36  func (s *BootstrapTrackerTest) IsBootstrapped() bool {
    37  	if s.IsBootstrappedF != nil {
    38  		return s.IsBootstrappedF()
    39  	}
    40  	if s.CantIsBootstrapped && s.T != nil {
    41  		require.FailNow(s.T, "Unexpectedly called IsBootstrapped")
    42  	}
    43  	return false
    44  }
    45  
    46  // Bootstrapped calls BootstrappedF if it was initialized. If it wasn't
    47  // initialized and this function shouldn't be called and testing was
    48  // initialized, then testing will fail.
    49  func (s *BootstrapTrackerTest) Bootstrapped(chainID ids.ID) {
    50  	if s.BootstrappedF != nil {
    51  		s.BootstrappedF(chainID)
    52  	} else if s.CantBootstrapped && s.T != nil {
    53  		require.FailNow(s.T, "Unexpectedly called Bootstrapped")
    54  	}
    55  }
    56  
    57  func (s *BootstrapTrackerTest) OnBootstrapCompleted() chan struct{} {
    58  	if s.OnBootstrapCompletedF != nil {
    59  		return s.OnBootstrapCompletedF()
    60  	} else if s.CantOnBootstrapCompleted && s.T != nil {
    61  		require.FailNow(s.T, "Unexpectedly called OnBootstrapCompleted")
    62  	}
    63  	return nil
    64  }