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 6d1a013
Show file tree
Hide file tree
Showing 13 changed files with 430 additions and 411 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/DataDog/zstd v1.5.2
github.com/Microsoft/go-winio v0.5.2
github.com/NYTimes/gziphandler v1.1.1
github.com/ava-labs/coreth v0.12.10-rc.0
github.com/ava-labs/coreth v0.12.9-rc.9.0.20231212082619-5b67f56f642d
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/ava-labs/coreth v0.12.10-rc.0 h1:qmuom7rtH5hc1E3lnqrMFNLFL1TMnEVa/2O8poB1YLU=
github.com/ava-labs/coreth v0.12.10-rc.0/go.mod h1:plFm/xzvWmx1+qJ3JQSTzF8+FdaA2xu7GgY/AdaZDfk=
github.com/ava-labs/coreth v0.12.9-rc.9.0.20231212082619-5b67f56f642d h1:QO/j255BRbbwE7uB/2Y6OKJqh80Z847KyuK1DHquaH8=
github.com/ava-labs/coreth v0.12.9-rc.9.0.20231212082619-5b67f56f642d/go.mod h1:plFm/xzvWmx1+qJ3JQSTzF8+FdaA2xu7GgY/AdaZDfk=
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34 h1:mg9Uw6oZFJKytJxgxnl3uxZOs/SB8CVHg6Io4Tf99Zc=
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34/go.mod h1:pJxaT9bUgeRNVmNRgtCHb7sFDIRKy7CzTQVi8gGNT6g=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
Expand Down
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 6d1a013

Please sign in to comment.