github.com/klaytn/klaytn@v1.12.1/node/cn/channel_manager_test.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package cn
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/klaytn/klaytn/consensus/istanbul/backend"
    23  	"github.com/klaytn/klaytn/networks/p2p"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  // TestChannelManager_ChannelSize_1 tests registering and retrieving of
    28  // message channels with the channel size 1.
    29  func TestChannelManager_ChannelSize_1(t *testing.T) {
    30  	testChannelManager(t, 1)
    31  }
    32  
    33  // TestChannelManager_ChannelSize_3 tests registering and retrieving of
    34  // message channels with the channel size 3.
    35  func TestChannelManager_ChannelSize_3(t *testing.T) {
    36  	testChannelManager(t, 3)
    37  }
    38  
    39  // TestChannelManager_ChannelSize_5 tests registering and retrieving of
    40  // message channels with the channel size 5.
    41  func TestChannelManager_ChannelSize_5(t *testing.T) {
    42  	testChannelManager(t, 5)
    43  }
    44  
    45  // testChannelManager tests registering and retrieving of
    46  // message channels with the given channel size.
    47  func testChannelManager(t *testing.T, chSize int) {
    48  	cm := NewChannelManager(chSize)
    49  	cm.RegisterMsgCode(ConsensusChannel, backend.IstanbulMsg)
    50  
    51  	channel := make(chan p2p.Msg, channelSizePerPeer)
    52  	consensusChannel := make(chan p2p.Msg, channelSizePerPeer)
    53  
    54  	for chIdx := 0; chIdx < chSize; chIdx++ {
    55  		// Before calling RegisterChannelWithIndex,
    56  		// calling GetChannelWithMsgCode with registered MsgCode should return no channel and no error.
    57  		for i := StatusMsg; i < MsgCodeEnd; i++ {
    58  			if i == Unused10 || i == Unused11 {
    59  				// skip for dummy messages
    60  				continue
    61  			}
    62  			ch, err := cm.GetChannelWithMsgCode(chIdx, uint64(i))
    63  			assert.Nil(t, ch)
    64  			assert.NoError(t, err)
    65  		}
    66  		ch, err := cm.GetChannelWithMsgCode(chIdx, backend.IstanbulMsg)
    67  		assert.Nil(t, ch)
    68  		assert.NoError(t, err)
    69  
    70  		// Before calling RegisterChannelWithIndex,
    71  		// calling GetChannelWithMsgCode with not-registered MsgCode should return no channel but an error.
    72  		ch, err = cm.GetChannelWithMsgCode(chIdx, MsgCodeEnd)
    73  		assert.Nil(t, ch)
    74  		assert.Error(t, err)
    75  	}
    76  
    77  	// Register channels with the port index.
    78  	for chIdx := 0; chIdx < chSize; chIdx++ {
    79  		cm.RegisterChannelWithIndex(chIdx, BlockChannel, channel)
    80  		cm.RegisterChannelWithIndex(chIdx, TxChannel, channel)
    81  		cm.RegisterChannelWithIndex(chIdx, MiscChannel, channel)
    82  		cm.RegisterChannelWithIndex(chIdx, ConsensusChannel, consensusChannel)
    83  	}
    84  
    85  	for chIdx := 0; chIdx < chSize; chIdx++ {
    86  		// After calling RegisterChannelWithIndex,
    87  		// calling GetChannelWithMsgCode with registered MsgCode should return a channel but no error.
    88  		for i := StatusMsg; i < MsgCodeEnd; i++ {
    89  			if i == Unused10 || i == Unused11 {
    90  				// skip for dummy messages
    91  				continue
    92  			}
    93  			ch, err := cm.GetChannelWithMsgCode(chIdx, uint64(i))
    94  			assert.Equal(t, channel, ch)
    95  			assert.NoError(t, err)
    96  		}
    97  		ch, err := cm.GetChannelWithMsgCode(chIdx, backend.IstanbulMsg)
    98  		assert.Equal(t, consensusChannel, ch)
    99  		assert.NoError(t, err)
   100  
   101  		// After calling RegisterChannelWithIndex,
   102  		// calling GetChannelWithMsgCode with not-registered MsgCode should return no channel but an error.
   103  		ch, err = cm.GetChannelWithMsgCode(0, MsgCodeEnd)
   104  		assert.Nil(t, ch)
   105  		assert.Error(t, err)
   106  	}
   107  }