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 (
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)
......
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