Skip to content

Commit

Permalink
refactor p2p unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Kim <[email protected]>
  • Loading branch information
joshua-kim committed Dec 12, 2023
1 parent e3d0278 commit 1ea7e04
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 408 deletions.
11 changes: 8 additions & 3 deletions network/p2p/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/set"
)

Expand Down Expand Up @@ -39,11 +38,17 @@ type CrossChainAppResponseCallback func(
err error,
)

type clientSender interface {
AppRequestSender
AppGossipSender
CrossChainAppRequestSender
}

type Client struct {
handlerID uint64
handlerPrefix []byte
router *router
sender common.AppSender
sender clientSender
options *clientOptions
}

Expand Down Expand Up @@ -88,7 +93,7 @@ func (c *Client) AppRequest(

if err := c.sender.SendAppRequest(
ctx,
set.Of(nodeID),
nodeID,
requestID,
appRequestBytes,
); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions network/p2p/gossip/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
var (
_ Gossiper = (*ValidatorGossiper)(nil)
_ Gossiper = (*PullGossiper[testTx, *testTx])(nil)
_ Gossiper = (*NoOpGossiper)(nil)
)

// Gossiper gossips Gossipables to other nodes
Expand Down Expand Up @@ -196,3 +197,9 @@ func Every(ctx context.Context, log logging.Logger, gossiper Gossiper, frequency
}
}
}

type NoOpGossiper struct{}

func (NoOpGossiper) Gossip(context.Context) error {
return nil
}
27 changes: 9 additions & 18 deletions network/p2p/gossip/gossip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/network/p2p"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/set"
)
Expand Down Expand Up @@ -115,8 +114,11 @@ func TestGossiperGossip(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)
ctx := context.Background()

responseSender := &common.SenderTest{}
responseSender := &p2p.FakeSender{
SentAppResponse: make(chan []byte, 1),
}
responseNetwork := p2p.NewNetwork(logging.NoLog{}, responseSender, prometheus.NewRegistry(), "")
responseBloom, err := NewBloomFilter(1000, 0.01)
require.NoError(err)
Expand All @@ -133,25 +135,13 @@ func TestGossiperGossip(t *testing.T) {
_, err = responseNetwork.NewAppProtocol(0x0, handler)
require.NoError(err)

requestSender := &common.SenderTest{
SendAppRequestF: func(ctx context.Context, nodeIDs set.Set[ids.NodeID], requestID uint32, request []byte) error {
go func() {
require.NoError(responseNetwork.AppRequest(ctx, ids.EmptyNodeID, requestID, time.Time{}, request))
}()
return nil
},
requestSender := &p2p.FakeSender{
SentAppRequest: make(chan []byte, 1),
}

requestNetwork := p2p.NewNetwork(logging.NoLog{}, requestSender, prometheus.NewRegistry(), "")
require.NoError(requestNetwork.Connected(context.Background(), ids.EmptyNodeID, nil))

gossiped := make(chan struct{})
responseSender.SendAppResponseF = func(ctx context.Context, nodeID ids.NodeID, requestID uint32, appResponseBytes []byte) error {
require.NoError(requestNetwork.AppResponse(ctx, nodeID, requestID, appResponseBytes))
close(gossiped)
return nil
}

bloom, err := NewBloomFilter(1000, 0.01)
require.NoError(err)
requestSet := testSet{
Expand Down Expand Up @@ -181,8 +171,9 @@ func TestGossiperGossip(t *testing.T) {
received.Add(tx)
}

require.NoError(gossiper.Gossip(context.Background()))
<-gossiped
require.NoError(gossiper.Gossip(ctx))
require.NoError(responseNetwork.AppRequest(ctx, ids.EmptyNodeID, 1, time.Time{}, <-requestSender.SentAppRequest))
require.NoError(requestNetwork.AppResponse(ctx, ids.EmptyNodeID, 1, <-responseSender.SentAppResponse))

require.Len(requestSet.set, tt.expectedLen)
require.Subset(tt.expectedPossibleValues, requestSet.set.List())
Expand Down
3 changes: 1 addition & 2 deletions network/p2p/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/message"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/logging"
)

Expand Down Expand Up @@ -92,7 +91,7 @@ type responder struct {
Handler
handlerID uint64
log logging.Logger
sender common.AppSender
sender routerSender
}

// AppRequest calls the underlying handler and sends back the response to nodeID
Expand Down
82 changes: 0 additions & 82 deletions network/p2p/mocks/mock_handler.go

This file was deleted.

4 changes: 2 additions & 2 deletions network/p2p/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type clientOptions struct {
// NewNetwork returns an instance of Network
func NewNetwork(
log logging.Logger,
sender common.AppSender,
sender AppSender,
metrics prometheus.Registerer,
namespace string,
) *Network {
Expand All @@ -72,7 +72,7 @@ type Network struct {
Peers *Peers

log logging.Logger
sender common.AppSender
sender AppSender
metrics prometheus.Registerer
namespace string

Expand Down
Loading

0 comments on commit 1ea7e04

Please sign in to comment.