2019-10-04 22:46:49 +00:00
|
|
|
package messagehandler_test
|
2019-03-10 22:53:28 +00:00
|
|
|
|
|
|
|
import (
|
2019-03-17 02:56:05 +00:00
|
|
|
"context"
|
2019-03-10 22:53:28 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-05-17 18:32:04 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/runtime/messagehandler"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
2019-03-10 22:53:28 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSafelyHandleMessage(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
2021-05-17 18:32:04 +00:00
|
|
|
messagehandler.SafelyHandleMessage(context.Background(), func(_ context.Context, _ *pubsub.Message) error {
|
2019-03-10 22:53:28 +00:00
|
|
|
panic("bad!")
|
2019-03-17 02:56:05 +00:00
|
|
|
return nil
|
2021-05-17 18:32:04 +00:00
|
|
|
}, &pubsub.Message{})
|
2019-03-10 22:53:28 +00:00
|
|
|
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, "Panicked when handling p2p message!")
|
2019-03-10 22:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSafelyHandleMessage_NoData(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
2021-05-17 18:32:04 +00:00
|
|
|
messagehandler.SafelyHandleMessage(context.Background(), func(_ context.Context, _ *pubsub.Message) error {
|
2019-03-10 22:53:28 +00:00
|
|
|
panic("bad!")
|
2019-03-17 02:56:05 +00:00
|
|
|
return nil
|
2019-03-10 22:53:28 +00:00
|
|
|
}, nil)
|
|
|
|
|
|
|
|
entry := hook.LastEntry()
|
|
|
|
if entry.Data["msg"] != "message contains no data" {
|
|
|
|
t.Errorf("Message logged was not what was expected: %s", entry.Data["msg"])
|
|
|
|
}
|
|
|
|
}
|