github.com/koko1123/flow-go-1@v0.29.6/network/internal/testutils/wrapper.go (about)

     1  package testutils
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/koko1123/flow-go-1/model/flow"
     7  	"github.com/koko1123/flow-go-1/network"
     8  )
     9  
    10  // ConduitSendWrapperFunc is a wrapper around the set of methods offered by the
    11  // Conduit (e.g., Publish). This data type is solely introduced at the test level.
    12  // Its primary purpose is to make the same test reusable on different Conduit methods.
    13  type ConduitSendWrapperFunc func(msg interface{}, conduit network.Conduit, targetIDs ...flow.Identifier) error
    14  
    15  type ConduitWrapper struct{}
    16  
    17  // Publish defines a function that receives a message, conduit of an engine instance, and
    18  // a set target IDs. It then sends the message to the target IDs using the Publish method of conduit.
    19  func (c *ConduitWrapper) Publish(msg interface{}, conduit network.Conduit, targetIDs ...flow.Identifier) error {
    20  	return conduit.Publish(msg, targetIDs...)
    21  }
    22  
    23  // Unicast defines a function that receives a message, conduit of an engine instance, and
    24  // a set of target IDs. It then sends the message to the target IDs using individual Unicasts to each
    25  // target in the underlying network.
    26  func (c *ConduitWrapper) Unicast(msg interface{}, conduit network.Conduit, targetIDs ...flow.Identifier) error {
    27  	for _, id := range targetIDs {
    28  		if err := conduit.Unicast(msg, id); err != nil {
    29  			return fmt.Errorf("could not unicast to node ID %x: %w", id, err)
    30  		}
    31  	}
    32  	return nil
    33  }
    34  
    35  // Multicast defines a function that receives a message, conduit of an engine instance, and
    36  // a set of target ID. It then sends the message to the target IDs using the Multicast method of conduit.
    37  func (c *ConduitWrapper) Multicast(msg interface{}, conduit network.Conduit, targetIDs ...flow.Identifier) error {
    38  	return conduit.Multicast(msg, uint(len(targetIDs)), targetIDs...)
    39  }