Skip to content

Commit

Permalink
rename p2p/discovery/routing package, run go mod tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jan 9, 2022
1 parent b5056a8 commit d5f4910
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 31 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ require (
github.com/libp2p/zeroconf/v2 v2.1.1
github.com/multiformats/go-multiaddr v0.5.0
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-varint v0.0.6
github.com/prometheus/common v0.30.0 // indirect
Expand Down
27 changes: 15 additions & 12 deletions p2p/discovery/backoff/backoffcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import (
"testing"
"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/peer"

bhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
)

Expand Down Expand Up @@ -66,14 +69,14 @@ func TestBackoffDiscoverySingleBackoff(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

discServer := newDiscoveryServer()
discServer := mocks.NewDiscoveryServer()

h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h1.Close()
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h2.Close()
d1 := &mockDiscoveryClient{h1, discServer}
d2 := &mockDiscoveryClient{h2, discServer}
d1 := mocks.NewDiscoveryClient(h1, discServer)
d2 := mocks.NewDiscoveryClient(h2, discServer)

bkf := NewExponentialBackoff(time.Millisecond*100, time.Second*10, NoJitter,
time.Millisecond*100, 2.5, 0, rand.NewSource(0))
Expand Down Expand Up @@ -108,14 +111,14 @@ func TestBackoffDiscoveryMultipleBackoff(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

discServer := newDiscoveryServer()
discServer := mocks.NewDiscoveryServer()

h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h1.Close()
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h2.Close()
d1 := &mockDiscoveryClient{h1, discServer}
d2 := &mockDiscoveryClient{h2, discServer}
d1 := mocks.NewDiscoveryClient(h1, discServer)
d2 := mocks.NewDiscoveryClient(h2, discServer)

// Startup delay is 0ms. First backoff after finding data is 100ms, second backoff is 250ms.
bkf := NewExponentialBackoff(
Expand Down Expand Up @@ -164,7 +167,7 @@ func TestBackoffDiscoverySimultaneousQuery(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

discServer := newDiscoveryServer()
discServer := mocks.NewDiscoveryServer()

// Testing with n larger than most internal buffer sizes (32)
n := 40
Expand All @@ -173,7 +176,7 @@ func TestBackoffDiscoverySimultaneousQuery(t *testing.T) {
for i := 0; i < n; i++ {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h.Close()
advertisers[i] = &mockDiscoveryClient{h, discServer}
advertisers[i] = mocks.NewDiscoveryClient(h, discServer)
}

d1 := &delayedDiscovery{advertisers[0], time.Millisecond * 10}
Expand Down Expand Up @@ -223,7 +226,7 @@ func TestBackoffDiscoveryCacheCapacity(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

discServer := newDiscoveryServer()
discServer := mocks.NewDiscoveryServer()

// Testing with n larger than most internal buffer sizes (32)
n := 40
Expand All @@ -232,11 +235,11 @@ func TestBackoffDiscoveryCacheCapacity(t *testing.T) {
for i := 0; i < n; i++ {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
defer h.Close()
advertisers[i] = &mockDiscoveryClient{h, discServer}
advertisers[i] = mocks.NewDiscoveryClient(h, discServer)
}

h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
d1 := &mockDiscoveryClient{h1, discServer}
d1 := mocks.NewDiscoveryClient(h1, discServer)

const discoveryInterval = time.Millisecond * 100

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package backoff
package mocks

import (
"context"
Expand All @@ -10,7 +10,7 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
)

type mockDiscoveryServer struct {
type MockDiscoveryServer struct {
mx sync.Mutex
db map[string]map[peer.ID]*discoveryRegistration
}
Expand All @@ -20,13 +20,13 @@ type discoveryRegistration struct {
expiration time.Time
}

func newDiscoveryServer() *mockDiscoveryServer {
return &mockDiscoveryServer{
func NewDiscoveryServer() *MockDiscoveryServer {
return &MockDiscoveryServer{
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()
defer s.mx.Unlock()

Expand All @@ -39,7 +39,7 @@ func (s *mockDiscoveryServer) Advertise(ns string, info peer.AddrInfo, ttl time.
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()
defer s.mx.Unlock()

Expand Down Expand Up @@ -75,12 +75,19 @@ func (s *mockDiscoveryServer) FindPeers(ns string, limit int) (<-chan peer.AddrI
return ch, nil
}

type mockDiscoveryClient struct {
type MockDiscoveryClient struct {
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
err := options.Apply(opts...)
if err != nil {
Expand All @@ -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)
}

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
err := options.Apply(opts...)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions p2p/discovery/routing/routing.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package discovery
package routing

import (
"context"
"github.com/libp2p/go-libp2p-core/discovery"
"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/routing"

"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
)

Expand Down
14 changes: 9 additions & 5 deletions p2p/discovery/routing/routing_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package discovery
package routing

import (
"context"
"sync"
"testing"
"time"

"github.com/libp2p/go-libp2p/p2p/discovery/mocks"

"github.com/libp2p/go-libp2p/p2p/discovery/util"

"github.com/ipfs/go-cid"
bhost "github.com/libp2p/go-libp2p-blankhost"
"github.com/libp2p/go-libp2p-core/discovery"
Expand Down Expand Up @@ -90,7 +94,7 @@ func TestRoutingDiscovery(t *testing.T) {
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 {
t.Fatal(err)
}
Expand All @@ -112,9 +116,9 @@ func TestDiscoveryRouting(t *testing.T) {
h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
h2 := bhost.NewBlankHost(swarmt.GenSwarm(t))

dserver := newDiscoveryServer()
d1 := &mockDiscoveryClient{h1, dserver}
d2 := &mockDiscoveryClient{h2, dserver}
dserver := mocks.NewDiscoveryServer()
d1 := mocks.NewDiscoveryClient(h1, dserver)
d2 := mocks.NewDiscoveryClient(h2, dserver)

r1 := NewDiscoveryRouting(d1, discovery.TTL(time.Hour))
r2 := NewDiscoveryRouting(d2, discovery.TTL(time.Hour))
Expand Down

0 comments on commit d5f4910

Please sign in to comment.