-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy paththree_legged_cat_test.go
150 lines (130 loc) · 3.45 KB
/
three_legged_cat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package integrationtest
import (
"bytes"
"context"
"errors"
"io"
"math"
"testing"
"time"
core "github.com/ipfs/go-ipfs/core"
bootstrap2 "github.com/ipfs/go-ipfs/core/bootstrap"
"github.com/ipfs/go-ipfs/core/coreapi"
mock "github.com/ipfs/go-ipfs/core/mock"
"github.com/ipfs/go-ipfs/thirdparty/unit"
files "github.com/ipfs/go-ipfs-files"
peer "github.com/libp2p/go-libp2p-core/peer"
testutil "github.com/libp2p/go-libp2p-testing/net"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
)
func TestThreeLeggedCatTransfer(t *testing.T) {
conf := testutil.LatencyConfig{
NetworkLatency: 0,
RoutingLatency: 0,
BlockstoreLatency: 0,
}
if err := RunThreeLeggedCat(RandomBytes(100*unit.MB), conf); err != nil {
t.Fatal(err)
}
}
func TestThreeLeggedCatDegenerateSlowBlockstore(t *testing.T) {
SkipUnlessEpic(t)
conf := testutil.LatencyConfig{BlockstoreLatency: 50 * time.Millisecond}
if err := RunThreeLeggedCat(RandomBytes(1*unit.KB), conf); err != nil {
t.Fatal(err)
}
}
func TestThreeLeggedCatDegenerateSlowNetwork(t *testing.T) {
SkipUnlessEpic(t)
conf := testutil.LatencyConfig{NetworkLatency: 400 * time.Millisecond}
if err := RunThreeLeggedCat(RandomBytes(1*unit.KB), conf); err != nil {
t.Fatal(err)
}
}
func TestThreeLeggedCatDegenerateSlowRouting(t *testing.T) {
SkipUnlessEpic(t)
conf := testutil.LatencyConfig{RoutingLatency: 400 * time.Millisecond}
if err := RunThreeLeggedCat(RandomBytes(1*unit.KB), conf); err != nil {
t.Fatal(err)
}
}
func TestThreeLeggedCat100MBMacbookCoastToCoast(t *testing.T) {
SkipUnlessEpic(t)
conf := testutil.LatencyConfig{}.NetworkNYtoSF().BlockstoreSlowSSD2014().RoutingSlow()
if err := RunThreeLeggedCat(RandomBytes(100*unit.MB), conf); err != nil {
t.Fatal(err)
}
}
func RunThreeLeggedCat(data []byte, conf testutil.LatencyConfig) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create network
mn := mocknet.New(ctx)
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,
})
bootstrap, err := core.NewNode(ctx, &core.BuildCfg{
Online: true,
Host: mock.MockHostOption(mn),
})
if err != nil {
return err
}
defer bootstrap.Close()
adder, err := core.NewNode(ctx, &core.BuildCfg{
Online: true,
Host: mock.MockHostOption(mn),
})
if err != nil {
return err
}
defer adder.Close()
catter, err := core.NewNode(ctx, &core.BuildCfg{
Online: true,
Host: mock.MockHostOption(mn),
})
if err != nil {
return err
}
defer catter.Close()
adderApi, err := coreapi.NewCoreAPI(adder)
if err != nil {
return err
}
catterApi, err := coreapi.NewCoreAPI(catter)
if err != nil {
return err
}
err = mn.LinkAll()
if err != nil {
return err
}
bis := bootstrap.Peerstore.PeerInfo(bootstrap.PeerHost.ID())
bcfg := bootstrap2.BootstrapConfigWithPeers([]peer.AddrInfo{bis})
if err := adder.Bootstrap(bcfg); err != nil {
return err
}
if err := catter.Bootstrap(bcfg); err != nil {
return err
}
added, err := adderApi.Unixfs().Add(ctx, files.NewBytesFile(data))
if err != nil {
return err
}
readerCatted, err := catterApi.Unixfs().Get(ctx, added)
if err != nil {
return err
}
// verify
var bufout bytes.Buffer
_, err = io.Copy(&bufout, readerCatted.(io.Reader))
if err != nil {
return err
}
if !bytes.Equal(bufout.Bytes(), data) {
return errors.New("catted data does not match added data")
}
return nil
}