2019-08-23 16:53:38 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGoodByeRPCHandler_Disconnects_With_Peer(t *testing.T) {
|
|
|
|
p1 := p2ptest.NewTestP2P(t)
|
|
|
|
p2 := p2ptest.NewTestP2P(t)
|
|
|
|
p1.Connect(p2)
|
|
|
|
if len(p1.Host.Network().Peers()) != 1 {
|
|
|
|
t.Error("Expected peers to be connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a head state in the database with data we expect.
|
|
|
|
d := db.SetupDB(t)
|
2019-12-17 01:53:55 +00:00
|
|
|
r := &Service{
|
2019-08-23 16:53:38 +00:00
|
|
|
db: d,
|
|
|
|
p2p: p1,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup streams
|
|
|
|
pcl := protocol.ID("/testing")
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
p2.Host.SetStreamHandler(pcl, func(stream network.Stream) {
|
|
|
|
defer wg.Done()
|
|
|
|
expectResetStream(t, r, stream)
|
|
|
|
})
|
|
|
|
stream1, err := p1.Host.NewStream(context.Background(), p2.Host.ID(), pcl)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-11 15:08:02 +00:00
|
|
|
failureCode := codeClientShutdown
|
2019-08-23 16:53:38 +00:00
|
|
|
|
2020-02-11 15:08:02 +00:00
|
|
|
err = r.goodbyeRPCHandler(context.Background(), &failureCode, stream1)
|
2019-08-23 16:53:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unxpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if testutil.WaitTimeout(&wg, 1*time.Second) {
|
|
|
|
t.Fatal("Did not receive stream within 1 sec")
|
|
|
|
}
|
|
|
|
|
|
|
|
conns := p1.Host.Network().ConnsToPeer(p1.Host.ID())
|
|
|
|
if len(conns) > 0 {
|
|
|
|
t.Error("Peer is still not disconnected despite sending a goodbye message")
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 19:46:24 +00:00
|
|
|
|
|
|
|
func TestSendGoodbye_SendsMessage(t *testing.T) {
|
|
|
|
p1 := p2ptest.NewTestP2P(t)
|
|
|
|
p2 := p2ptest.NewTestP2P(t)
|
|
|
|
p1.Connect(p2)
|
|
|
|
if len(p1.Host.Network().Peers()) != 1 {
|
|
|
|
t.Error("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,
|
|
|
|
}
|
|
|
|
failureCode := codeClientShutdown
|
|
|
|
|
|
|
|
// Setup streams
|
|
|
|
pcl := protocol.ID("/eth2/beacon_chain/req/goodbye/1/ssz")
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
p2.Host.SetStreamHandler(pcl, func(stream network.Stream) {
|
|
|
|
defer wg.Done()
|
|
|
|
out := new(uint64)
|
|
|
|
if err := r.p2p.Encoding().DecodeWithLength(stream, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if *out != failureCode {
|
|
|
|
t.Fatalf("Wanted goodbye code of %d but got %d", failureCode, *out)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
err := r.sendGoodByeMessage(context.Background(), failureCode, p2.Host.ID())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unxpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if testutil.WaitTimeout(&wg, 1*time.Second) {
|
|
|
|
t.Fatal("Did not receive stream within 1 sec")
|
|
|
|
}
|
|
|
|
|
|
|
|
conns := p1.Host.Network().ConnsToPeer(p1.Host.ID())
|
|
|
|
if len(conns) > 0 {
|
|
|
|
t.Error("Peer is still not disconnected despite sending a goodbye message")
|
|
|
|
}
|
|
|
|
}
|