mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 10:27:46 +08:00
refactor(peer): create peer through peerstore
for safety! use mockpeer.WithID methods to create peers in tests License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
This commit is contained in:
parent
73a89e161d
commit
d77c4bb5e0
@ -1,10 +1,11 @@
|
||||
package blockstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||
syncds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
||||
"github.com/jbenet/go-ipfs/blocks"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReturnsErrorWhenSizeNegative(t *testing.T) {
|
||||
|
||||
@ -25,12 +25,13 @@ func NewMockNode() (*IpfsNode, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p, err := peer.WithKeyPair(sk, pk)
|
||||
nd.Peerstore = peer.NewPeerstore()
|
||||
|
||||
p, err := nd.Peerstore.WithKeyPair(sk, pk)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nd.Peerstore = peer.NewPeerstore()
|
||||
nd.Identity, err = nd.Peerstore.Add(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -14,9 +14,10 @@ import (
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
bfish "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.crypto/blowfish"
|
||||
"hash"
|
||||
|
||||
bfish "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.crypto/blowfish"
|
||||
|
||||
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
|
||||
|
||||
ci "github.com/jbenet/go-ipfs/crypto"
|
||||
|
||||
@ -7,6 +7,7 @@ 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"
|
||||
)
|
||||
@ -17,7 +18,7 @@ func getPeer(tb testing.TB) peer.Peer {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
|
||||
p, err := peer.WithKeyPair(privk, pubk)
|
||||
p, err := mockpeer.WithKeyPair(privk, pubk)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ 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"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
mock "github.com/jbenet/go-ipfs/routing/mock"
|
||||
)
|
||||
|
||||
@ -53,7 +53,7 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
|
||||
g := NewSessionGenerator(net, rs)
|
||||
|
||||
block := blocks.NewBlock([]byte("block"))
|
||||
rs.Announce(peer.WithIDString("testing"), block.Key()) // but not on network
|
||||
rs.Announce(mockpeer.WithIDString("testing"), block.Key()) // but not on network
|
||||
|
||||
solo := g.Next()
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
|
||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||
pb "github.com/jbenet/go-ipfs/exchange/bitswap/message/internal/pb"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
)
|
||||
|
||||
@ -89,7 +89,7 @@ func TestCopyProtoByValue(t *testing.T) {
|
||||
|
||||
func TestToNetMethodSetsPeer(t *testing.T) {
|
||||
m := New()
|
||||
p := peer.WithIDString("X")
|
||||
p := mockpeer.WithIDString("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 := peer.WithIDString("X")
|
||||
p := mockpeer.WithIDString("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 := peer.WithIDString("X")
|
||||
p := mockpeer.WithIDString("X")
|
||||
netmsg, err := original.ToNet(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@ -7,6 +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"
|
||||
)
|
||||
|
||||
type peerAndStrategist struct {
|
||||
@ -16,7 +17,7 @@ type peerAndStrategist struct {
|
||||
|
||||
func newPeerAndStrategist(idStr string) peerAndStrategist {
|
||||
return peerAndStrategist{
|
||||
Peer: peer.WithIDString(idStr),
|
||||
Peer: mockpeer.WithIDString(idStr),
|
||||
Strategy: New(true),
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,6 +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"
|
||||
)
|
||||
|
||||
func TestSendRequestToCooperativePeer(t *testing.T) {
|
||||
@ -18,8 +19,8 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
|
||||
|
||||
t.Log("Get two network adapters")
|
||||
|
||||
initiator := net.Adapter(peer.WithIDString("initiator"))
|
||||
recipient := net.Adapter(peer.WithID(idOfRecipient))
|
||||
initiator := net.Adapter(mockpeer.WithIDString("initiator"))
|
||||
recipient := net.Adapter(mockpeer.WithID(idOfRecipient))
|
||||
|
||||
expectedStr := "response from recipient"
|
||||
recipient.SetDelegate(lambda(func(
|
||||
@ -43,7 +44,7 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
|
||||
message := bsmsg.New()
|
||||
message.AddBlock(blocks.NewBlock([]byte("data")))
|
||||
response, err := initiator.SendRequest(
|
||||
context.Background(), peer.WithID(idOfRecipient), message)
|
||||
context.Background(), mockpeer.WithID(idOfRecipient), message)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -61,8 +62,8 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
|
||||
func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
|
||||
net := VirtualNetwork()
|
||||
idOfResponder := []byte("responder")
|
||||
waiter := net.Adapter(peer.WithIDString("waiter"))
|
||||
responder := net.Adapter(peer.WithID(idOfResponder))
|
||||
waiter := net.Adapter(mockpeer.WithIDString("waiter"))
|
||||
responder := net.Adapter(mockpeer.WithID(idOfResponder))
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
@ -107,7 +108,7 @@ func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
|
||||
messageSentAsync := bsmsg.New()
|
||||
messageSentAsync.AddBlock(blocks.NewBlock([]byte("data")))
|
||||
errSending := waiter.SendMessage(
|
||||
context.Background(), peer.WithID(idOfResponder), messageSentAsync)
|
||||
context.Background(), mockpeer.WithID(idOfResponder), messageSentAsync)
|
||||
if errSending != nil {
|
||||
t.Fatal(errSending)
|
||||
}
|
||||
|
||||
@ -16,6 +16,7 @@ func NewSessionGenerator(
|
||||
return SessionGenerator{
|
||||
net: net,
|
||||
rs: rs,
|
||||
ps: peer.NewPeerstore(),
|
||||
seq: 0,
|
||||
}
|
||||
}
|
||||
@ -24,11 +25,12 @@ type SessionGenerator struct {
|
||||
seq int
|
||||
net tn.Network
|
||||
rs mock.RoutingServer
|
||||
ps peer.Peerstore
|
||||
}
|
||||
|
||||
func (g *SessionGenerator) Next() Instance {
|
||||
g.seq++
|
||||
return session(g.net, g.rs, []byte(string(g.seq)))
|
||||
return session(g.net, g.rs, g.ps, []byte(string(g.seq)))
|
||||
}
|
||||
|
||||
func (g *SessionGenerator) Instances(n int) []Instance {
|
||||
@ -51,8 +53,8 @@ type Instance struct {
|
||||
// NB: It's easy make mistakes by providing the same peer ID to two different
|
||||
// sessions. To safeguard, use the SessionGenerator to generate sessions. It's
|
||||
// just a much better idea.
|
||||
func session(net tn.Network, rs mock.RoutingServer, id peer.ID) Instance {
|
||||
p := peer.WithID(id)
|
||||
func session(net tn.Network, rs mock.RoutingServer, ps peer.Peerstore, id peer.ID) Instance {
|
||||
p := ps.WithID(id)
|
||||
|
||||
adapter := net.Adapter(p)
|
||||
htc := rs.Client(p)
|
||||
|
||||
@ -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"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
mock "github.com/jbenet/go-ipfs/routing/mock"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
)
|
||||
|
||||
func TestRoutingResolve(t *testing.T) {
|
||||
local := peer.WithIDString("testID")
|
||||
local := mockpeer.WithIDString("testID")
|
||||
lds := ds.NewMapDatastore()
|
||||
d := mock.NewMockRouter(local, lds)
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ import (
|
||||
|
||||
ci "github.com/jbenet/go-ipfs/crypto"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
|
||||
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"
|
||||
@ -21,7 +22,7 @@ func setupPeer(addr string) (peer.Peer, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p, err := peer.WithKeyPair(sk, pk)
|
||||
p, err := mockpeer.WithKeyPair(sk, pk)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -11,6 +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"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
)
|
||||
@ -30,7 +31,7 @@ func newPeer(t *testing.T, id string) peer.Peer {
|
||||
return nil
|
||||
}
|
||||
|
||||
return peer.WithID(peer.ID(mh))
|
||||
return mockpeer.WithID(peer.ID(mh))
|
||||
}
|
||||
|
||||
func testMsg(t *testing.T, m msg.NetMessage, data []byte) {
|
||||
|
||||
@ -7,6 +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"
|
||||
|
||||
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"
|
||||
@ -32,7 +33,7 @@ func newPeer(t *testing.T, id string) peer.Peer {
|
||||
return nil
|
||||
}
|
||||
|
||||
return peer.WithID(peer.ID(mh))
|
||||
return mockpeer.WithID(peer.ID(mh))
|
||||
}
|
||||
|
||||
func TestServiceHandler(t *testing.T) {
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"testing"
|
||||
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
)
|
||||
@ -28,7 +29,7 @@ func TestSimultOpen(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
connect := func(s *Swarm, dst peer.Peer) {
|
||||
// copy for other peer
|
||||
cp := peer.WithID(dst.ID())
|
||||
cp := mockpeer.WithID(dst.ID())
|
||||
cp.AddAddress(dst.Addresses()[0])
|
||||
|
||||
if _, err := s.Dial(cp); err != nil {
|
||||
|
||||
@ -9,6 +9,7 @@ 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"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
@ -43,7 +44,7 @@ func setupPeer(t *testing.T, addr string) peer.Peer {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
p, err := peer.WithKeyPair(sk, pk)
|
||||
p, err := mockpeer.WithKeyPair(sk, pk)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
19
peer/mock/mock.go
Normal file
19
peer/mock/mock.go
Normal file
@ -0,0 +1,19 @@
|
||||
package mockpeer
|
||||
|
||||
import (
|
||||
"github.com/jbenet/go-ipfs/peer"
|
||||
|
||||
ic "github.com/jbenet/go-ipfs/crypto"
|
||||
)
|
||||
|
||||
func WithKeyPair(sk ic.PrivKey, pk ic.PubKey) (peer.Peer, error) {
|
||||
return peer.NewPeerstore().WithKeyPair(sk, pk)
|
||||
}
|
||||
|
||||
func WithID(id peer.ID) peer.Peer {
|
||||
return peer.NewPeerstore().WithID(id)
|
||||
}
|
||||
|
||||
func WithIDString(id string) peer.Peer {
|
||||
return peer.NewPeerstore().WithIDString(id)
|
||||
}
|
||||
31
peer/peer.go
31
peer/peer.go
@ -394,34 +394,3 @@ func (p *peer) SetVersions(agent, protocol string) {
|
||||
p.agentVersion = agent
|
||||
p.protocolVersion = protocol
|
||||
}
|
||||
|
||||
// WithKeyPair returns a Peer object with given keys.
|
||||
func WithKeyPair(sk ic.PrivKey, pk ic.PubKey) (Peer, error) {
|
||||
if sk == nil && pk == nil {
|
||||
return nil, fmt.Errorf("PeerWithKeyPair nil keys")
|
||||
}
|
||||
|
||||
pk2 := sk.GetPublic()
|
||||
if pk == nil {
|
||||
pk = pk2
|
||||
} else if !pk.Equals(pk2) {
|
||||
return nil, fmt.Errorf("key mismatch. pubkey is not privkey's pubkey")
|
||||
}
|
||||
|
||||
pkid, err := IDFromPubKey(pk)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to hash public key: %v", err)
|
||||
}
|
||||
|
||||
return &peer{id: pkid, pubKey: pk, privKey: sk}, nil
|
||||
}
|
||||
|
||||
// WithID constructs a peer with given ID.
|
||||
func WithID(id ID) Peer {
|
||||
return &peer{id: id}
|
||||
}
|
||||
|
||||
// WithIDString constructs a peer with given ID (string).
|
||||
func WithIDString(id string) Peer {
|
||||
return WithID(ID(id))
|
||||
}
|
||||
|
||||
@ -27,7 +27,7 @@ func TestNetAddress(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
p := WithID(ID(mh))
|
||||
p := NewPeerstore().WithID(ID(mh))
|
||||
p.AddAddress(tcp)
|
||||
p.AddAddress(udp)
|
||||
p.AddAddress(tcp)
|
||||
@ -48,7 +48,7 @@ func TestNetAddress(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStringMethodWithSmallId(t *testing.T) {
|
||||
p := WithID([]byte(string(0)))
|
||||
p := NewPeerstore().WithID([]byte(string(0)))
|
||||
p1, ok := p.(*peer)
|
||||
if !ok {
|
||||
t.Fatal("WithID doesn't return a peer")
|
||||
|
||||
@ -3,7 +3,9 @@ package peer
|
||||
import (
|
||||
"sync"
|
||||
|
||||
ic "github.com/jbenet/go-ipfs/crypto"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
errors "github.com/jbenet/go-ipfs/util/debugerror"
|
||||
)
|
||||
|
||||
// Peerstore provides a threadsafe collection for peers.
|
||||
@ -12,6 +14,10 @@ type Peerstore interface {
|
||||
Add(Peer) (Peer, error)
|
||||
Delete(ID) error
|
||||
All() (*Map, error)
|
||||
|
||||
WithKeyPair(sk ic.PrivKey, pk ic.PubKey) (Peer, error)
|
||||
WithID(id ID) Peer
|
||||
WithIDString(id string) Peer
|
||||
}
|
||||
|
||||
type peerstore struct {
|
||||
@ -89,3 +95,34 @@ func (p *peerstore) All() (*Map, error) {
|
||||
}
|
||||
return &ps, nil
|
||||
}
|
||||
|
||||
// WithKeyPair returns a Peer object with given keys.
|
||||
func (ps *peerstore) WithKeyPair(sk ic.PrivKey, pk ic.PubKey) (Peer, error) {
|
||||
if sk == nil && pk == nil {
|
||||
return nil, errors.Errorf("PeerWithKeyPair nil keys")
|
||||
}
|
||||
|
||||
pk2 := sk.GetPublic()
|
||||
if pk == nil {
|
||||
pk = pk2
|
||||
} else if !pk.Equals(pk2) {
|
||||
return nil, errors.Errorf("key mismatch. pubkey is not privkey's pubkey")
|
||||
}
|
||||
|
||||
pkid, err := IDFromPubKey(pk)
|
||||
if err != nil {
|
||||
return nil, errors.Errorf("Failed to hash public key: %v", err)
|
||||
}
|
||||
|
||||
return &peer{id: pkid, pubKey: pk, privKey: sk}, nil
|
||||
}
|
||||
|
||||
// WithID constructs a peer with given ID.
|
||||
func (ps *peerstore) WithID(id ID) Peer {
|
||||
return &peer{id: id}
|
||||
}
|
||||
|
||||
// WithIDString constructs a peer with given ID (string).
|
||||
func (ps *peerstore) WithIDString(id string) Peer {
|
||||
return ps.WithID(ID(id))
|
||||
}
|
||||
|
||||
@ -7,13 +7,13 @@ import (
|
||||
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
||||
)
|
||||
|
||||
func setupPeer(id string, addr string) (Peer, error) {
|
||||
func setupPeer(ps Peerstore, id string, addr string) (Peer, error) {
|
||||
tcp, err := ma.NewMultiaddr(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p := WithIDString(id)
|
||||
p := ps.WithIDString(id)
|
||||
p.AddAddress(tcp)
|
||||
return p, nil
|
||||
}
|
||||
@ -22,8 +22,8 @@ func TestPeerstore(t *testing.T) {
|
||||
|
||||
ps := NewPeerstore()
|
||||
|
||||
p11, _ := setupPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31", "/ip4/127.0.0.1/tcp/1234")
|
||||
p21, _ := setupPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32", "/ip4/127.0.0.1/tcp/2345")
|
||||
p11, _ := setupPeer(ps, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31", "/ip4/127.0.0.1/tcp/1234")
|
||||
p21, _ := setupPeer(ps, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32", "/ip4/127.0.0.1/tcp/2345")
|
||||
// p31, _ := setupPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33", "/ip4/127.0.0.1/tcp/3456")
|
||||
// p41, _ := setupPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a34", "/ip4/127.0.0.1/tcp/4567")
|
||||
|
||||
|
||||
@ -7,13 +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"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
)
|
||||
|
||||
func newPeer(id string) peer.Peer {
|
||||
return peer.WithIDString(id)
|
||||
return mockpeer.WithIDString(id)
|
||||
}
|
||||
|
||||
func TestQueue(t *testing.T) {
|
||||
@ -69,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 peer.WithID(peer.ID(h))
|
||||
return mockpeer.WithID(peer.ID(h))
|
||||
}
|
||||
|
||||
func TestSyncQueue(t *testing.T) {
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
config "github.com/jbenet/go-ipfs/config"
|
||||
util "github.com/jbenet/go-ipfs/util"
|
||||
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
|
||||
config "github.com/jbenet/go-ipfs/config"
|
||||
)
|
||||
|
||||
func ConfigureEventLogger(config config.Logs) error {
|
||||
|
||||
@ -14,6 +14,7 @@ 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"
|
||||
|
||||
"fmt"
|
||||
@ -68,7 +69,7 @@ func makePeer(addr ma.Multiaddr) peer.Peer {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
p, err := peer.WithKeyPair(sk, pk)
|
||||
p, err := mockpeer.WithKeyPair(sk, pk)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@ import (
|
||||
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"
|
||||
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
@ -210,7 +211,7 @@ func TestGetFailures(t *testing.T) {
|
||||
func _randPeer() peer.Peer {
|
||||
id := make(peer.ID, 16)
|
||||
crand.Read(id)
|
||||
p := peer.WithID(id)
|
||||
p := mockpeer.WithID(id)
|
||||
return p
|
||||
}
|
||||
|
||||
|
||||
@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/jbenet/go-ipfs/peer"
|
||||
"github.com/jbenet/go-ipfs/peer/mock"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
@ -14,7 +15,7 @@ func TestProviderManager(t *testing.T) {
|
||||
mid := peer.ID("testing")
|
||||
p := NewProviderManager(ctx, mid)
|
||||
a := u.Key("test")
|
||||
p.AddProvider(a, peer.WithIDString("testingprovider"))
|
||||
p.AddProvider(a, mockpeer.WithIDString("testingprovider"))
|
||||
resp := p.GetProviders(ctx, a)
|
||||
if len(resp) != 1 {
|
||||
t.Fatal("Could not retrieve provider.")
|
||||
|
||||
@ -6,6 +6,7 @@ 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"
|
||||
)
|
||||
|
||||
@ -20,7 +21,7 @@ func TestKeyNotFound(t *testing.T) {
|
||||
|
||||
func TestSetAndGet(t *testing.T) {
|
||||
pid := peer.ID([]byte("the peer id"))
|
||||
p := peer.WithID(pid)
|
||||
p := mockpeer.WithID(pid)
|
||||
k := u.Key("42")
|
||||
rs := VirtualRoutingServer()
|
||||
err := rs.Announce(p, k)
|
||||
@ -40,7 +41,7 @@ func TestSetAndGet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestClientFindProviders(t *testing.T) {
|
||||
peer := peer.WithIDString("42")
|
||||
peer := mockpeer.WithIDString("42")
|
||||
rs := VirtualRoutingServer()
|
||||
client := rs.Client(peer)
|
||||
|
||||
@ -79,7 +80,7 @@ func TestClientOverMax(t *testing.T) {
|
||||
k := u.Key("hello")
|
||||
numProvidersForHelloKey := 100
|
||||
for i := 0; i < numProvidersForHelloKey; i++ {
|
||||
peer := peer.WithIDString(string(i))
|
||||
peer := mockpeer.WithIDString(string(i))
|
||||
err := rs.Announce(peer, k)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@ -92,7 +93,7 @@ func TestClientOverMax(t *testing.T) {
|
||||
}
|
||||
|
||||
max := 10
|
||||
peer := peer.WithIDString("TODO")
|
||||
peer := mockpeer.WithIDString("TODO")
|
||||
client := rs.Client(peer)
|
||||
|
||||
providersFromClient := client.FindProvidersAsync(context.Background(), k, max)
|
||||
@ -114,7 +115,7 @@ func TestCanceledContext(t *testing.T) {
|
||||
i := 0
|
||||
go func() { // infinite stream
|
||||
for {
|
||||
peer := peer.WithIDString(string(i))
|
||||
peer := mockpeer.WithIDString(string(i))
|
||||
err := rs.Announce(peer, k)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@ -123,7 +124,7 @@ func TestCanceledContext(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
local := peer.WithIDString("peer id doesn't matter")
|
||||
local := mockpeer.WithIDString("peer id doesn't matter")
|
||||
client := rs.Client(local)
|
||||
|
||||
t.Log("warning: max is finite so this test is non-deterministic")
|
||||
|
||||
@ -3,8 +3,8 @@ package eventlog
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/jbenet/go-ipfs/util"
|
||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/maybebtc/logrus"
|
||||
"github.com/jbenet/go-ipfs/util"
|
||||
)
|
||||
|
||||
type entry struct {
|
||||
|
||||
@ -28,5 +28,6 @@ func RandPeer() peer.Peer {
|
||||
id := make([]byte, 16)
|
||||
crand.Read(id)
|
||||
mhid := u.Hash(id)
|
||||
return peer.WithID(peer.ID(mhid))
|
||||
ps := peer.NewPeerstore()
|
||||
return ps.WithID(peer.ID(mhid))
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user