prysm-pulse/beacon-chain/sync/rpc_goodbye_test.go
Nishant Das 022b6667e5
Use Custom SSZ for P2P Types (#7436)
* checkpoint progress

* add roundtrip tests

* change all

* remove error response

* clean up

* Update beacon-chain/sync/error_test.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

* gaz

* fix tests

* fmt

* gaz

* change back

* fix again

* clean up

* deep source

* fix all tests

* add gaz

* fix tests

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2020-10-14 07:55:28 +00:00

142 lines
4.2 KiB
Go

package sync
import (
"context"
"sync"
"testing"
"time"
"github.com/kevinms/leakybucket-go"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/protocol"
db "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
p2pTypes "github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestGoodByeRPCHandler_Disconnects_With_Peer(t *testing.T) {
p1 := p2ptest.NewTestP2P(t)
p2 := p2ptest.NewTestP2P(t)
p1.Connect(p2)
assert.Equal(t, 1, len(p1.BHost.Network().Peers()), "Expected peers to be connected")
// Set up a head state in the database with data we expect.
d, _ := db.SetupDB(t)
r := &Service{
db: d,
p2p: p1,
rateLimiter: newRateLimiter(p1),
}
// Setup streams
pcl := protocol.ID("/testing")
topic := string(pcl)
r.rateLimiter.limiterMap[topic] = leakybucket.NewCollector(1, 1, false)
var wg sync.WaitGroup
wg.Add(1)
p2.BHost.SetStreamHandler(pcl, func(stream network.Stream) {
defer wg.Done()
expectResetStream(t, stream)
})
stream1, err := p1.BHost.NewStream(context.Background(), p2.BHost.ID(), pcl)
require.NoError(t, err)
failureCode := codeClientShutdown
assert.NoError(t, r.goodbyeRPCHandler(context.Background(), &failureCode, stream1))
if testutil.WaitTimeout(&wg, 1*time.Second) {
t.Fatal("Did not receive stream within 1 sec")
}
conns := p1.BHost.Network().ConnsToPeer(p1.BHost.ID())
if len(conns) > 0 {
t.Error("Peer is still not disconnected despite sending a goodbye message")
}
}
func TestSendGoodbye_SendsMessage(t *testing.T) {
p1 := p2ptest.NewTestP2P(t)
p2 := p2ptest.NewTestP2P(t)
p1.Connect(p2)
assert.Equal(t, 1, len(p1.BHost.Network().Peers()), "Expected peers to be connected")
// Set up a head state in the database with data we expect.
d, _ := db.SetupDB(t)
r := &Service{
db: d,
p2p: p1,
rateLimiter: newRateLimiter(p1),
}
failureCode := codeClientShutdown
// Setup streams
pcl := protocol.ID("/eth2/beacon_chain/req/goodbye/1/ssz_snappy")
topic := string(pcl)
r.rateLimiter.limiterMap[topic] = leakybucket.NewCollector(1, 1, false)
var wg sync.WaitGroup
wg.Add(1)
p2.BHost.SetStreamHandler(pcl, func(stream network.Stream) {
defer wg.Done()
out := new(p2pTypes.SSZUint64)
assert.NoError(t, r.p2p.Encoding().DecodeWithMaxLength(stream, out))
assert.Equal(t, failureCode, *out)
assert.NoError(t, stream.Close())
})
err := r.sendGoodByeMessage(context.Background(), failureCode, p2.BHost.ID())
assert.NoError(t, err)
if testutil.WaitTimeout(&wg, 1*time.Second) {
t.Fatal("Did not receive stream within 1 sec")
}
conns := p1.BHost.Network().ConnsToPeer(p1.BHost.ID())
if len(conns) > 0 {
t.Error("Peer is still not disconnected despite sending a goodbye message")
}
}
func TestSendGoodbye_DisconnectWithPeer(t *testing.T) {
p1 := p2ptest.NewTestP2P(t)
p2 := p2ptest.NewTestP2P(t)
p1.Connect(p2)
assert.Equal(t, 1, len(p1.BHost.Network().Peers()), "Expected peers to be connected")
// Set up a head state in the database with data we expect.
d, _ := db.SetupDB(t)
r := &Service{
db: d,
p2p: p1,
rateLimiter: newRateLimiter(p1),
}
failureCode := codeClientShutdown
// Setup streams
pcl := protocol.ID("/eth2/beacon_chain/req/goodbye/1/ssz_snappy")
topic := string(pcl)
r.rateLimiter.limiterMap[topic] = leakybucket.NewCollector(1, 1, false)
var wg sync.WaitGroup
wg.Add(1)
p2.BHost.SetStreamHandler(pcl, func(stream network.Stream) {
defer wg.Done()
out := new(p2pTypes.SSZUint64)
assert.NoError(t, r.p2p.Encoding().DecodeWithMaxLength(stream, out))
assert.Equal(t, failureCode, *out)
assert.NoError(t, stream.Close())
})
assert.NoError(t, r.sendGoodByeAndDisconnect(context.Background(), failureCode, p2.BHost.ID()))
conns := p1.BHost.Network().ConnsToPeer(p2.BHost.ID())
if len(conns) > 0 {
t.Error("Peer is still not disconnected despite sending a goodbye message")
}
if testutil.WaitTimeout(&wg, 1*time.Second) {
t.Fatal("Did not receive stream within 1 sec")
}
}