Commit f4229470 authored by Steven Allen's avatar Steven Allen
Browse files

mocknet: use peer ID in peer address

RandLocalTCPAddress is mostly useful when we *actually* want to listen on a real
address. Unfortunately, when running a bunch of tests, we can actually run
out.

With this change, a collision means we have a duplicate peer ID so yeah...

fixes #473
parent 3e2dc092
...@@ -3,6 +3,7 @@ package mocknet ...@@ -3,6 +3,7 @@ package mocknet
import ( import (
"context" "context"
"fmt" "fmt"
"net"
"sort" "sort"
"sync" "sync"
...@@ -17,10 +18,13 @@ import ( ...@@ -17,10 +18,13 @@ import (
peer "github.com/libp2p/go-libp2p-peer" peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore" pstore "github.com/libp2p/go-libp2p-peerstore"
pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem" pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem"
testutil "github.com/libp2p/go-testutil"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
) )
// IP6 range that gets blackholed (in case our traffic ever makes it out onto
// the internet).
var blackholeIP6 = net.ParseIP("100::")
// mocknet implements mocknet.Mocknet // mocknet implements mocknet.Mocknet
type mocknet struct { type mocknet struct {
nets map[peer.ID]*peernet nets map[peer.ID]*peernet
...@@ -54,8 +58,20 @@ func (mn *mocknet) GenPeer() (host.Host, error) { ...@@ -54,8 +58,20 @@ func (mn *mocknet) GenPeer() (host.Host, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
id, err := peer.IDFromPrivateKey(sk)
a := testutil.RandLocalTCPAddress() if err != nil {
return nil, err
}
suffix := id
if len(id) > 8 {
suffix = id[len(id)-8:]
}
ip := append(net.IP{}, blackholeIP6...)
copy(ip[net.IPv6len-len(suffix):], suffix)
a, err := ma.NewMultiaddr(fmt.Sprintf("/ip6/%s/tcp/4242", ip))
if err != nil {
return nil, fmt.Errorf("failed to create test multiaddr: %s", err)
}
h, err := mn.AddPeer(sk, a) h, err := mn.AddPeer(sk, a)
if err != nil { if err != nil {
......
...@@ -582,6 +582,14 @@ func TestLimitedStreams(t *testing.T) { ...@@ -582,6 +582,14 @@ func TestLimitedStreams(t *testing.T) {
t.Fatal("Expected 2ish seconds but got ", time.Since(before)) t.Fatal("Expected 2ish seconds but got ", time.Since(before))
} }
} }
func TestFuzzManyPeers(t *testing.T) {
for i := 0; i < 50000; i++ {
_, err := FullMeshConnected(context.Background(), 2)
if err != nil {
t.Fatal(err)
}
}
}
func TestStreamsWithLatency(t *testing.T) { func TestStreamsWithLatency(t *testing.T) {
latency := time.Millisecond * 500 latency := time.Millisecond * 500
......
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