Browse Source

rename p2p/discovery/routing package, run go mod tidy

pull/1291/head
Marten Seemann 3 years ago
parent
commit
d5f4910af7
  1. 1
      go.mod
  2. 27
      p2p/discovery/backoff/backoffcache_test.go
  3. 27
      p2p/discovery/mocks/mocks.go
  4. 7
      p2p/discovery/routing/routing.go
  5. 14
      p2p/discovery/routing/routing_test.go

1
go.mod

@ -43,6 +43,7 @@ require (
github.com/libp2p/zeroconf/v2 v2.1.1 github.com/libp2p/zeroconf/v2 v2.1.1
github.com/multiformats/go-multiaddr v0.5.0 github.com/multiformats/go-multiaddr v0.5.0
github.com/multiformats/go-multiaddr-dns v0.3.1 github.com/multiformats/go-multiaddr-dns v0.3.1
github.com/multiformats/go-multihash v0.0.15
github.com/multiformats/go-multistream v0.2.2 github.com/multiformats/go-multistream v0.2.2
github.com/multiformats/go-varint v0.0.6 github.com/multiformats/go-varint v0.0.6
github.com/prometheus/common v0.30.0 // indirect github.com/prometheus/common v0.30.0 // indirect

27
p2p/discovery/backoff/backoffcache_test.go

@ -7,9 +7,12 @@ import (
"testing" "testing"
"time" "time"
bhost "github.com/libp2p/go-libp2p-blankhost" "github.com/libp2p/go-libp2p/p2p/discovery/mocks"
"github.com/libp2p/go-libp2p-core/discovery" "github.com/libp2p/go-libp2p-core/discovery"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
bhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing" swarmt "github.com/libp2p/go-libp2p-swarm/testing"
) )
@ -66,14 +69,14 @@ func TestBackoffDiscoverySingleBackoff(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
discServer := newDiscoveryServer() discServer := mocks.NewDiscoveryServer()
h1 := bhost.NewBlankHost(swarmt.GenSwarm(t)) h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h1.Close() defer h1.Close()
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t)) h2 := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h2.Close() defer h2.Close()
d1 := &mockDiscoveryClient{h1, discServer} d1 := mocks.NewDiscoveryClient(h1, discServer)
d2 := &mockDiscoveryClient{h2, discServer} d2 := mocks.NewDiscoveryClient(h2, discServer)
bkf := NewExponentialBackoff(time.Millisecond*100, time.Second*10, NoJitter, bkf := NewExponentialBackoff(time.Millisecond*100, time.Second*10, NoJitter,
time.Millisecond*100, 2.5, 0, rand.NewSource(0)) time.Millisecond*100, 2.5, 0, rand.NewSource(0))
@ -108,14 +111,14 @@ func TestBackoffDiscoveryMultipleBackoff(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
discServer := newDiscoveryServer() discServer := mocks.NewDiscoveryServer()
h1 := bhost.NewBlankHost(swarmt.GenSwarm(t)) h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h1.Close() defer h1.Close()
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t)) h2 := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h2.Close() defer h2.Close()
d1 := &mockDiscoveryClient{h1, discServer} d1 := mocks.NewDiscoveryClient(h1, discServer)
d2 := &mockDiscoveryClient{h2, discServer} d2 := mocks.NewDiscoveryClient(h2, discServer)
// Startup delay is 0ms. First backoff after finding data is 100ms, second backoff is 250ms. // Startup delay is 0ms. First backoff after finding data is 100ms, second backoff is 250ms.
bkf := NewExponentialBackoff( bkf := NewExponentialBackoff(
@ -164,7 +167,7 @@ func TestBackoffDiscoverySimultaneousQuery(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
discServer := newDiscoveryServer() discServer := mocks.NewDiscoveryServer()
// Testing with n larger than most internal buffer sizes (32) // Testing with n larger than most internal buffer sizes (32)
n := 40 n := 40
@ -173,7 +176,7 @@ func TestBackoffDiscoverySimultaneousQuery(t *testing.T) {
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
h := bhost.NewBlankHost(swarmt.GenSwarm(t)) h := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h.Close() defer h.Close()
advertisers[i] = &mockDiscoveryClient{h, discServer} advertisers[i] = mocks.NewDiscoveryClient(h, discServer)
} }
d1 := &delayedDiscovery{advertisers[0], time.Millisecond * 10} d1 := &delayedDiscovery{advertisers[0], time.Millisecond * 10}
@ -223,7 +226,7 @@ func TestBackoffDiscoveryCacheCapacity(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
discServer := newDiscoveryServer() discServer := mocks.NewDiscoveryServer()
// Testing with n larger than most internal buffer sizes (32) // Testing with n larger than most internal buffer sizes (32)
n := 40 n := 40
@ -232,11 +235,11 @@ func TestBackoffDiscoveryCacheCapacity(t *testing.T) {
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
h := bhost.NewBlankHost(swarmt.GenSwarm(t)) h := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h.Close() defer h.Close()
advertisers[i] = &mockDiscoveryClient{h, discServer} advertisers[i] = mocks.NewDiscoveryClient(h, discServer)
} }
h1 := bhost.NewBlankHost(swarmt.GenSwarm(t)) h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
d1 := &mockDiscoveryClient{h1, discServer} d1 := mocks.NewDiscoveryClient(h1, discServer)
const discoveryInterval = time.Millisecond * 100 const discoveryInterval = time.Millisecond * 100

27
p2p/discovery/backoff/mocks_test.go → p2p/discovery/mocks/mocks.go

@ -1,4 +1,4 @@
package backoff package mocks
import ( import (
"context" "context"
@ -10,7 +10,7 @@ import (
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
) )
type mockDiscoveryServer struct { type MockDiscoveryServer struct {
mx sync.Mutex mx sync.Mutex
db map[string]map[peer.ID]*discoveryRegistration db map[string]map[peer.ID]*discoveryRegistration
} }
@ -20,13 +20,13 @@ type discoveryRegistration struct {
expiration time.Time expiration time.Time
} }
func newDiscoveryServer() *mockDiscoveryServer { func NewDiscoveryServer() *MockDiscoveryServer {
return &mockDiscoveryServer{ return &MockDiscoveryServer{
db: make(map[string]map[peer.ID]*discoveryRegistration), db: make(map[string]map[peer.ID]*discoveryRegistration),
} }
} }
func (s *mockDiscoveryServer) Advertise(ns string, info peer.AddrInfo, ttl time.Duration) (time.Duration, error) { func (s *MockDiscoveryServer) Advertise(ns string, info peer.AddrInfo, ttl time.Duration) (time.Duration, error) {
s.mx.Lock() s.mx.Lock()
defer s.mx.Unlock() defer s.mx.Unlock()
@ -39,7 +39,7 @@ func (s *mockDiscoveryServer) Advertise(ns string, info peer.AddrInfo, ttl time.
return ttl, nil return ttl, nil
} }
func (s *mockDiscoveryServer) FindPeers(ns string, limit int) (<-chan peer.AddrInfo, error) { func (s *MockDiscoveryServer) FindPeers(ns string, limit int) (<-chan peer.AddrInfo, error) {
s.mx.Lock() s.mx.Lock()
defer s.mx.Unlock() defer s.mx.Unlock()
@ -75,12 +75,19 @@ func (s *mockDiscoveryServer) FindPeers(ns string, limit int) (<-chan peer.AddrI
return ch, nil return ch, nil
} }
type mockDiscoveryClient struct { type MockDiscoveryClient struct {
host host.Host host host.Host
server *mockDiscoveryServer server *MockDiscoveryServer
} }
func (d *mockDiscoveryClient) Advertise(ctx context.Context, ns string, opts ...discovery.Option) (time.Duration, error) { func NewDiscoveryClient(h host.Host, server *MockDiscoveryServer) *MockDiscoveryClient {
return &MockDiscoveryClient{
host: h,
server: server,
}
}
func (d *MockDiscoveryClient) Advertise(ctx context.Context, ns string, opts ...discovery.Option) (time.Duration, error) {
var options discovery.Options var options discovery.Options
err := options.Apply(opts...) err := options.Apply(opts...)
if err != nil { if err != nil {
@ -90,7 +97,7 @@ func (d *mockDiscoveryClient) Advertise(ctx context.Context, ns string, opts ...
return d.server.Advertise(ns, *host.InfoFromHost(d.host), options.Ttl) return d.server.Advertise(ns, *host.InfoFromHost(d.host), options.Ttl)
} }
func (d *mockDiscoveryClient) FindPeers(ctx context.Context, ns string, opts ...discovery.Option) (<-chan peer.AddrInfo, error) { func (d *MockDiscoveryClient) FindPeers(ctx context.Context, ns string, opts ...discovery.Option) (<-chan peer.AddrInfo, error) {
var options discovery.Options var options discovery.Options
err := options.Apply(opts...) err := options.Apply(opts...)
if err != nil { if err != nil {

7
p2p/discovery/routing/routing.go

@ -1,15 +1,14 @@
package discovery package routing
import ( import (
"context" "context"
"github.com/libp2p/go-libp2p-core/discovery"
"time" "time"
"github.com/ipfs/go-cid" "github.com/libp2p/go-libp2p-core/discovery"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing" "github.com/libp2p/go-libp2p-core/routing"
"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash" mh "github.com/multiformats/go-multihash"
) )

14
p2p/discovery/routing/routing_test.go

@ -1,4 +1,4 @@
package discovery package routing
import ( import (
"context" "context"
@ -6,6 +6,10 @@ import (
"testing" "testing"
"time" "time"
"github.com/libp2p/go-libp2p/p2p/discovery/mocks"
"github.com/libp2p/go-libp2p/p2p/discovery/util"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
bhost "github.com/libp2p/go-libp2p-blankhost" bhost "github.com/libp2p/go-libp2p-blankhost"
"github.com/libp2p/go-libp2p-core/discovery" "github.com/libp2p/go-libp2p-core/discovery"
@ -90,7 +94,7 @@ func TestRoutingDiscovery(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
pis, err := FindPeers(ctx, d2, "/test", discovery.Limit(20)) pis, err := util.FindPeers(ctx, d2, "/test", discovery.Limit(20))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -112,9 +116,9 @@ func TestDiscoveryRouting(t *testing.T) {
h1 := bhost.NewBlankHost(swarmt.GenSwarm(t)) h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t)) h2 := bhost.NewBlankHost(swarmt.GenSwarm(t))
dserver := newDiscoveryServer() dserver := mocks.NewDiscoveryServer()
d1 := &mockDiscoveryClient{h1, dserver} d1 := mocks.NewDiscoveryClient(h1, dserver)
d2 := &mockDiscoveryClient{h2, dserver} d2 := mocks.NewDiscoveryClient(h2, dserver)
r1 := NewDiscoveryRouting(d1, discovery.TTL(time.Hour)) r1 := NewDiscoveryRouting(d1, discovery.TTL(time.Hour))
r2 := NewDiscoveryRouting(d2, discovery.TTL(time.Hour)) r2 := NewDiscoveryRouting(d2, discovery.TTL(time.Hour))

Loading…
Cancel
Save