github.com/MetalBlockchain/metalgo@v1.11.9/network/p2p/handler_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package p2p
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/MetalBlockchain/metalgo/ids"
    14  	"github.com/MetalBlockchain/metalgo/utils/logging"
    15  	"github.com/MetalBlockchain/metalgo/utils/set"
    16  )
    17  
    18  var _ ValidatorSet = (*testValidatorSet)(nil)
    19  
    20  type testValidatorSet struct {
    21  	validators set.Set[ids.NodeID]
    22  }
    23  
    24  func (t testValidatorSet) Has(_ context.Context, nodeID ids.NodeID) bool {
    25  	return t.validators.Contains(nodeID)
    26  }
    27  
    28  func TestValidatorHandlerAppGossip(t *testing.T) {
    29  	nodeID := ids.GenerateTestNodeID()
    30  	validatorSet := set.Of(nodeID)
    31  
    32  	tests := []struct {
    33  		name         string
    34  		validatorSet ValidatorSet
    35  		nodeID       ids.NodeID
    36  		expected     bool
    37  	}{
    38  		{
    39  			name:         "message dropped",
    40  			validatorSet: testValidatorSet{},
    41  			nodeID:       nodeID,
    42  		},
    43  		{
    44  			name: "message handled",
    45  			validatorSet: testValidatorSet{
    46  				validators: validatorSet,
    47  			},
    48  			nodeID:   nodeID,
    49  			expected: true,
    50  		},
    51  	}
    52  
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			require := require.New(t)
    56  
    57  			called := false
    58  			handler := NewValidatorHandler(
    59  				&TestHandler{
    60  					AppGossipF: func(context.Context, ids.NodeID, []byte) {
    61  						called = true
    62  					},
    63  				},
    64  				tt.validatorSet,
    65  				logging.NoLog{},
    66  			)
    67  
    68  			handler.AppGossip(context.Background(), tt.nodeID, []byte("foobar"))
    69  			require.Equal(tt.expected, called)
    70  		})
    71  	}
    72  }
    73  
    74  func TestValidatorHandlerAppRequest(t *testing.T) {
    75  	nodeID := ids.GenerateTestNodeID()
    76  	validatorSet := set.Of(nodeID)
    77  
    78  	tests := []struct {
    79  		name         string
    80  		validatorSet ValidatorSet
    81  		nodeID       ids.NodeID
    82  		expected     error
    83  	}{
    84  		{
    85  			name:         "message dropped",
    86  			validatorSet: testValidatorSet{},
    87  			nodeID:       nodeID,
    88  			expected:     ErrNotValidator,
    89  		},
    90  		{
    91  			name: "message handled",
    92  			validatorSet: testValidatorSet{
    93  				validators: validatorSet,
    94  			},
    95  			nodeID: nodeID,
    96  		},
    97  	}
    98  
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			require := require.New(t)
   102  
   103  			handler := NewValidatorHandler(
   104  				NoOpHandler{},
   105  				tt.validatorSet,
   106  				logging.NoLog{},
   107  			)
   108  
   109  			_, err := handler.AppRequest(context.Background(), tt.nodeID, time.Time{}, []byte("foobar"))
   110  			require.ErrorIs(err, tt.expected)
   111  		})
   112  	}
   113  }