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) {
testErr := errors.New("test error")
client, cleanup := newTestServer(t, testServerOptions{
P2P: mock.NewService(func(ctx context.Context, addr ma.Multiaddr) (string, error) {
P2P: &mock.Service{
ConnectFunc: func(ctx context.Context, addr ma.Multiaddr) (string, error) {
if addr.String() == errorUnderlay {
return "", testErr
}
return overlay, nil
}),
},
},
})
defer cleanup()
......
......@@ -327,6 +327,15 @@ func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (overlay strin
s.logger.Infof("peer %q connected", i.Address)
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) {
peerID, found := s.peers.peerID(overlay)
if !found {
......
......@@ -6,7 +6,6 @@ package mock
import (
"context"
"errors"
"fmt"
"io"
"sync"
......@@ -16,19 +15,21 @@ import (
)
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 {
return &Service{connectFunc: connectFunc}
func (s *Service) AddProtocol(spec p2p.ProtocolSpec) error {
return s.AddProtocolFunc(spec)
}
func (s *Service) AddProtocol(_ p2p.ProtocolSpec) error {
return errors.New("not implemented")
func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (overlay string, err error) {
return s.ConnectFunc(ctx, addr)
}
func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (overlay string, err error) {
return s.connectFunc(ctx, addr)
func (s *Service) Disconnect(overlay string) error {
return s.DisconnectFunc(overlay)
}
type Recorder struct {
......
......@@ -15,6 +15,7 @@ import (
type Service interface {
AddProtocol(ProtocolSpec) error
Connect(ctx context.Context, addr ma.Multiaddr) (overlay string, err error)
Disconnect(overlay string) error
}
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