prysm-pulse/beacon-chain/sync/rpc_test.go
Preston Van Loon 95c528f0bc First pass: single peer initial sync (#3363)
* lint

* add requests

* add all new stuff

* comment

* preston's review

* initial commit

* reorder sync so it isn't required to wait until start

* checkpoint

* fix

* improved handler API

* Set up prechain start values

* improved handler API

* ooops

* successful peer handshakes

* successful peer handshakes

* successful peer handshakes

* checkpoint

* chpkt

* handle init after chain start

* emit state initialized feed if existing db state

* merge error

* Done

* Test

* Fixed test

* emit state initialized

* force fork choice update

* wait for genesis time

* sync to current slot

* Use saved head in DB

* gaz

* fix tests

* lint

* lint

* lint

* lint

* Revert "Use saved head in DB"

This reverts commit c5f3404fdf333c8aac20bce8c349b1978494616b.

* remove db

* lint

* remove unused interfaces from composite

* resolve comments
2019-08-30 15:15:40 -05:00

69 lines
1.8 KiB
Go

package sync
import (
"bytes"
"context"
"sync"
"testing"
"time"
"github.com/gogo/protobuf/proto"
libp2pcore "github.com/libp2p/go-libp2p-core"
"github.com/libp2p/go-libp2p-core/network"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
pb "github.com/prysmaticlabs/prysm/proto/testing"
"github.com/prysmaticlabs/prysm/shared/testutil"
)
// expectSuccess status code from a stream in regular sync.
func expectSuccess(t *testing.T, r *RegularSync, stream network.Stream) {
code, errMsg, err := ReadStatusCode(stream, &encoder.SszNetworkEncoder{})
if err != nil {
t.Fatal(err)
}
if code != 0 {
t.Fatalf("Received non-zero response code: %d", code)
}
if errMsg != nil {
t.Fatalf("Received error message from stream: %+v", errMsg)
}
}
// expectResetStream status code from a stream in regular sync.
func expectResetStream(t *testing.T, r *RegularSync, stream network.Stream) {
expectedErr := "stream reset"
_, _, err := ReadStatusCode(stream, &encoder.SszNetworkEncoder{})
if err.Error() != expectedErr {
t.Fatalf("Wanted this error %s but got %v instead", expectedErr, err)
}
}
func TestRegisterRPC_ReceivesValidMessage(t *testing.T) {
p2p := p2ptest.NewTestP2P(t)
r := &RegularSync{
ctx: context.Background(),
p2p: p2p,
}
var wg sync.WaitGroup
wg.Add(1)
topic := "/testing/foobar/1"
handler := func(ctx context.Context, msg proto.Message, stream libp2pcore.Stream) error {
m := msg.(*pb.TestSimpleMessage)
if !bytes.Equal(m.Foo, []byte("foo")) {
t.Errorf("Unexpected incoming message: %+v", m)
}
wg.Done()
return nil
}
r.registerRPC(topic, &pb.TestSimpleMessage{}, handler)
p2p.ReceiveRPC(topic, &pb.TestSimpleMessage{Foo: []byte("foo")})
if testutil.WaitTimeout(&wg, time.Second) {
t.Fatal("Did not receive RPC in 1 second")
}
}