github.com/koko1123/flow-go-1@v0.29.6/network/proxy/network_test.go (about) 1 package proxy_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/mock" 7 "github.com/stretchr/testify/suite" 8 9 "github.com/koko1123/flow-go-1/model/flow" 10 "github.com/koko1123/flow-go-1/network" 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/network/proxy" 14 "github.com/koko1123/flow-go-1/utils/unittest" 15 ) 16 17 func getEvent() interface{} { 18 return struct { 19 foo string 20 }{ 21 foo: "bar", 22 } 23 } 24 25 type Suite struct { 26 suite.Suite 27 net network.Network 28 targetNodeID flow.Identifier 29 proxyNet *proxy.ProxyNetwork 30 con *mocknetwork.Conduit 31 engine network.Engine 32 } 33 34 func TestProxyNetwork(t *testing.T) { 35 suite.Run(t, new(Suite)) 36 } 37 38 func (suite *Suite) SetupTest() { 39 net := new(mocknetwork.Network) 40 suite.net = net 41 suite.con = new(mocknetwork.Conduit) 42 suite.targetNodeID = unittest.IdentifierFixture() 43 suite.proxyNet = proxy.NewProxyNetwork(suite.net, suite.targetNodeID) 44 suite.engine = new(mocknetwork.Engine) 45 46 net.On("Register", mock.AnythingOfType("channels.Channel"), mock.Anything).Return(suite.con, nil) 47 } 48 49 // TestUnicast tests that the Unicast method is translated to a unicast to the target node 50 // on the underlying network instance. 51 func (suite *Suite) TestUnicast() { 52 channel := channels.TestNetworkChannel 53 targetID := unittest.IdentifierFixture() 54 event := getEvent() 55 56 con, err := suite.proxyNet.Register(channel, suite.engine) 57 suite.Assert().NoError(err) 58 59 suite.con.On("Unicast", event, suite.targetNodeID).Return(nil).Once() 60 61 err = con.Unicast(event, targetID) 62 suite.Assert().NoError(err) 63 64 suite.con.AssertNumberOfCalls(suite.T(), "Unicast", 1) 65 suite.con.AssertExpectations(suite.T()) 66 } 67 68 // TestPublish tests that the Publish method is translated to a publish to the target node 69 // on the underlying network instance. 70 func (suite *Suite) TestPublish() { 71 channel := channels.TestNetworkChannel 72 targetIDs := make([]flow.Identifier, 10) 73 74 for i := 0; i < 10; i++ { 75 targetIDs = append(targetIDs, unittest.IdentifierFixture()) 76 } 77 78 event := getEvent() 79 80 con, err := suite.proxyNet.Register(channel, suite.engine) 81 suite.Assert().NoError(err) 82 83 suite.con.On("Publish", event, suite.targetNodeID).Return(nil).Once() 84 85 err = con.Publish(event, targetIDs...) 86 suite.Assert().NoError(err) 87 88 suite.con.AssertNumberOfCalls(suite.T(), "Publish", 1) 89 suite.con.AssertExpectations(suite.T()) 90 } 91 92 // TestUnicast tests that the Multicast method is translated to a multicast to the target node 93 // on the underlying network instance. 94 func (suite *Suite) TestMulticast() { 95 channel := channels.TestNetworkChannel 96 targetIDs := make([]flow.Identifier, 10) 97 98 for i := 0; i < 10; i++ { 99 targetIDs = append(targetIDs, unittest.IdentifierFixture()) 100 } 101 102 event := getEvent() 103 104 con, err := suite.proxyNet.Register(channel, suite.engine) 105 suite.Assert().NoError(err) 106 107 suite.con.On("Multicast", event, uint(1), suite.targetNodeID).Return(nil).Once() 108 109 err = con.Multicast(event, 5, targetIDs...) 110 suite.Assert().NoError(err) 111 112 suite.con.AssertNumberOfCalls(suite.T(), "Multicast", 1) 113 suite.con.AssertExpectations(suite.T()) 114 } 115 116 // TestClose tests that closing the proxy conduit closes the wrapped conduit. 117 func (suite *Suite) TestClose() { 118 channel := channels.TestNetworkChannel 119 120 con, err := suite.proxyNet.Register(channel, suite.engine) 121 suite.Assert().NoError(err) 122 123 suite.con.On("Close").Return(nil).Once() 124 125 err = con.Close() 126 suite.Assert().NoError(err) 127 128 suite.con.AssertNumberOfCalls(suite.T(), "Close", 1) 129 suite.con.AssertExpectations(suite.T()) 130 }