Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve bitswap tests #4499

Merged
merged 3 commits into from
Dec 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions exchange/bitswap/bitswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Max go routines with race detector: 8196.
Bitswap workers per peer: n^2.

75 means 75^2 = 5625

That gives us some healthy head-room for other random goroutines.

} else if travis.IsRunning() {
numInstances = 200
} else {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 21 additions & 2 deletions exchange/bitswap/testnet/virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -29,13 +30,17 @@ 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
conns map[string]struct{}
}

func (n *network) Adapter(p testutil.Identity) bsnet.BitSwapNetwork {
n.mu.Lock()
defer n.mu.Unlock()

client := &networkClient{
local: p.ID(),
network: n,
Expand All @@ -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
}
Expand All @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down