do not panic if dv5Listener is not inited (#3339)

This commit is contained in:
skillful-alex 2019-08-28 13:59:34 +03:00 committed by Nishant Das
parent 9f2c2f0197
commit 7bb5ac0dde
2 changed files with 11 additions and 4 deletions

View File

@ -7,7 +7,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/discv5"
"github.com/gogo/protobuf/proto"
"github.com/libp2p/go-libp2p"
libp2p "github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/host"
network "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
@ -116,7 +116,9 @@ func (s *Service) Start() {
// Stop the p2p service and terminate all peer connections.
func (s *Service) Stop() error {
s.started = false
s.dv5Listener.Close()
if s.dv5Listener != nil {
s.dv5Listener.Close()
}
return nil
}

View File

@ -9,10 +9,10 @@ import (
"time"
"github.com/ethereum/go-ethereum/p2p/discv5"
"github.com/libp2p/go-libp2p"
libp2p "github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multiaddr"
multiaddr "github.com/multiformats/go-multiaddr"
"github.com/prysmaticlabs/prysm/shared/testutil"
logTest "github.com/sirupsen/logrus/hooks/test"
)
@ -87,6 +87,11 @@ func TestService_Stop_SetsStartedToFalse(t *testing.T) {
}
}
func TestService_Stop_DontPanicIfDv5ListenerIsNotInited(t *testing.T) {
s, _ := NewService(nil)
_ = s.Stop()
}
func TestService_Start_OnlyStartsOnce(t *testing.T) {
hook := logTest.NewGlobal()