1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-26 23:53:19 +08:00

Merge pull request #4499 from ipfs/fix/better-bitswap-test

improve bitswap tests
This commit is contained in:
Whyrusleeping
2017-12-16 10:31:03 -08:00
committed by GitHub
2 changed files with 38 additions and 12 deletions

View File

@ -108,7 +108,7 @@ func TestLargeSwarm(t *testing.T) {
if detectrace.WithRace() { if detectrace.WithRace() {
// when running with the race detector, 500 instances launches // when running with the race detector, 500 instances launches
// well over 8k goroutines. This hits a race detector limit. // well over 8k goroutines. This hits a race detector limit.
numInstances = 100 numInstances = 75
} else if travis.IsRunning() { } else if travis.IsRunning() {
numInstances = 200 numInstances = 200
} else { } else {
@ -292,23 +292,22 @@ func TestEmptyKey(t *testing.T) {
} }
} }
func assertStat(st *Stat, sblks, rblks, sdata, rdata uint64) error { func assertStat(t *testing.T, st *Stat, sblks, rblks, sdata, rdata uint64) {
if sblks != st.BlocksSent { if sblks != st.BlocksSent {
return fmt.Errorf("mismatch in blocks sent: %d vs %d", sblks, st.BlocksSent) t.Errorf("mismatch in blocks sent: %d vs %d", sblks, st.BlocksSent)
} }
if rblks != st.BlocksReceived { if rblks != st.BlocksReceived {
return fmt.Errorf("mismatch in blocks recvd: %d vs %d", rblks, st.BlocksReceived) t.Errorf("mismatch in blocks recvd: %d vs %d", rblks, st.BlocksReceived)
} }
if sdata != st.DataSent { if sdata != st.DataSent {
return fmt.Errorf("mismatch in data sent: %d vs %d", sdata, st.DataSent) t.Errorf("mismatch in data sent: %d vs %d", sdata, st.DataSent)
} }
if rdata != st.DataReceived { if rdata != st.DataReceived {
return fmt.Errorf("mismatch in data recvd: %d vs %d", rdata, st.DataReceived) t.Errorf("mismatch in data recvd: %d vs %d", rdata, st.DataReceived)
} }
return nil
} }
func TestBasicBitswap(t *testing.T) { func TestBasicBitswap(t *testing.T) {
@ -355,12 +354,20 @@ func TestBasicBitswap(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if err := assertStat(st0, 1, 0, 1, 0); err != nil { st2, err := instances[2].Exchange.Stat()
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := assertStat(st1, 0, 1, 0, 1); err != nil { t.Log("stat node 0")
t.Fatal(err) assertStat(t, st0, 1, 0, uint64(len(blk.RawData())), 0)
t.Log("stat node 1")
assertStat(t, st1, 0, 1, 0, uint64(len(blk.RawData())))
t.Log("stat node 2")
assertStat(t, st2, 0, 0, 0, 0)
if !bytes.Equal(blk.RawData(), blocks[0].RawData()) {
t.Errorf("blocks aren't equal: expected %v, actual %v", blocks[0].RawData(), blk.RawData())
} }
t.Log(blk) t.Log(blk)

View File

@ -3,6 +3,7 @@ package bitswap
import ( import (
"context" "context"
"errors" "errors"
"sync"
bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message" bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network" bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
@ -29,6 +30,7 @@ func VirtualNetwork(rs mockrouting.Server, d delay.D) Network {
} }
type network struct { type network struct {
mu sync.Mutex
clients map[peer.ID]bsnet.Receiver clients map[peer.ID]bsnet.Receiver
routingserver mockrouting.Server routingserver mockrouting.Server
delay delay.D delay delay.D
@ -36,6 +38,9 @@ type network struct {
} }
func (n *network) Adapter(p testutil.Identity) bsnet.BitSwapNetwork { func (n *network) Adapter(p testutil.Identity) bsnet.BitSwapNetwork {
n.mu.Lock()
defer n.mu.Unlock()
client := &networkClient{ client := &networkClient{
local: p.ID(), local: p.ID(),
network: n, network: n,
@ -46,6 +51,9 @@ func (n *network) Adapter(p testutil.Identity) bsnet.BitSwapNetwork {
} }
func (n *network) HasPeer(p peer.ID) bool { func (n *network) HasPeer(p peer.ID) bool {
n.mu.Lock()
defer n.mu.Unlock()
_, found := n.clients[p] _, found := n.clients[p]
return found return found
} }
@ -58,6 +66,9 @@ func (n *network) SendMessage(
to peer.ID, to peer.ID,
message bsmsg.BitSwapMessage) error { message bsmsg.BitSwapMessage) error {
n.mu.Lock()
defer n.mu.Unlock()
receiver, ok := n.clients[to] receiver, ok := n.clients[to]
if !ok { if !ok {
return errors.New("Cannot locate peer on network") return errors.New("Cannot locate peer on network")
@ -161,18 +172,26 @@ func (nc *networkClient) SetDelegate(r bsnet.Receiver) {
} }
func (nc *networkClient) ConnectTo(_ context.Context, p peer.ID) error { func (nc *networkClient) ConnectTo(_ context.Context, p peer.ID) error {
if !nc.network.HasPeer(p) { nc.network.mu.Lock()
otherClient, ok := nc.network.clients[p]
if !ok {
nc.network.mu.Unlock()
return errors.New("no such peer in network") return errors.New("no such peer in network")
} }
tag := tagForPeers(nc.local, p) tag := tagForPeers(nc.local, p)
if _, ok := nc.network.conns[tag]; ok { if _, ok := nc.network.conns[tag]; ok {
nc.network.mu.Unlock()
log.Warning("ALREADY CONNECTED TO PEER (is this a reconnect? test lib needs fixing)") log.Warning("ALREADY CONNECTED TO PEER (is this a reconnect? test lib needs fixing)")
return nil return nil
} }
nc.network.conns[tag] = struct{}{} nc.network.conns[tag] = struct{}{}
nc.network.mu.Unlock()
// TODO: add handling for disconnects // TODO: add handling for disconnects
nc.network.clients[p].PeerConnected(nc.local) otherClient.PeerConnected(nc.local)
nc.Receiver.PeerConnected(p) nc.Receiver.PeerConnected(p)
return nil return nil
} }