github.com/iotexproject/iotex-core@v1.14.1-rc1/dispatcher/dispatcher_test.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package dispatcher
     7  
     8  import (
     9  	"context"
    10  	"testing"
    11  
    12  	"github.com/golang/mock/gomock"
    13  	"github.com/libp2p/go-libp2p-core/peer"
    14  	"github.com/stretchr/testify/assert"
    15  	"google.golang.org/protobuf/proto"
    16  
    17  	"github.com/iotexproject/iotex-proto/golang/iotexrpc"
    18  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
    19  	"github.com/iotexproject/iotex-proto/golang/testingpb"
    20  )
    21  
    22  // TODO: define defaultChainID in chain.DefaultConfig
    23  var (
    24  	defaultChainID uint32 = 1
    25  )
    26  
    27  func createDispatcher(t *testing.T, chainID uint32) Dispatcher {
    28  	dp, err := NewDispatcher(DefaultConfig)
    29  	assert.NoError(t, err)
    30  	dp.AddSubscriber(chainID, &dummySubscriber{})
    31  	return dp
    32  }
    33  
    34  func startDispatcher(t *testing.T) (ctx context.Context, d Dispatcher) {
    35  	ctx = context.Background()
    36  	d = createDispatcher(t, defaultChainID)
    37  	assert.NotNil(t, d)
    38  	err := d.Start(ctx)
    39  	assert.NoError(t, err)
    40  	return
    41  }
    42  
    43  func stopDispatcher(ctx context.Context, d Dispatcher, t *testing.T) {
    44  	err := d.Stop(ctx)
    45  	assert.NoError(t, err)
    46  }
    47  
    48  func setTestCase() []proto.Message {
    49  	return []proto.Message{
    50  		&iotextypes.Action{},
    51  		&iotextypes.ConsensusMessage{},
    52  		&iotextypes.Block{},
    53  		&iotexrpc.BlockSync{},
    54  		&testingpb.TestPayload{},
    55  		&iotextypes.NodeInfoRequest{},
    56  		&iotextypes.NodeInfo{},
    57  	}
    58  }
    59  
    60  func TestHandleBroadcast(t *testing.T) {
    61  	msgs := setTestCase()
    62  	ctrl := gomock.NewController(t)
    63  	defer ctrl.Finish()
    64  
    65  	ctx, d := startDispatcher(t)
    66  	defer stopDispatcher(ctx, d, t)
    67  
    68  	for i := 0; i < 100; i++ {
    69  		for _, msg := range msgs {
    70  			d.HandleBroadcast(ctx, defaultChainID, "peer1", msg)
    71  		}
    72  	}
    73  }
    74  
    75  func TestHandleTell(t *testing.T) {
    76  	msgs := setTestCase()
    77  	ctrl := gomock.NewController(t)
    78  	defer ctrl.Finish()
    79  
    80  	ctx, d := startDispatcher(t)
    81  	defer stopDispatcher(ctx, d, t)
    82  
    83  	for i := 0; i < 100; i++ {
    84  		for _, msg := range msgs {
    85  			d.HandleTell(ctx, defaultChainID, peer.AddrInfo{}, msg)
    86  		}
    87  	}
    88  }
    89  
    90  type dummySubscriber struct{}
    91  
    92  func (ds *dummySubscriber) ReportFullness(context.Context, iotexrpc.MessageType, float32) {}
    93  
    94  func (ds *dummySubscriber) HandleBlock(context.Context, string, *iotextypes.Block) error { return nil }
    95  
    96  func (ds *dummySubscriber) HandleSyncRequest(context.Context, peer.AddrInfo, *iotexrpc.BlockSync) error {
    97  	return nil
    98  }
    99  
   100  func (ds *dummySubscriber) HandleAction(context.Context, *iotextypes.Action) error { return nil }
   101  
   102  func (ds *dummySubscriber) HandleConsensusMsg(*iotextypes.ConsensusMessage) error { return nil }
   103  
   104  func (ds *dummySubscriber) HandleNodeInfoRequest(context.Context, peer.AddrInfo, *iotextypes.NodeInfoRequest) error {
   105  	return nil
   106  }
   107  
   108  func (ds *dummySubscriber) HandleNodeInfo(context.Context, string, *iotextypes.NodeInfo) error {
   109  	return nil
   110  }