kubo/epictest/three_legged_cat_test.go
Juan Batiz-Benet 6bd4a6ddb2 epictest: fix mn.Peers() setup race
@maybebtc the error was not inside mocknet. the error is in
assuming the peers / nets returned follow the same order.
See:
- https://github.com/jbenet/go-ipfs/blob/master/epictest/addcat_test.go#L100
- https://gist.github.com/jbenet/a39bb9d2f16532a03bb8

if you want the results to be sorted by peer.ID before they
are returned, we can totally do that, but that's probably an
unsafe assumption to make in general-- if you do your
initialization async, the number of networks or peers may have
changed between the two calls. LMK what you prefer.

(thank you golang map chaosmonkey ;)
2014-12-27 22:59:35 -08:00

79 lines
1.8 KiB
Go

package epictest
import (
"bytes"
"io"
"math"
"testing"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
mocknet "github.com/jbenet/go-ipfs/net/mock"
errors "github.com/jbenet/go-ipfs/util/debugerror"
)
func TestThreeLeggedCat(t *testing.T) {
conf := Config{
NetworkLatency: 0,
RoutingLatency: 0,
BlockstoreLatency: 0,
}
if err := RunThreeLeggedCat(RandomBytes(1*KB), conf); err != nil {
t.Fatal(err)
}
}
func RunThreeLeggedCat(data []byte, conf Config) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
const numPeers = 3
// create network
mn, err := mocknet.FullMeshLinked(ctx, numPeers)
if err != nil {
return errors.Wrap(err)
}
mn.SetLinkDefaults(mocknet.LinkOptions{
Latency: conf.NetworkLatency,
// TODO add to conf. This is tricky because we want 0 values to be functional.
Bandwidth: math.MaxInt32,
})
peers := mn.Peers()
if len(peers) < numPeers {
return errors.New("test initialization error")
}
adder, err := makeCore(ctx, MocknetTestRepo(peers[0], mn.Net(peers[0]), conf))
if err != nil {
return err
}
catter, err := makeCore(ctx, MocknetTestRepo(peers[1], mn.Net(peers[1]), conf))
if err != nil {
return err
}
bootstrap, err := makeCore(ctx, MocknetTestRepo(peers[2], mn.Net(peers[2]), conf))
if err != nil {
return err
}
adder.Bootstrap(ctx, bootstrap.ID())
catter.Bootstrap(ctx, bootstrap.ID())
keyAdded, err := adder.Add(bytes.NewReader(data))
if err != nil {
return err
}
readerCatted, err := catter.Cat(keyAdded)
if err != nil {
return err
}
// verify
var bufout bytes.Buffer
io.Copy(&bufout, readerCatted)
if 0 != bytes.Compare(bufout.Bytes(), data) {
return errors.New("catted data does not match added data")
}
return nil
}