github.com/prysmaticlabs/prysm@v1.4.4/shared/messagehandler/messagehandler_test.go (about)

     1  package messagehandler_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	pubsub "github.com/libp2p/go-libp2p-pubsub"
     8  	"github.com/prysmaticlabs/prysm/shared/messagehandler"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    10  	logTest "github.com/sirupsen/logrus/hooks/test"
    11  )
    12  
    13  func TestSafelyHandleMessage(t *testing.T) {
    14  	hook := logTest.NewGlobal()
    15  
    16  	messagehandler.SafelyHandleMessage(context.Background(), func(_ context.Context, _ *pubsub.Message) error {
    17  		panic("bad!")
    18  		return nil
    19  	}, &pubsub.Message{})
    20  
    21  	require.LogsContain(t, hook, "Panicked when handling p2p message!")
    22  }
    23  
    24  func TestSafelyHandleMessage_NoData(t *testing.T) {
    25  	hook := logTest.NewGlobal()
    26  
    27  	messagehandler.SafelyHandleMessage(context.Background(), func(_ context.Context, _ *pubsub.Message) error {
    28  		panic("bad!")
    29  		return nil
    30  	}, nil)
    31  
    32  	entry := hook.LastEntry()
    33  	if entry.Data["msg"] != "message contains no data" {
    34  		t.Errorf("Message logged was not what was expected: %s", entry.Data["msg"])
    35  	}
    36  }