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

     1  package testutils
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/koko1123/flow-go-1/model/flow"
    11  	mockcomponent "github.com/koko1123/flow-go-1/module/component/mock"
    12  	"github.com/koko1123/flow-go-1/network"
    13  	"github.com/koko1123/flow-go-1/network/channels"
    14  )
    15  
    16  // MeshEngine is a simple engine that is used for testing the correctness of
    17  // driving the engines with libp2p, it simply receives and stores the incoming messages
    18  type MeshEngine struct {
    19  	sync.Mutex
    20  	t        *testing.T
    21  	Con      network.Conduit       // used to directly communicate with the network
    22  	originID flow.Identifier       // used to keep track of the id of the sender of the messages
    23  	Event    chan interface{}      // used to keep track of the events that the node receives
    24  	Channel  chan channels.Channel // used to keep track of the channels that events are Received on
    25  	Received chan struct{}         // used as an indicator on reception of messages for testing
    26  	mockcomponent.Component
    27  }
    28  
    29  func NewMeshEngine(t *testing.T, net network.Network, cap int, channel channels.Channel) *MeshEngine {
    30  	te := &MeshEngine{
    31  		t:        t,
    32  		Event:    make(chan interface{}, cap),
    33  		Channel:  make(chan channels.Channel, cap),
    34  		Received: make(chan struct{}, cap),
    35  	}
    36  
    37  	c2, err := net.Register(channel, te)
    38  	require.NoError(te.t, err)
    39  	te.Con = c2
    40  
    41  	return te
    42  }
    43  
    44  // SubmitLocal is implemented for a valid type assertion to Engine
    45  // any call to it fails the test
    46  func (e *MeshEngine) SubmitLocal(event interface{}) {
    47  	require.Fail(e.t, "not implemented")
    48  }
    49  
    50  // Submit is implemented for a valid type assertion to Engine
    51  // any call to it fails the test
    52  func (e *MeshEngine) Submit(channel channels.Channel, originID flow.Identifier, event interface{}) {
    53  	go func() {
    54  		err := e.Process(channel, originID, event)
    55  		if err != nil {
    56  			require.Fail(e.t, "could not process submitted Event")
    57  		}
    58  	}()
    59  }
    60  
    61  // ProcessLocal is implemented for a valid type assertion to Engine
    62  // any call to it fails the test
    63  func (e *MeshEngine) ProcessLocal(event interface{}) error {
    64  	require.Fail(e.t, "not implemented")
    65  	return fmt.Errorf(" unexpected method called")
    66  }
    67  
    68  // Process receives an originID and an Event and casts them into the corresponding fields of the
    69  // MeshEngine. It then flags the Received Channel on reception of an Event.
    70  func (e *MeshEngine) Process(channel channels.Channel, originID flow.Identifier, event interface{}) error {
    71  	e.Lock()
    72  	defer e.Unlock()
    73  
    74  	// stores the message locally
    75  	e.originID = originID
    76  	e.Channel <- channel
    77  	e.Event <- event
    78  	e.Received <- struct{}{}
    79  	return nil
    80  }