github.com/koko1123/flow-go-1@v0.29.6/engine/access/relay/engine_test.go (about)

     1  package relay
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/rs/zerolog"
     7  	"github.com/stretchr/testify/mock"
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/koko1123/flow-go-1/model/flow"
    11  	"github.com/koko1123/flow-go-1/network/channels"
    12  	"github.com/koko1123/flow-go-1/network/mocknetwork"
    13  	"github.com/koko1123/flow-go-1/utils/unittest"
    14  )
    15  
    16  type Suite struct {
    17  	suite.Suite
    18  
    19  	engine   *Engine
    20  	channels channels.ChannelList
    21  	conduits map[channels.Channel]*mocknetwork.Conduit
    22  }
    23  
    24  func (suite *Suite) SetupTest() {
    25  	suite.channels = channels.ChannelList{
    26  		channels.Channel("test-channel-1"),
    27  	}
    28  	net := new(mocknetwork.Network)
    29  	unstakedNet := new(mocknetwork.Network)
    30  	suite.conduits = make(map[channels.Channel]*mocknetwork.Conduit)
    31  
    32  	for _, channel := range suite.channels {
    33  		con := new(mocknetwork.Conduit)
    34  		suite.conduits[channel] = con
    35  		net.On("Register", channel, mock.Anything).Return(new(mocknetwork.Conduit), nil).Once()
    36  		unstakedNet.On("Register", channel, mock.Anything).Return(con, nil).Once()
    37  	}
    38  
    39  	eng, err := New(
    40  		zerolog.Logger{},
    41  		suite.channels,
    42  		net,
    43  		unstakedNet,
    44  	)
    45  	suite.Require().Nil(err)
    46  
    47  	suite.engine = eng
    48  }
    49  
    50  func TestRelayEngine(t *testing.T) {
    51  	suite.Run(t, new(Suite))
    52  }
    53  
    54  func getEvent() interface{} {
    55  	return struct {
    56  		foo string
    57  	}{
    58  		foo: "bar",
    59  	}
    60  }
    61  
    62  // TestHappyPath tests that the relay engine relays events for each
    63  // channel that it was created with
    64  func (suite *Suite) TestHappyPath() {
    65  	for channel, conduit := range suite.conduits {
    66  		id := unittest.IdentifierFixture()
    67  		event := getEvent()
    68  
    69  		conduit.On("Publish", event, flow.ZeroID).Return(nil).Once()
    70  
    71  		err := suite.engine.Process(channel, id, event)
    72  		suite.Assert().Nil(err)
    73  
    74  		conduit.AssertNumberOfCalls(suite.T(), "Publish", 1)
    75  		conduit.AssertExpectations(suite.T())
    76  	}
    77  }