Commit 3839f3fa authored by Janos Guljas's avatar Janos Guljas

add p2p Disconnect method

parent 7eb14f1e
...@@ -24,12 +24,14 @@ func TestConnect(t *testing.T) { ...@@ -24,12 +24,14 @@ func TestConnect(t *testing.T) {
testErr := errors.New("test error") testErr := errors.New("test error")
client, cleanup := newTestServer(t, testServerOptions{ client, cleanup := newTestServer(t, testServerOptions{
P2P: mock.NewService(func(ctx context.Context, addr ma.Multiaddr) (string, error) { P2P: &mock.Service{
if addr.String() == errorUnderlay { ConnectFunc: func(ctx context.Context, addr ma.Multiaddr) (string, error) {
return "", testErr if addr.String() == errorUnderlay {
} return "", testErr
return overlay, nil }
}), return overlay, nil
},
},
}) })
defer cleanup() defer cleanup()
......
...@@ -327,6 +327,15 @@ func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (overlay strin ...@@ -327,6 +327,15 @@ func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (overlay strin
s.logger.Infof("peer %q connected", i.Address) s.logger.Infof("peer %q connected", i.Address)
return i.Address, nil return i.Address, nil
} }
func (s *Service) Disconnect(overlay string) error {
peerID, found := s.peers.peerID(overlay)
if !found {
return p2p.ErrPeerNotFound
}
return s.host.Network().ClosePeer(peerID)
}
func (s *Service) NewStream(ctx context.Context, overlay, protocolName, streamName, version string) (p2p.Stream, error) { func (s *Service) NewStream(ctx context.Context, overlay, protocolName, streamName, version string) (p2p.Stream, error) {
peerID, found := s.peers.peerID(overlay) peerID, found := s.peers.peerID(overlay)
if !found { if !found {
......
...@@ -6,7 +6,6 @@ package mock ...@@ -6,7 +6,6 @@ package mock
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"sync" "sync"
...@@ -16,19 +15,21 @@ import ( ...@@ -16,19 +15,21 @@ import (
) )
type Service struct { type Service struct {
connectFunc func(ctx context.Context, addr ma.Multiaddr) (overlay string, err error) AddProtocolFunc func(p2p.ProtocolSpec) error
ConnectFunc func(ctx context.Context, addr ma.Multiaddr) (overlay string, err error)
DisconnectFunc func(overlay string) error
} }
func NewService(connectFunc func(ctx context.Context, addr ma.Multiaddr) (overlay string, err error)) *Service { func (s *Service) AddProtocol(spec p2p.ProtocolSpec) error {
return &Service{connectFunc: connectFunc} return s.AddProtocolFunc(spec)
} }
func (s *Service) AddProtocol(_ p2p.ProtocolSpec) error { func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (overlay string, err error) {
return errors.New("not implemented") return s.ConnectFunc(ctx, addr)
} }
func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (overlay string, err error) { func (s *Service) Disconnect(overlay string) error {
return s.connectFunc(ctx, addr) return s.DisconnectFunc(overlay)
} }
type Recorder struct { type Recorder struct {
......
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
type Service interface { type Service interface {
AddProtocol(ProtocolSpec) error AddProtocol(ProtocolSpec) error
Connect(ctx context.Context, addr ma.Multiaddr) (overlay string, err error) Connect(ctx context.Context, addr ma.Multiaddr) (overlay string, err error)
Disconnect(overlay string) error
} }
type Streamer interface { type Streamer interface {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment