Commit 8bff08bd authored by Brian Tiger Chow's avatar Brian Tiger Chow

refactor(peer): create peer through peerstore

for safety!

use mockpeer.WithID methods to create peers in tests

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent a48e70f9
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
blocks "github.com/jbenet/go-ipfs/blocks" blocks "github.com/jbenet/go-ipfs/blocks"
blocksutil "github.com/jbenet/go-ipfs/blocks/blocksutil" blocksutil "github.com/jbenet/go-ipfs/blocks/blocksutil"
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet" 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" mock "github.com/jbenet/go-ipfs/routing/mock"
) )
...@@ -53,7 +53,7 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) { ...@@ -53,7 +53,7 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
g := NewSessionGenerator(net, rs) g := NewSessionGenerator(net, rs)
block := blocks.NewBlock([]byte("block")) 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() solo := g.Next()
......
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
blocks "github.com/jbenet/go-ipfs/blocks" blocks "github.com/jbenet/go-ipfs/blocks"
pb "github.com/jbenet/go-ipfs/exchange/bitswap/message/internal/pb" 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" u "github.com/jbenet/go-ipfs/util"
) )
...@@ -89,7 +89,7 @@ func TestCopyProtoByValue(t *testing.T) { ...@@ -89,7 +89,7 @@ func TestCopyProtoByValue(t *testing.T) {
func TestToNetMethodSetsPeer(t *testing.T) { func TestToNetMethodSetsPeer(t *testing.T) {
m := New() m := New()
p := peer.WithIDString("X") p := mockpeer.WithIDString("X")
netmsg, err := m.ToNet(p) netmsg, err := m.ToNet(p)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -107,7 +107,7 @@ func TestToNetFromNetPreservesWantList(t *testing.T) { ...@@ -107,7 +107,7 @@ func TestToNetFromNetPreservesWantList(t *testing.T) {
original.AddWanted(u.Key("T")) original.AddWanted(u.Key("T"))
original.AddWanted(u.Key("F")) original.AddWanted(u.Key("F"))
p := peer.WithIDString("X") p := mockpeer.WithIDString("X")
netmsg, err := original.ToNet(p) netmsg, err := original.ToNet(p)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -138,7 +138,7 @@ func TestToAndFromNetMessage(t *testing.T) { ...@@ -138,7 +138,7 @@ func TestToAndFromNetMessage(t *testing.T) {
original.AddBlock(blocks.NewBlock([]byte("F"))) original.AddBlock(blocks.NewBlock([]byte("F")))
original.AddBlock(blocks.NewBlock([]byte("M"))) original.AddBlock(blocks.NewBlock([]byte("M")))
p := peer.WithIDString("X") p := mockpeer.WithIDString("X")
netmsg, err := original.ToNet(p) netmsg, err := original.ToNet(p)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
......
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
blocks "github.com/jbenet/go-ipfs/blocks" blocks "github.com/jbenet/go-ipfs/blocks"
message "github.com/jbenet/go-ipfs/exchange/bitswap/message" message "github.com/jbenet/go-ipfs/exchange/bitswap/message"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
"github.com/jbenet/go-ipfs/peer/mock"
) )
type peerAndStrategist struct { type peerAndStrategist struct {
...@@ -16,7 +17,7 @@ type peerAndStrategist struct { ...@@ -16,7 +17,7 @@ type peerAndStrategist struct {
func newPeerAndStrategist(idStr string) peerAndStrategist { func newPeerAndStrategist(idStr string) peerAndStrategist {
return peerAndStrategist{ return peerAndStrategist{
Peer: peer.WithIDString(idStr), Peer: mockpeer.WithIDString(idStr),
Strategy: New(true), Strategy: New(true),
} }
} }
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message" bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network" bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
"github.com/jbenet/go-ipfs/peer/mock"
) )
func TestSendRequestToCooperativePeer(t *testing.T) { func TestSendRequestToCooperativePeer(t *testing.T) {
...@@ -18,8 +19,8 @@ func TestSendRequestToCooperativePeer(t *testing.T) { ...@@ -18,8 +19,8 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
t.Log("Get two network adapters") t.Log("Get two network adapters")
initiator := net.Adapter(peer.WithIDString("initiator")) initiator := net.Adapter(mockpeer.WithIDString("initiator"))
recipient := net.Adapter(peer.WithID(idOfRecipient)) recipient := net.Adapter(mockpeer.WithID(idOfRecipient))
expectedStr := "response from recipient" expectedStr := "response from recipient"
recipient.SetDelegate(lambda(func( recipient.SetDelegate(lambda(func(
...@@ -43,7 +44,7 @@ func TestSendRequestToCooperativePeer(t *testing.T) { ...@@ -43,7 +44,7 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
message := bsmsg.New() message := bsmsg.New()
message.AddBlock(blocks.NewBlock([]byte("data"))) message.AddBlock(blocks.NewBlock([]byte("data")))
response, err := initiator.SendRequest( response, err := initiator.SendRequest(
context.Background(), peer.WithID(idOfRecipient), message) context.Background(), mockpeer.WithID(idOfRecipient), message)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -61,8 +62,8 @@ func TestSendRequestToCooperativePeer(t *testing.T) { ...@@ -61,8 +62,8 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
func TestSendMessageAsyncButWaitForResponse(t *testing.T) { func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
net := VirtualNetwork() net := VirtualNetwork()
idOfResponder := []byte("responder") idOfResponder := []byte("responder")
waiter := net.Adapter(peer.WithIDString("waiter")) waiter := net.Adapter(mockpeer.WithIDString("waiter"))
responder := net.Adapter(peer.WithID(idOfResponder)) responder := net.Adapter(mockpeer.WithID(idOfResponder))
var wg sync.WaitGroup var wg sync.WaitGroup
...@@ -107,7 +108,7 @@ func TestSendMessageAsyncButWaitForResponse(t *testing.T) { ...@@ -107,7 +108,7 @@ func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
messageSentAsync := bsmsg.New() messageSentAsync := bsmsg.New()
messageSentAsync.AddBlock(blocks.NewBlock([]byte("data"))) messageSentAsync.AddBlock(blocks.NewBlock([]byte("data")))
errSending := waiter.SendMessage( errSending := waiter.SendMessage(
context.Background(), peer.WithID(idOfResponder), messageSentAsync) context.Background(), mockpeer.WithID(idOfResponder), messageSentAsync)
if errSending != nil { if errSending != nil {
t.Fatal(errSending) t.Fatal(errSending)
} }
......
...@@ -16,6 +16,7 @@ func NewSessionGenerator( ...@@ -16,6 +16,7 @@ func NewSessionGenerator(
return SessionGenerator{ return SessionGenerator{
net: net, net: net,
rs: rs, rs: rs,
ps: peer.NewPeerstore(),
seq: 0, seq: 0,
} }
} }
...@@ -24,11 +25,12 @@ type SessionGenerator struct { ...@@ -24,11 +25,12 @@ type SessionGenerator struct {
seq int seq int
net tn.Network net tn.Network
rs mock.RoutingServer rs mock.RoutingServer
ps peer.Peerstore
} }
func (g *SessionGenerator) Next() Instance { func (g *SessionGenerator) Next() Instance {
g.seq++ 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 { func (g *SessionGenerator) Instances(n int) []Instance {
...@@ -51,8 +53,8 @@ type Instance struct { ...@@ -51,8 +53,8 @@ type Instance struct {
// NB: It's easy make mistakes by providing the same peer ID to two different // 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 // sessions. To safeguard, use the SessionGenerator to generate sessions. It's
// just a much better idea. // just a much better idea.
func session(net tn.Network, rs mock.RoutingServer, id peer.ID) Instance { func session(net tn.Network, rs mock.RoutingServer, ps peer.Peerstore, id peer.ID) Instance {
p := peer.WithID(id) p := ps.WithID(id)
adapter := net.Adapter(p) adapter := net.Adapter(p)
htc := rs.Client(p) htc := rs.Client(p)
......
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