addcat_test.go 3.56 KB
Newer Older
1 2 3 4 5 6
package epictest

import (
	"bytes"
	"fmt"
	"io"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
7
	"math"
8 9 10 11
	"os"
	"testing"
	"time"

12
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
13
	random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
14
	"github.com/jbenet/go-ipfs/core"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
15
	coreunix "github.com/jbenet/go-ipfs/core/coreunix"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
	mocknet "github.com/jbenet/go-ipfs/p2p/net/mock"
17
	"github.com/jbenet/go-ipfs/p2p/peer"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
18
	"github.com/jbenet/go-ipfs/thirdparty/unit"
19
	errors "github.com/jbenet/go-ipfs/util/debugerror"
20
	testutil "github.com/jbenet/go-ipfs/util/testutil"
21 22
)

Brian Tiger Chow's avatar
Brian Tiger Chow committed
23 24
const kSeed = 1

Brian Tiger Chow's avatar
Brian Tiger Chow committed
25
func Test1KBInstantaneous(t *testing.T) {
26
	conf := testutil.LatencyConfig{
27 28 29 30 31
		NetworkLatency:    0,
		RoutingLatency:    0,
		BlockstoreLatency: 0,
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
32
	if err := DirectAddCat(RandomBytes(1*unit.KB), conf); err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
33 34
		t.Fatal(err)
	}
35 36 37 38
}

func TestDegenerateSlowBlockstore(t *testing.T) {
	SkipUnlessEpic(t)
39
	conf := testutil.LatencyConfig{BlockstoreLatency: 50 * time.Millisecond}
40 41 42 43 44 45 46
	if err := AddCatPowers(conf, 128); err != nil {
		t.Fatal(err)
	}
}

func TestDegenerateSlowNetwork(t *testing.T) {
	SkipUnlessEpic(t)
47
	conf := testutil.LatencyConfig{NetworkLatency: 400 * time.Millisecond}
48 49 50 51 52 53 54
	if err := AddCatPowers(conf, 128); err != nil {
		t.Fatal(err)
	}
}

func TestDegenerateSlowRouting(t *testing.T) {
	SkipUnlessEpic(t)
55
	conf := testutil.LatencyConfig{RoutingLatency: 400 * time.Millisecond}
56 57 58 59 60 61 62
	if err := AddCatPowers(conf, 128); err != nil {
		t.Fatal(err)
	}
}

func Test100MBMacbookCoastToCoast(t *testing.T) {
	SkipUnlessEpic(t)
63
	conf := testutil.LatencyConfig{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow()
Brian Tiger Chow's avatar
rename  
Brian Tiger Chow committed
64
	if err := DirectAddCat(RandomBytes(100*1024*1024), conf); err != nil {
65 66 67 68
		t.Fatal(err)
	}
}

69
func AddCatPowers(conf testutil.LatencyConfig, megabytesMax int64) error {
70 71 72
	var i int64
	for i = 1; i < megabytesMax; i = i * 2 {
		fmt.Printf("%d MB\n", i)
Brian Tiger Chow's avatar
rename  
Brian Tiger Chow committed
73
		if err := DirectAddCat(RandomBytes(i*1024*1024), conf); err != nil {
74 75 76 77 78 79
			return err
		}
	}
	return nil
}

80 81 82 83 84 85
func RandomBytes(n int64) []byte {
	var data bytes.Buffer
	random.WritePseudoRandomBytes(n, &data, kSeed)
	return data.Bytes()
}

86
func DirectAddCat(data []byte, conf testutil.LatencyConfig) error {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
87 88
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
89 90 91 92 93 94 95
	const numPeers = 2

	// create network
	mn, err := mocknet.FullMeshLinked(ctx, numPeers)
	if err != nil {
		return errors.Wrap(err)
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
96
	mn.SetLinkDefaults(mocknet.LinkOptions{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
97
		Latency: conf.NetworkLatency,
98 99
		// TODO add to conf. This is tricky because we want 0 values to be functional.
		Bandwidth: math.MaxInt32,
100
	})
Brian Tiger Chow's avatar
Brian Tiger Chow committed
101

102 103
	peers := mn.Peers()
	if len(peers) < numPeers {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
104
		return errors.New("test initialization error")
105
	}
106

107
	adder, err := core.NewIPFSNode(ctx, core.ConfigOption(MocknetTestRepo(peers[0], mn.Host(peers[0]), conf)))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
108 109
	if err != nil {
		return err
Brian Tiger Chow's avatar
Brian Tiger Chow committed
110
	}
Brian Tiger Chow's avatar
cleanup  
Brian Tiger Chow committed
111
	defer adder.Close()
112
	catter, err := core.NewIPFSNode(ctx, core.ConfigOption(MocknetTestRepo(peers[1], mn.Host(peers[1]), conf)))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
113 114
	if err != nil {
		return err
Brian Tiger Chow's avatar
Brian Tiger Chow committed
115
	}
Brian Tiger Chow's avatar
cleanup  
Brian Tiger Chow committed
116
	defer catter.Close()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
117

118 119
	catter.Bootstrap(ctx, []peer.PeerInfo{adder.Peerstore.PeerInfo(adder.Identity)})
	adder.Bootstrap(ctx, []peer.PeerInfo{catter.Peerstore.PeerInfo(catter.Identity)})
120

Brian Tiger Chow's avatar
Brian Tiger Chow committed
121
	keyAdded, err := coreunix.Add(adder, bytes.NewReader(data))
122 123 124 125
	if err != nil {
		return err
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
126
	readerCatted, err := coreunix.Cat(catter, keyAdded)
127 128 129 130 131 132 133
	if err != nil {
		return err
	}

	// verify
	var bufout bytes.Buffer
	io.Copy(&bufout, readerCatted)
134
	if 0 != bytes.Compare(bufout.Bytes(), data) {
135 136 137 138 139 140 141 142 143 144
		return errors.New("catted data does not match added data")
	}
	return nil
}

func SkipUnlessEpic(t *testing.T) {
	if os.Getenv("IPFS_EPIC_TEST") == "" {
		t.SkipNow()
	}
}