github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/poll/protocol_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 poll
     7  
     8  import (
     9  	"context"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/golang/mock/gomock"
    14  	"github.com/stretchr/testify/require"
    15  
    16  	"github.com/iotexproject/go-pkgs/hash"
    17  	"github.com/iotexproject/iotex-election/test/mock/mock_committee"
    18  
    19  	"github.com/iotexproject/iotex-core/action/protocol"
    20  	"github.com/iotexproject/iotex-core/blockchain"
    21  	"github.com/iotexproject/iotex-core/blockchain/genesis"
    22  )
    23  
    24  func TestNewProtocol(t *testing.T) {
    25  	require := require.New(t)
    26  	ctrl := gomock.NewController(t)
    27  	committee := mock_committee.NewMockCommittee(ctrl)
    28  	g := genesis.Default
    29  	g.ScoreThreshold = "1200000"
    30  	p, err := NewProtocol(
    31  		_rollDPoSScheme,
    32  		blockchain.DefaultConfig,
    33  		g,
    34  		nil,
    35  		func(context.Context, string, []byte, bool) ([]byte, error) { return nil, nil },
    36  		nil,
    37  		nil,
    38  		nil,
    39  		committee,
    40  		nil,
    41  		func(uint64) (time.Time, error) { return time.Now(), nil },
    42  		func(uint64, uint64) (map[string]uint64, error) {
    43  			return nil, nil
    44  		},
    45  		func(uint64) (hash.Hash256, error) {
    46  			return hash.ZeroHash256, nil
    47  		},
    48  		func(u uint64) (time.Time, error) {
    49  			return time.Time{}, nil
    50  		},
    51  	)
    52  	require.NoError(err)
    53  	require.NotNil(p)
    54  }
    55  
    56  func TestFindProtocol(t *testing.T) {
    57  	require := require.New(t)
    58  	ctrl := gomock.NewController(t)
    59  	p, _, _, _, _ := initConstruct(ctrl)
    60  	//if not registered
    61  	re := protocol.NewRegistry()
    62  	require.Nil(FindProtocol(re))
    63  
    64  	//if registered
    65  	require.NoError(p.Register(re))
    66  	require.NotNil(FindProtocol(re))
    67  }
    68  
    69  func TestMustGetProtocol(t *testing.T) {
    70  	require := require.New(t)
    71  	ctrl := gomock.NewController(t)
    72  	p, _, _, _, _ := initConstruct(ctrl)
    73  	//if not registered
    74  	re := protocol.NewRegistry()
    75  	require.Panics(func() { MustGetProtocol(re) })
    76  
    77  	//if registered
    78  	require.NoError(p.Register(re))
    79  	require.NotNil(FindProtocol(re))
    80  }