Unverified Commit 2ea8d171 authored by Whyrusleeping's avatar Whyrusleeping Committed by GitHub

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

improve bitswap tests
parents 34ade520 a5e9d0a2
......@@ -108,7 +108,7 @@ func TestLargeSwarm(t *testing.T) {
if detectrace.WithRace() {
// when running with the race detector, 500 instances launches
// well over 8k goroutines. This hits a race detector limit.
numInstances = 100
numInstances = 75
} else if travis.IsRunning() {
numInstances = 200
} 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 {
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 {
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 {
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 {
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) {
......@@ -355,12 +354,20 @@ func TestBasicBitswap(t *testing.T) {
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)
}
if err := assertStat(st1, 0, 1, 0, 1); err != nil {
t.Fatal(err)
t.Log("stat node 0")
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)
......
......@@ -3,6 +3,7 @@ package bitswap
import (
"context"
"errors"
"sync"
bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
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 {
mu sync.Mutex
clients map[peer.ID]bsnet.Receiver
routingserver mockrouting.Server
delay delay.D
......@@ -36,6 +38,9 @@ type network struct {
}
func (n *network) Adapter(p testutil.Identity) bsnet.BitSwapNetwork {
n.mu.Lock()
defer n.mu.Unlock()
client := &networkClient{
local: p.ID(),
network: n,
......@@ -46,6 +51,9 @@ func (n *network) Adapter(p testutil.Identity) bsnet.BitSwapNetwork {
}
func (n *network) HasPeer(p peer.ID) bool {
n.mu.Lock()
defer n.mu.Unlock()
_, found := n.clients[p]
return found
}
......@@ -58,6 +66,9 @@ func (n *network) SendMessage(
to peer.ID,
message bsmsg.BitSwapMessage) error {
n.mu.Lock()
defer n.mu.Unlock()
receiver, ok := n.clients[to]
if !ok {
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 {
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")
}
tag := tagForPeers(nc.local, p)
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)")
return nil
}
nc.network.conns[tag] = struct{}{}
nc.network.mu.Unlock()
// TODO: add handling for disconnects
nc.network.clients[p].PeerConnected(nc.local)
otherClient.PeerConnected(nc.local)
nc.Receiver.PeerConnected(p)
return nil
}
......
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