github.com/MetalBlockchain/metalgo@v1.11.9/snow/networking/sender/test_external_sender.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package sender
     5  
     6  import (
     7  	"errors"
     8  	"testing"
     9  
    10  	"github.com/MetalBlockchain/metalgo/ids"
    11  	"github.com/MetalBlockchain/metalgo/message"
    12  	"github.com/MetalBlockchain/metalgo/snow/engine/common"
    13  	"github.com/MetalBlockchain/metalgo/subnets"
    14  	"github.com/MetalBlockchain/metalgo/utils/set"
    15  )
    16  
    17  var (
    18  	_ ExternalSender = (*ExternalSenderTest)(nil)
    19  
    20  	errSend = errors.New("unexpectedly called Send")
    21  )
    22  
    23  // ExternalSenderTest is a test sender
    24  type ExternalSenderTest struct {
    25  	TB testing.TB
    26  
    27  	CantSend bool
    28  
    29  	SendF func(msg message.OutboundMessage, config common.SendConfig, subnetID ids.ID, allower subnets.Allower) set.Set[ids.NodeID]
    30  }
    31  
    32  // Default set the default callable value to [cant]
    33  func (s *ExternalSenderTest) Default(cant bool) {
    34  	s.CantSend = cant
    35  }
    36  
    37  func (s *ExternalSenderTest) Send(
    38  	msg message.OutboundMessage,
    39  	config common.SendConfig,
    40  	subnetID ids.ID,
    41  	allower subnets.Allower,
    42  ) set.Set[ids.NodeID] {
    43  	if s.SendF != nil {
    44  		return s.SendF(msg, config, subnetID, allower)
    45  	}
    46  	if s.CantSend {
    47  		if s.TB != nil {
    48  			s.TB.Helper()
    49  			s.TB.Fatal(errSend)
    50  		}
    51  	}
    52  	return nil
    53  }