addcat_test.go 3.02 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"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
14
	mocknet "github.com/jbenet/go-ipfs/net/mock"
15 16 17
	errors "github.com/jbenet/go-ipfs/util/debugerror"
)

Brian Tiger Chow's avatar
Brian Tiger Chow committed
18 19
const kSeed = 1

Brian Tiger Chow's avatar
Brian Tiger Chow committed
20
func Test1KBInstantaneous(t *testing.T) {
21 22 23 24 25 26
	conf := Config{
		NetworkLatency:    0,
		RoutingLatency:    0,
		BlockstoreLatency: 0,
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
27
	if err := AddCatBytes(RandomBytes(1*KB), conf); err != nil {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
28 29
		t.Fatal(err)
	}
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
}

func TestDegenerateSlowBlockstore(t *testing.T) {
	SkipUnlessEpic(t)
	conf := Config{BlockstoreLatency: 50 * time.Millisecond}
	if err := AddCatPowers(conf, 128); err != nil {
		t.Fatal(err)
	}
}

func TestDegenerateSlowNetwork(t *testing.T) {
	SkipUnlessEpic(t)
	conf := Config{NetworkLatency: 400 * time.Millisecond}
	if err := AddCatPowers(conf, 128); err != nil {
		t.Fatal(err)
	}
}

func TestDegenerateSlowRouting(t *testing.T) {
	SkipUnlessEpic(t)
	conf := Config{RoutingLatency: 400 * time.Millisecond}
	if err := AddCatPowers(conf, 128); err != nil {
		t.Fatal(err)
	}
}

func Test100MBMacbookCoastToCoast(t *testing.T) {
	SkipUnlessEpic(t)
58 59
	conf := Config{}.Network_NYtoSF().Blockstore_SlowSSD2014().Routing_Slow()
	if err := AddCatBytes(RandomBytes(100*1024*1024), conf); err != nil {
60 61 62 63 64 65 66 67
		t.Fatal(err)
	}
}

func AddCatPowers(conf Config, megabytesMax int64) error {
	var i int64
	for i = 1; i < megabytesMax; i = i * 2 {
		fmt.Printf("%d MB\n", i)
68
		if err := AddCatBytes(RandomBytes(i*1024*1024), conf); err != nil {
69 70 71 72 73 74
			return err
		}
	}
	return nil
}

75 76 77 78 79 80 81
func RandomBytes(n int64) []byte {
	var data bytes.Buffer
	random.WritePseudoRandomBytes(n, &data, kSeed)
	return data.Bytes()
}

func AddCatBytes(data []byte, conf Config) error {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
82 83
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
84 85 86 87 88 89 90
	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
91
	mn.SetLinkDefaults(mocknet.LinkOptions{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
92
		Latency: conf.NetworkLatency,
93 94
		// TODO add to conf. This is tricky because we want 0 values to be functional.
		Bandwidth: math.MaxInt32,
95
	})
Brian Tiger Chow's avatar
Brian Tiger Chow committed
96

Brian Tiger Chow's avatar
Brian Tiger Chow committed
97 98
	if len(mn.Peers()) < numPeers {
		return errors.New("test initialization error")
99
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
100 101 102
	adder, err := makeCore(ctx, MocknetTestRepo(mn.Peers()[0], mn.Net(mn.Peers()[0]), conf))
	if err != nil {
		return err
Brian Tiger Chow's avatar
Brian Tiger Chow committed
103
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
104 105 106
	catter, err := makeCore(ctx, MocknetTestRepo(mn.Peers()[1], mn.Net(mn.Peers()[1]), conf))
	if err != nil {
		return err
Brian Tiger Chow's avatar
Brian Tiger Chow committed
107 108
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
109 110
	adder.Bootstrap(ctx, catter.ID())
	catter.Bootstrap(ctx, adder.ID())
111

Brian Tiger Chow's avatar
Brian Tiger Chow committed
112
	keyAdded, err := adder.Add(bytes.NewReader(data))
113 114 115 116
	if err != nil {
		return err
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
117
	readerCatted, err := catter.Cat(keyAdded)
118 119 120 121 122 123 124
	if err != nil {
		return err
	}

	// verify
	var bufout bytes.Buffer
	io.Copy(&bufout, readerCatted)
125
	if 0 != bytes.Compare(bufout.Bytes(), data) {
126 127 128 129 130 131 132 133 134 135
		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()
	}
}