mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-09 23:42:20 +08:00
fix(core, peer) helpers to testutil, err handling
License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
This commit is contained in:
@ -267,7 +267,8 @@ func initConnections(ctx context.Context, cfg *config.Config, pstore peer.Peerst
|
||||
// setup peer
|
||||
id, err := peer.DecodePrettyID(p.PeerID)
|
||||
if err != nil {
|
||||
// return err
|
||||
log.Criticalf("Bootstrapping error: %v", err)
|
||||
continue
|
||||
}
|
||||
npeer, err := pstore.FindOrCreate(id)
|
||||
if err != nil {
|
||||
|
@ -7,9 +7,9 @@ import (
|
||||
|
||||
ci "github.com/jbenet/go-ipfs/crypto"
|
||||
"github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
"github.com/jbenet/go-ipfs/util"
|
||||
"github.com/jbenet/go-ipfs/util/pipes"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
)
|
||||
|
||||
func getPeer(tb testing.TB) peer.Peer {
|
||||
@ -18,7 +18,7 @@ func getPeer(tb testing.TB) peer.Peer {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
|
||||
p, err := mockpeer.WithKeyPair(privk, pubk)
|
||||
p, err := testutil.NewPeerWithKeyPair(privk, pubk)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
|
@ -10,8 +10,8 @@ import (
|
||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||
blocksutil "github.com/jbenet/go-ipfs/blocks/blocksutil"
|
||||
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
mock "github.com/jbenet/go-ipfs/routing/mock"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
)
|
||||
|
||||
func TestClose(t *testing.T) {
|
||||
@ -53,7 +53,7 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
|
||||
g := NewSessionGenerator(net, rs)
|
||||
|
||||
block := blocks.NewBlock([]byte("block"))
|
||||
rs.Announce(mockpeer.WithIDString("testing"), block.Key()) // but not on network
|
||||
rs.Announce(testutil.NewPeerWithIDString("testing"), block.Key()) // but not on network
|
||||
|
||||
solo := g.Next()
|
||||
|
||||
|
@ -6,8 +6,8 @@ import (
|
||||
|
||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||
pb "github.com/jbenet/go-ipfs/exchange/bitswap/message/internal/pb"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
)
|
||||
|
||||
func TestAppendWanted(t *testing.T) {
|
||||
@ -89,7 +89,7 @@ func TestCopyProtoByValue(t *testing.T) {
|
||||
|
||||
func TestToNetMethodSetsPeer(t *testing.T) {
|
||||
m := New()
|
||||
p := mockpeer.WithIDString("X")
|
||||
p := testutil.NewPeerWithIDString("X")
|
||||
netmsg, err := m.ToNet(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@ -107,7 +107,7 @@ func TestToNetFromNetPreservesWantList(t *testing.T) {
|
||||
original.AddWanted(u.Key("T"))
|
||||
original.AddWanted(u.Key("F"))
|
||||
|
||||
p := mockpeer.WithIDString("X")
|
||||
p := testutil.NewPeerWithIDString("X")
|
||||
netmsg, err := original.ToNet(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@ -138,7 +138,7 @@ func TestToAndFromNetMessage(t *testing.T) {
|
||||
original.AddBlock(blocks.NewBlock([]byte("F")))
|
||||
original.AddBlock(blocks.NewBlock([]byte("M")))
|
||||
|
||||
p := mockpeer.WithIDString("X")
|
||||
p := testutil.NewPeerWithIDString("X")
|
||||
netmsg, err := original.ToNet(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||
message "github.com/jbenet/go-ipfs/exchange/bitswap/message"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
)
|
||||
|
||||
type peerAndStrategist struct {
|
||||
@ -17,7 +17,7 @@ type peerAndStrategist struct {
|
||||
|
||||
func newPeerAndStrategist(idStr string) peerAndStrategist {
|
||||
return peerAndStrategist{
|
||||
Peer: mockpeer.WithIDString(idStr),
|
||||
Peer: testutil.NewPeerWithIDString(idStr),
|
||||
Strategy: New(true),
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
|
||||
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
)
|
||||
|
||||
func TestSendRequestToCooperativePeer(t *testing.T) {
|
||||
@ -19,8 +19,8 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
|
||||
|
||||
t.Log("Get two network adapters")
|
||||
|
||||
initiator := net.Adapter(mockpeer.WithIDString("initiator"))
|
||||
recipient := net.Adapter(mockpeer.WithID(idOfRecipient))
|
||||
initiator := net.Adapter(testutil.NewPeerWithIDString("initiator"))
|
||||
recipient := net.Adapter(testutil.NewPeerWithID(idOfRecipient))
|
||||
|
||||
expectedStr := "response from recipient"
|
||||
recipient.SetDelegate(lambda(func(
|
||||
@ -44,7 +44,7 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
|
||||
message := bsmsg.New()
|
||||
message.AddBlock(blocks.NewBlock([]byte("data")))
|
||||
response, err := initiator.SendRequest(
|
||||
context.Background(), mockpeer.WithID(idOfRecipient), message)
|
||||
context.Background(), testutil.NewPeerWithID(idOfRecipient), message)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -62,8 +62,8 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
|
||||
func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
|
||||
net := VirtualNetwork()
|
||||
idOfResponder := []byte("responder")
|
||||
waiter := net.Adapter(mockpeer.WithIDString("waiter"))
|
||||
responder := net.Adapter(mockpeer.WithID(idOfResponder))
|
||||
waiter := net.Adapter(testutil.NewPeerWithIDString("waiter"))
|
||||
responder := net.Adapter(testutil.NewPeerWithID(idOfResponder))
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
@ -108,7 +108,7 @@ func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
|
||||
messageSentAsync := bsmsg.New()
|
||||
messageSentAsync.AddBlock(blocks.NewBlock([]byte("data")))
|
||||
errSending := waiter.SendMessage(
|
||||
context.Background(), mockpeer.WithID(idOfResponder), messageSentAsync)
|
||||
context.Background(), testutil.NewPeerWithID(idOfResponder), messageSentAsync)
|
||||
if errSending != nil {
|
||||
t.Fatal(errSending)
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
"github.com/jbenet/go-ipfs/importer/chunk"
|
||||
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
"github.com/jbenet/go-ipfs/util/testutil"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
)
|
||||
|
||||
// NOTE:
|
||||
|
@ -5,13 +5,13 @@ import (
|
||||
|
||||
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||
ci "github.com/jbenet/go-ipfs/crypto"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
mock "github.com/jbenet/go-ipfs/routing/mock"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
)
|
||||
|
||||
func TestRoutingResolve(t *testing.T) {
|
||||
local := mockpeer.WithIDString("testID")
|
||||
local := testutil.NewPeerWithIDString("testID")
|
||||
lds := ds.NewMapDatastore()
|
||||
d := mock.NewMockRouter(local, lds)
|
||||
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
|
||||
ci "github.com/jbenet/go-ipfs/crypto"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
||||
@ -22,7 +22,7 @@ func setupPeer(addr string) (peer.Peer, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p, err := mockpeer.WithKeyPair(sk, pk)
|
||||
p, err := testutil.NewPeerWithKeyPair(sk, pk)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
msg "github.com/jbenet/go-ipfs/net/message"
|
||||
pb "github.com/jbenet/go-ipfs/net/mux/internal/pb"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
)
|
||||
@ -31,7 +31,7 @@ func newPeer(t *testing.T, id string) peer.Peer {
|
||||
return nil
|
||||
}
|
||||
|
||||
return mockpeer.WithID(peer.ID(mh))
|
||||
return testutil.NewPeerWithID(peer.ID(mh))
|
||||
}
|
||||
|
||||
func testMsg(t *testing.T, m msg.NetMessage, data []byte) {
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
|
||||
msg "github.com/jbenet/go-ipfs/net/message"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
||||
@ -33,7 +33,7 @@ func newPeer(t *testing.T, id string) peer.Peer {
|
||||
return nil
|
||||
}
|
||||
|
||||
return mockpeer.WithID(peer.ID(mh))
|
||||
return testutil.NewPeerWithID(peer.ID(mh))
|
||||
}
|
||||
|
||||
func TestServiceHandler(t *testing.T) {
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
"github.com/jbenet/go-ipfs/util/testutil"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
)
|
||||
@ -29,7 +29,7 @@ func TestSimultOpen(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
connect := func(s *Swarm, dst peer.Peer) {
|
||||
// copy for other peer
|
||||
cp := mockpeer.WithID(dst.ID())
|
||||
cp := testutil.NewPeerWithID(dst.ID())
|
||||
cp.AddAddress(dst.Addresses()[0])
|
||||
|
||||
if _, err := s.Dial(cp); err != nil {
|
||||
|
@ -9,8 +9,8 @@ import (
|
||||
ci "github.com/jbenet/go-ipfs/crypto"
|
||||
msg "github.com/jbenet/go-ipfs/net/message"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
||||
@ -44,7 +44,7 @@ func setupPeer(t *testing.T, addr string) peer.Peer {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
p, err := mockpeer.WithKeyPair(sk, pk)
|
||||
p, err := testutil.NewPeerWithKeyPair(sk, pk)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -7,14 +7,14 @@ import (
|
||||
"time"
|
||||
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
)
|
||||
|
||||
func newPeer(id string) peer.Peer {
|
||||
return mockpeer.WithIDString(id)
|
||||
return testutil.NewPeerWithIDString(id)
|
||||
}
|
||||
|
||||
func TestQueue(t *testing.T) {
|
||||
@ -70,7 +70,7 @@ func TestQueue(t *testing.T) {
|
||||
func newPeerTime(t time.Time) peer.Peer {
|
||||
s := fmt.Sprintf("hmmm time: %v", t)
|
||||
h := u.Hash([]byte(s))
|
||||
return mockpeer.WithID(peer.ID(h))
|
||||
return testutil.NewPeerWithID(peer.ID(h))
|
||||
}
|
||||
|
||||
func TestSyncQueue(t *testing.T) {
|
||||
|
@ -14,8 +14,8 @@ import (
|
||||
mux "github.com/jbenet/go-ipfs/net/mux"
|
||||
netservice "github.com/jbenet/go-ipfs/net/service"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
|
||||
"fmt"
|
||||
"time"
|
||||
@ -69,7 +69,7 @@ func makePeer(addr ma.Multiaddr) peer.Peer {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
p, err := mockpeer.WithKeyPair(sk, pk)
|
||||
p, err := testutil.NewPeerWithKeyPair(sk, pk)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -7,15 +7,14 @@ import (
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
|
||||
|
||||
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||
msg "github.com/jbenet/go-ipfs/net/message"
|
||||
mux "github.com/jbenet/go-ipfs/net/mux"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
"github.com/jbenet/go-ipfs/routing"
|
||||
routing "github.com/jbenet/go-ipfs/routing"
|
||||
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
|
||||
"sync"
|
||||
"time"
|
||||
@ -211,7 +210,7 @@ func TestGetFailures(t *testing.T) {
|
||||
func _randPeer() peer.Peer {
|
||||
id := make(peer.ID, 16)
|
||||
crand.Read(id)
|
||||
p := mockpeer.WithID(id)
|
||||
p := testutil.NewPeerWithID(id)
|
||||
return p
|
||||
}
|
||||
|
||||
|
@ -4,8 +4,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
)
|
||||
@ -15,7 +15,7 @@ func TestProviderManager(t *testing.T) {
|
||||
mid := peer.ID("testing")
|
||||
p := NewProviderManager(ctx, mid)
|
||||
a := u.Key("test")
|
||||
p.AddProvider(a, mockpeer.WithIDString("testingprovider"))
|
||||
p.AddProvider(a, testutil.NewPeerWithIDString("testingprovider"))
|
||||
resp := p.GetProviders(ctx, a)
|
||||
if len(resp) != 1 {
|
||||
t.Fatal("Could not retrieve provider.")
|
||||
|
@ -6,8 +6,8 @@ import (
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
"github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
)
|
||||
|
||||
func TestKeyNotFound(t *testing.T) {
|
||||
@ -21,7 +21,7 @@ func TestKeyNotFound(t *testing.T) {
|
||||
|
||||
func TestSetAndGet(t *testing.T) {
|
||||
pid := peer.ID([]byte("the peer id"))
|
||||
p := mockpeer.WithID(pid)
|
||||
p := testutil.NewPeerWithID(pid)
|
||||
k := u.Key("42")
|
||||
rs := VirtualRoutingServer()
|
||||
err := rs.Announce(p, k)
|
||||
@ -41,7 +41,7 @@ func TestSetAndGet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestClientFindProviders(t *testing.T) {
|
||||
peer := mockpeer.WithIDString("42")
|
||||
peer := testutil.NewPeerWithIDString("42")
|
||||
rs := VirtualRoutingServer()
|
||||
client := rs.Client(peer)
|
||||
|
||||
@ -80,7 +80,7 @@ func TestClientOverMax(t *testing.T) {
|
||||
k := u.Key("hello")
|
||||
numProvidersForHelloKey := 100
|
||||
for i := 0; i < numProvidersForHelloKey; i++ {
|
||||
peer := mockpeer.WithIDString(string(i))
|
||||
peer := testutil.NewPeerWithIDString(string(i))
|
||||
err := rs.Announce(peer, k)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@ -93,7 +93,7 @@ func TestClientOverMax(t *testing.T) {
|
||||
}
|
||||
|
||||
max := 10
|
||||
peer := mockpeer.WithIDString("TODO")
|
||||
peer := testutil.NewPeerWithIDString("TODO")
|
||||
client := rs.Client(peer)
|
||||
|
||||
providersFromClient := client.FindProvidersAsync(context.Background(), k, max)
|
||||
@ -115,7 +115,7 @@ func TestCanceledContext(t *testing.T) {
|
||||
i := 0
|
||||
go func() { // infinite stream
|
||||
for {
|
||||
peer := mockpeer.WithIDString(string(i))
|
||||
peer := testutil.NewPeerWithIDString(string(i))
|
||||
err := rs.Announce(peer, k)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@ -124,7 +124,7 @@ func TestCanceledContext(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
local := mockpeer.WithIDString("peer id doesn't matter")
|
||||
local := testutil.NewPeerWithIDString("peer id doesn't matter")
|
||||
client := rs.Client(local)
|
||||
|
||||
t.Log("warning: max is finite so this test is non-deterministic")
|
||||
|
@ -28,6 +28,5 @@ func RandPeer() peer.Peer {
|
||||
id := make([]byte, 16)
|
||||
crand.Read(id)
|
||||
mhid := u.Hash(id)
|
||||
ps := peer.NewPeerstore()
|
||||
return ps.WithID(peer.ID(mhid))
|
||||
return NewPeerWithID(peer.ID(mhid))
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package mockpeer
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"github.com/jbenet/go-ipfs/peer"
|
||||
@ -6,14 +6,14 @@ import (
|
||||
ic "github.com/jbenet/go-ipfs/crypto"
|
||||
)
|
||||
|
||||
func WithKeyPair(sk ic.PrivKey, pk ic.PubKey) (peer.Peer, error) {
|
||||
func NewPeerWithKeyPair(sk ic.PrivKey, pk ic.PubKey) (peer.Peer, error) {
|
||||
return peer.NewPeerstore().WithKeyPair(sk, pk)
|
||||
}
|
||||
|
||||
func WithID(id peer.ID) peer.Peer {
|
||||
func NewPeerWithID(id peer.ID) peer.Peer {
|
||||
return peer.NewPeerstore().WithID(id)
|
||||
}
|
||||
|
||||
func WithIDString(id string) peer.Peer {
|
||||
func NewPeerWithIDString(id string) peer.Peer {
|
||||
return peer.NewPeerstore().WithIDString(id)
|
||||
}
|
Reference in New Issue
Block a user