2018-06-06 02:38:16 +00:00
|
|
|
package p2p
|
|
|
|
|
2018-07-17 18:39:04 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2018-11-15 12:54:45 +00:00
|
|
|
"fmt"
|
2018-07-17 18:39:04 +00:00
|
|
|
"reflect"
|
2018-11-03 16:59:39 +00:00
|
|
|
"strings"
|
2018-07-25 16:11:15 +00:00
|
|
|
"sync"
|
2018-07-17 18:39:04 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-03-08 02:37:15 +00:00
|
|
|
ggio "github.com/gogo/protobuf/io"
|
2018-12-23 20:34:59 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-12-23 22:51:04 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2018-08-29 16:32:54 +00:00
|
|
|
bhost "github.com/libp2p/go-libp2p-blankhost"
|
2019-03-08 02:37:15 +00:00
|
|
|
pstore "github.com/libp2p/go-libp2p-peerstore"
|
|
|
|
protocol "github.com/libp2p/go-libp2p-protocol"
|
2018-11-03 16:59:39 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2018-07-17 18:39:04 +00:00
|
|
|
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
|
2019-03-17 02:56:05 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2018-07-31 18:54:45 +00:00
|
|
|
shardpb "github.com/prysmaticlabs/prysm/proto/sharding/p2p/v1"
|
2018-08-29 16:32:54 +00:00
|
|
|
testpb "github.com/prysmaticlabs/prysm/proto/testing"
|
2019-01-13 04:35:00 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
2018-11-03 16:59:39 +00:00
|
|
|
p2pmock "github.com/prysmaticlabs/prysm/shared/p2p/mock"
|
2019-03-06 04:57:44 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2018-07-21 17:51:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-08-29 16:32:54 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
2018-07-17 18:39:04 +00:00
|
|
|
)
|
2018-06-06 02:38:16 +00:00
|
|
|
|
2018-06-13 12:44:33 +00:00
|
|
|
// Ensure that server implements service.
|
2019-01-13 04:35:00 +00:00
|
|
|
var _ = shared.Service(&Server{})
|
2019-02-05 13:47:25 +00:00
|
|
|
var _ = Broadcaster(&Server{})
|
2019-03-08 02:37:15 +00:00
|
|
|
var _ = Sender(&Server{})
|
2018-07-17 18:39:04 +00:00
|
|
|
|
2019-04-26 06:24:01 +00:00
|
|
|
const bar = "bar"
|
|
|
|
const testTopic = "test_topic"
|
|
|
|
|
2018-07-17 18:39:04 +00:00
|
|
|
func init() {
|
2018-07-21 17:51:18 +00:00
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
2018-07-17 18:39:04 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 19:08:27 +00:00
|
|
|
func TestNewServer_InvalidMultiaddress(t *testing.T) {
|
|
|
|
_, err := NewServer(&ServerConfig{
|
2018-11-25 16:55:02 +00:00
|
|
|
RelayNodeAddr: "bad",
|
|
|
|
})
|
|
|
|
|
2019-04-27 19:08:27 +00:00
|
|
|
if err.Error() != "invalid multiaddr, must begin with /" {
|
|
|
|
t.Fatal("expected invalid multiaddr err")
|
2018-11-25 16:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestP2P_PortTaken(t *testing.T) {
|
|
|
|
port := 10000
|
2018-12-17 08:57:22 +00:00
|
|
|
_, err := NewServer(&ServerConfig{
|
2019-02-22 15:11:26 +00:00
|
|
|
Port: port,
|
2018-12-17 08:57:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create server: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = NewServer(&ServerConfig{
|
2019-02-22 15:11:26 +00:00
|
|
|
Port: port,
|
2018-12-17 08:57:22 +00:00
|
|
|
})
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
if !strings.Contains(err.Error(), fmt.Sprintf("port %d already taken", port)) {
|
2018-12-17 08:57:22 +00:00
|
|
|
t.Fatalf("expected fail when setting another server with same p2p port")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestBroadcast_OK(t *testing.T) {
|
2019-01-19 00:27:21 +00:00
|
|
|
s, err := NewServer(&ServerConfig{})
|
2018-07-17 18:39:04 +00:00
|
|
|
if err != nil {
|
2019-01-19 00:27:21 +00:00
|
|
|
t.Fatalf("Could not start a new server: %v", err)
|
2018-07-17 18:39:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 18:54:45 +00:00
|
|
|
msg := &shardpb.CollationBodyRequest{}
|
2019-03-17 02:56:05 +00:00
|
|
|
s.Broadcast(context.Background(), msg)
|
2018-07-17 18:39:04 +00:00
|
|
|
|
2019-01-19 00:27:21 +00:00
|
|
|
// TODO(543): test that topic was published
|
2018-07-17 18:39:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestEmit_OK(t *testing.T) {
|
2018-11-25 16:55:02 +00:00
|
|
|
s, _ := NewServer(&ServerConfig{})
|
2019-04-26 06:24:01 +00:00
|
|
|
p := &testpb.TestMessage{Foo: bar}
|
2018-11-03 16:59:39 +00:00
|
|
|
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
feed := p2pmock.NewMockFeed(ctrl)
|
|
|
|
var got Message
|
|
|
|
feed.EXPECT().Send(gomock.AssignableToTypeOf(Message{})).Times(1).Do(func(m Message) {
|
|
|
|
got = m
|
|
|
|
})
|
2018-11-15 12:54:45 +00:00
|
|
|
s.emit(Message{Ctx: context.Background(), Data: p}, feed)
|
2018-11-03 16:59:39 +00:00
|
|
|
if !proto.Equal(p, got.Data) {
|
|
|
|
t.Error("feed was not called with the correct data")
|
|
|
|
}
|
|
|
|
}
|
2018-09-16 18:02:53 +00:00
|
|
|
|
2019-03-08 02:37:15 +00:00
|
|
|
func TestSubscribeToTopic_onPubSub_OK(t *testing.T) {
|
2018-07-17 18:39:04 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
|
|
|
|
defer cancel()
|
2018-08-29 16:32:54 +00:00
|
|
|
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
2019-03-08 02:37:15 +00:00
|
|
|
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
2018-07-17 18:39:04 +00:00
|
|
|
|
2019-03-08 02:37:15 +00:00
|
|
|
gsub, err := pubsub.NewFloodSub(ctx, h2)
|
2018-07-17 18:39:04 +00:00
|
|
|
if err != nil {
|
2018-11-03 16:59:39 +00:00
|
|
|
t.Errorf("Failed to create pubsub: %v", err)
|
2018-07-17 18:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := Server{
|
2018-08-29 16:32:54 +00:00
|
|
|
ctx: ctx,
|
|
|
|
gsub: gsub,
|
|
|
|
host: h,
|
2018-09-16 18:02:53 +00:00
|
|
|
feeds: make(map[reflect.Type]Feed),
|
2018-08-29 16:32:54 +00:00
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
topicMapping: make(map[reflect.Type]string),
|
2018-07-17 18:39:04 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 22:15:24 +00:00
|
|
|
feed := s.Feed(&shardpb.CollationBodyRequest{})
|
2018-07-17 18:39:04 +00:00
|
|
|
ch := make(chan Message)
|
|
|
|
sub := feed.Subscribe(ch)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
2018-07-29 01:18:56 +00:00
|
|
|
testSubscribe(ctx, t, s, gsub, ch)
|
2018-07-26 14:16:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 02:37:15 +00:00
|
|
|
func TestSubscribeToTopic_directMessaging_OK(t *testing.T) {
|
2018-07-26 14:16:31 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
|
|
|
|
defer cancel()
|
2018-08-29 16:32:54 +00:00
|
|
|
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
2018-07-26 14:16:31 +00:00
|
|
|
|
2018-11-03 16:59:39 +00:00
|
|
|
gsub, err := pubsub.NewFloodSub(ctx, h)
|
2018-07-26 14:16:31 +00:00
|
|
|
if err != nil {
|
2018-11-03 16:59:39 +00:00
|
|
|
t.Errorf("Failed to create pubsub: %v", err)
|
2018-07-26 14:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := Server{
|
2018-08-29 16:32:54 +00:00
|
|
|
ctx: ctx,
|
|
|
|
gsub: gsub,
|
|
|
|
host: h,
|
2018-09-16 18:02:53 +00:00
|
|
|
feeds: make(map[reflect.Type]Feed),
|
2018-08-29 16:32:54 +00:00
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
topicMapping: make(map[reflect.Type]string),
|
2018-07-26 14:16:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 02:37:15 +00:00
|
|
|
feed := s.Feed(&shardpb.CollationBodyRequest{})
|
|
|
|
ch := make(chan Message)
|
|
|
|
sub := feed.Subscribe(ch)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
topic := shardpb.Topic_COLLATION_BODY_REQUEST
|
|
|
|
|
|
|
|
s.RegisterTopic(topic.String(), &shardpb.CollationBodyRequest{})
|
|
|
|
pbMsg := &shardpb.CollationBodyRequest{ShardId: 5}
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
// The message should be received from the feed.
|
|
|
|
msg := <-ch
|
|
|
|
if !proto.Equal(msg.Data.(proto.Message), pbMsg) {
|
|
|
|
t.Errorf("Unexpected msg: %+v. Wanted %+v.", msg.Data, pbMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
|
|
|
if err := h2.Connect(ctx, pstore.PeerInfo{ID: h.ID(), Addrs: h.Addrs()}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
stream, err := h2.NewStream(ctx, h.ID(), protocol.ID(prysmProtocolPrefix+"/"+topic.String()))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
w := ggio.NewDelimitedWriter(stream)
|
|
|
|
defer w.Close()
|
2019-03-17 02:56:05 +00:00
|
|
|
w.WriteMsg(createEnvelope(t, pbMsg))
|
2019-03-08 02:37:15 +00:00
|
|
|
|
|
|
|
// Wait for our message assertion to complete.
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Error("Context timed out before a message was received!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubscribe_OK(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
|
|
|
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
|
|
|
|
|
|
|
gsub, err := pubsub.NewFloodSub(ctx, h2)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create pubsub: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := Server{
|
|
|
|
ctx: ctx,
|
|
|
|
gsub: gsub,
|
|
|
|
host: h,
|
|
|
|
feeds: make(map[reflect.Type]Feed),
|
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
topicMapping: make(map[reflect.Type]string),
|
|
|
|
}
|
|
|
|
|
2018-07-26 14:16:31 +00:00
|
|
|
ch := make(chan Message)
|
2018-09-09 22:15:24 +00:00
|
|
|
sub := s.Subscribe(&shardpb.CollationBodyRequest{}, ch)
|
2018-07-26 14:16:31 +00:00
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
2018-07-29 01:18:56 +00:00
|
|
|
testSubscribe(ctx, t, s, gsub, ch)
|
2018-07-26 14:16:31 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 16:59:39 +00:00
|
|
|
func testSubscribe(ctx context.Context, t *testing.T, s Server, gsub *pubsub.PubSub, ch chan Message) {
|
2018-07-31 18:54:45 +00:00
|
|
|
topic := shardpb.Topic_COLLATION_BODY_REQUEST
|
2018-08-29 16:32:54 +00:00
|
|
|
|
2018-09-09 22:15:24 +00:00
|
|
|
s.RegisterTopic(topic.String(), &shardpb.CollationBodyRequest{})
|
2018-07-17 18:39:04 +00:00
|
|
|
|
|
|
|
// Short delay to let goroutine add subscription.
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
|
|
|
|
// The topic should be subscribed with gsub.
|
|
|
|
topics := gsub.GetTopics()
|
|
|
|
if len(topics) < 1 || topics[0] != topic.String() {
|
|
|
|
t.Errorf("Unexpected subscribed topics: %v. Wanted %s", topics, topic)
|
|
|
|
}
|
|
|
|
|
2018-07-31 18:54:45 +00:00
|
|
|
pbMsg := &shardpb.CollationBodyRequest{ShardId: 5}
|
2018-07-17 18:39:04 +00:00
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
// The message should be received from the feed.
|
|
|
|
msg := <-ch
|
|
|
|
if !proto.Equal(msg.Data.(proto.Message), pbMsg) {
|
|
|
|
t.Errorf("Unexpected msg: %+v. Wanted %+v.", msg.Data, pbMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
2019-03-17 02:56:05 +00:00
|
|
|
if err := gsub.Publish(topic.String(), createEnvelopeBytes(t, pbMsg)); err != nil {
|
2018-07-17 18:39:04 +00:00
|
|
|
t.Errorf("Failed to publish message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for our message assertion to complete.
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Error("Context timed out before a message was received!")
|
|
|
|
}
|
|
|
|
}
|
2018-08-29 16:32:54 +00:00
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestRegisterTopic_InvalidProtobufs(t *testing.T) {
|
2018-11-15 12:54:45 +00:00
|
|
|
topic := shardpb.Topic_COLLATION_BODY_REQUEST
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
2019-03-08 02:37:15 +00:00
|
|
|
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
2018-11-15 12:54:45 +00:00
|
|
|
|
2019-03-08 02:37:15 +00:00
|
|
|
gsub, err := pubsub.NewFloodSub(ctx, h2)
|
2018-11-15 12:54:45 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create floodsub: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := Server{
|
|
|
|
ctx: ctx,
|
|
|
|
gsub: gsub,
|
|
|
|
host: h,
|
|
|
|
feeds: make(map[reflect.Type]Feed),
|
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
topicMapping: make(map[reflect.Type]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.RegisterTopic(topic.String(), &shardpb.CollationBodyRequest{})
|
|
|
|
ch := make(chan Message)
|
|
|
|
sub := s.Subscribe(&shardpb.CollationBodyRequest{}, ch)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
if err = gsub.Publish(topic.String(), []byte("invalid protobuf message")); err != nil {
|
|
|
|
t.Errorf("Failed to publish message: %v", err)
|
|
|
|
}
|
|
|
|
pbMsg := &shardpb.CollationBodyRequest{ShardId: 5}
|
2019-03-17 02:56:05 +00:00
|
|
|
if err = gsub.Publish(topic.String(), createEnvelopeBytes(t, pbMsg)); err != nil {
|
2018-11-15 12:54:45 +00:00
|
|
|
t.Errorf("Failed to publish message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Error("Context timed out before a message was received!")
|
|
|
|
case <-ch:
|
|
|
|
logContains(t, hook, "Failed to decode data", logrus.ErrorLevel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 16:32:54 +00:00
|
|
|
func TestRegisterTopic_WithoutAdapters(t *testing.T) {
|
2018-11-25 16:55:02 +00:00
|
|
|
s, err := NewServer(&ServerConfig{})
|
2018-08-29 16:32:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create new server: %v", err)
|
|
|
|
}
|
2019-04-26 06:24:01 +00:00
|
|
|
topic := testTopic
|
|
|
|
testMessage := &testpb.TestMessage{Foo: bar}
|
2018-08-29 16:32:54 +00:00
|
|
|
|
2018-09-09 22:15:24 +00:00
|
|
|
s.RegisterTopic(topic, testMessage)
|
2018-08-29 16:32:54 +00:00
|
|
|
|
|
|
|
ch := make(chan Message)
|
|
|
|
sub := s.Subscribe(testMessage, ch)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
wait := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(wait)
|
2018-09-09 22:15:24 +00:00
|
|
|
msg := <-ch
|
|
|
|
tmsg := msg.Data.(*testpb.TestMessage)
|
2019-04-26 06:24:01 +00:00
|
|
|
if tmsg.Foo != bar {
|
|
|
|
t.Errorf("Expected test message foo:\"bar\". Got: %v", tmsg)
|
2018-09-09 22:15:24 +00:00
|
|
|
}
|
2018-08-29 16:32:54 +00:00
|
|
|
}()
|
|
|
|
|
2019-03-06 04:57:44 +00:00
|
|
|
if err := simulateIncomingMessage(t, s, topic, testMessage); err != nil {
|
2018-08-29 16:32:54 +00:00
|
|
|
t.Errorf("Failed to send to topic %s", topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-wait:
|
|
|
|
return // OK
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("TestMessage not received within 1 seconds")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 22:15:24 +00:00
|
|
|
func TestRegisterTopic_WithAdapters(t *testing.T) {
|
2018-11-25 16:55:02 +00:00
|
|
|
s, err := NewServer(&ServerConfig{})
|
2018-08-29 16:32:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create new server: %v", err)
|
|
|
|
}
|
2019-04-26 06:24:01 +00:00
|
|
|
topic := testTopic
|
|
|
|
testMessage := &testpb.TestMessage{Foo: bar}
|
2018-08-29 16:32:54 +00:00
|
|
|
|
|
|
|
i := 0
|
|
|
|
var testAdapter Adapter = func(next Handler) Handler {
|
2018-09-20 11:46:35 +00:00
|
|
|
return func(msg Message) {
|
2018-08-29 16:32:54 +00:00
|
|
|
i++
|
2018-09-20 11:46:35 +00:00
|
|
|
next(msg)
|
2018-08-29 16:32:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
adapters := []Adapter{
|
|
|
|
testAdapter,
|
|
|
|
testAdapter,
|
|
|
|
testAdapter,
|
|
|
|
testAdapter,
|
|
|
|
testAdapter,
|
|
|
|
}
|
|
|
|
|
2018-09-09 22:15:24 +00:00
|
|
|
s.RegisterTopic(topic, testMessage, adapters...)
|
2018-08-29 16:32:54 +00:00
|
|
|
|
|
|
|
ch := make(chan Message)
|
|
|
|
sub := s.Subscribe(testMessage, ch)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
wait := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(wait)
|
2018-09-09 22:15:24 +00:00
|
|
|
msg := <-ch
|
|
|
|
tmsg := msg.Data.(*testpb.TestMessage)
|
2019-04-26 06:24:01 +00:00
|
|
|
if tmsg.Foo != bar {
|
2018-09-09 22:15:24 +00:00
|
|
|
t.Errorf("Expected test message Foo: \"bar\". Got: %v", tmsg)
|
|
|
|
}
|
2018-08-29 16:32:54 +00:00
|
|
|
}()
|
|
|
|
|
2019-03-06 04:57:44 +00:00
|
|
|
if err := simulateIncomingMessage(t, s, topic, testMessage); err != nil {
|
2018-08-29 16:32:54 +00:00
|
|
|
t.Errorf("Failed to send to topic %s", topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-wait:
|
|
|
|
if i != 5 {
|
|
|
|
t.Errorf("Expected testAdapter to increment i to 5, but was %d", i)
|
|
|
|
}
|
|
|
|
return // OK
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("TestMessage not received within 1 seconds")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 04:57:44 +00:00
|
|
|
func TestRegisterTopic_HandlesPanic(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
|
|
|
s, err := NewServer(&ServerConfig{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create new server: %v", err)
|
|
|
|
}
|
2019-04-26 06:24:01 +00:00
|
|
|
topic := testTopic
|
|
|
|
testMessage := &testpb.TestMessage{Foo: bar}
|
2019-03-06 04:57:44 +00:00
|
|
|
|
|
|
|
var panicAdapter Adapter = func(next Handler) Handler {
|
|
|
|
return func(msg Message) {
|
|
|
|
panic("bad!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.RegisterTopic(topic, testMessage, panicAdapter)
|
|
|
|
|
|
|
|
ch := make(chan Message)
|
|
|
|
sub := s.Subscribe(testMessage, ch)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
if err := simulateIncomingMessage(t, s, topic, testMessage); err != nil {
|
|
|
|
t.Errorf("Failed to send to topic %s", topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.WaitForLog(t, hook, "P2P message caused a panic")
|
|
|
|
}
|
|
|
|
|
2018-12-30 21:20:43 +00:00
|
|
|
func TestStatus_MinimumPeers(t *testing.T) {
|
2019-04-05 20:09:05 +00:00
|
|
|
minPeers := 3
|
2018-12-30 21:20:43 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
|
|
|
s := Server{host: h}
|
|
|
|
|
|
|
|
err := s.Status()
|
2019-04-05 20:09:05 +00:00
|
|
|
if err == nil || err.Error() != "less than 3 peers" {
|
2018-12-30 21:20:43 +00:00
|
|
|
t.Errorf("p2p server did not return expected status, instead returned %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < minPeers; i++ {
|
|
|
|
other := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
|
|
|
if err := h.Connect(ctx, other.Peerstore().PeerInfo(other.ID())); err != nil {
|
|
|
|
t.Fatalf("Could not connect to host for test setup")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Status(); err != nil {
|
|
|
|
t.Errorf("Unexpected server status %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 04:57:44 +00:00
|
|
|
func simulateIncomingMessage(t *testing.T, s *Server, topic string, msg proto.Message) error {
|
|
|
|
ctx := context.Background()
|
2018-08-29 16:32:54 +00:00
|
|
|
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
|
|
|
|
|
2019-04-27 19:08:27 +00:00
|
|
|
setHandshakeHandler(h, "")
|
|
|
|
|
2018-11-03 16:59:39 +00:00
|
|
|
gsub, err := pubsub.NewFloodSub(ctx, h)
|
2018-08-29 16:32:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pinfo := h.Peerstore().PeerInfo(h.ID())
|
|
|
|
if err = s.host.Connect(ctx, pinfo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Short timeout to allow libp2p to handle peer connection.
|
2019-03-06 04:57:44 +00:00
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
2019-03-17 02:56:05 +00:00
|
|
|
return gsub.Publish(topic, createEnvelopeBytes(t, msg))
|
|
|
|
}
|
|
|
|
|
|
|
|
func createEnvelope(t *testing.T, msg proto.Message) *pb.Envelope {
|
|
|
|
payload, err := proto.Marshal(msg)
|
2019-03-06 04:57:44 +00:00
|
|
|
if err != nil {
|
2019-03-17 02:56:05 +00:00
|
|
|
t.Fatal(err)
|
2019-03-06 04:57:44 +00:00
|
|
|
}
|
2018-08-29 16:32:54 +00:00
|
|
|
|
2019-03-17 02:56:05 +00:00
|
|
|
// span context test data from
|
|
|
|
// https://github.com/census-instrumentation/opencensus-go/blob/3b8e2721f2c3c01fa1bf4a2e455874e7b8319cd7/trace/propagation/propagation_test.go#L69
|
|
|
|
envelope := &pb.Envelope{
|
|
|
|
SpanContext: []byte{0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, 100, 101, 102, 103, 104, 2, 1},
|
|
|
|
Payload: payload,
|
|
|
|
}
|
|
|
|
|
|
|
|
return envelope
|
|
|
|
}
|
|
|
|
|
|
|
|
func createEnvelopeBytes(t *testing.T, msg proto.Message) []byte {
|
|
|
|
b, err := proto.Marshal(createEnvelope(t, msg))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return b
|
2018-08-29 16:32:54 +00:00
|
|
|
}
|
2018-11-15 12:54:45 +00:00
|
|
|
|
|
|
|
func logContains(t *testing.T, hook *logTest.Hook, message string, level logrus.Level) {
|
|
|
|
var logs string
|
|
|
|
for _, entry := range hook.AllEntries() {
|
|
|
|
logs = fmt.Sprintf("%s\nlevel=%s msg=\"%s\"", logs, entry.Level, entry.Message)
|
|
|
|
if entry.Level == level && strings.Contains(entry.Message, message) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Errorf("Expected log to contain level=%s and msg=\"%s\" inside log entries: %s", level, message, logs)
|
|
|
|
}
|