github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/test/integration/addcat_test.go (about) 1 package integrationtest 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "io" 8 "math" 9 "os" 10 "testing" 11 "time" 12 13 random "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random" 14 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" 15 16 "github.com/ipfs/go-ipfs/core" 17 coreunix "github.com/ipfs/go-ipfs/core/coreunix" 18 mock "github.com/ipfs/go-ipfs/core/mock" 19 mocknet "github.com/ipfs/go-ipfs/p2p/net/mock" 20 "github.com/ipfs/go-ipfs/p2p/peer" 21 eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog" 22 "github.com/ipfs/go-ipfs/thirdparty/unit" 23 testutil "github.com/ipfs/go-ipfs/util/testutil" 24 ) 25 26 var log = eventlog.Logger("epictest") 27 28 const kSeed = 1 29 30 func Test1KBInstantaneous(t *testing.T) { 31 conf := testutil.LatencyConfig{ 32 NetworkLatency: 0, 33 RoutingLatency: 0, 34 BlockstoreLatency: 0, 35 } 36 37 if err := DirectAddCat(RandomBytes(1*unit.KB), conf); err != nil { 38 t.Fatal(err) 39 } 40 } 41 42 func TestDegenerateSlowBlockstore(t *testing.T) { 43 SkipUnlessEpic(t) 44 conf := testutil.LatencyConfig{BlockstoreLatency: 50 * time.Millisecond} 45 if err := AddCatPowers(conf, 128); err != nil { 46 t.Fatal(err) 47 } 48 } 49 50 func TestDegenerateSlowNetwork(t *testing.T) { 51 SkipUnlessEpic(t) 52 conf := testutil.LatencyConfig{NetworkLatency: 400 * time.Millisecond} 53 if err := AddCatPowers(conf, 128); err != nil { 54 t.Fatal(err) 55 } 56 } 57 58 func TestDegenerateSlowRouting(t *testing.T) { 59 SkipUnlessEpic(t) 60 conf := testutil.LatencyConfig{RoutingLatency: 400 * time.Millisecond} 61 if err := AddCatPowers(conf, 128); err != nil { 62 t.Fatal(err) 63 } 64 } 65 66 func Test100MBMacbookCoastToCoast(t *testing.T) { 67 SkipUnlessEpic(t) 68 conf := testutil.LatencyConfig{}.NetworkNYtoSF().BlockstoreSlowSSD2014().RoutingSlow() 69 if err := DirectAddCat(RandomBytes(100*1024*1024), conf); err != nil { 70 t.Fatal(err) 71 } 72 } 73 74 func AddCatPowers(conf testutil.LatencyConfig, megabytesMax int64) error { 75 var i int64 76 for i = 1; i < megabytesMax; i = i * 2 { 77 fmt.Printf("%d MB\n", i) 78 if err := DirectAddCat(RandomBytes(i*1024*1024), conf); err != nil { 79 return err 80 } 81 } 82 return nil 83 } 84 85 func RandomBytes(n int64) []byte { 86 data := new(bytes.Buffer) 87 random.WritePseudoRandomBytes(n, data, kSeed) 88 return data.Bytes() 89 } 90 91 func DirectAddCat(data []byte, conf testutil.LatencyConfig) error { 92 ctx, cancel := context.WithCancel(context.Background()) 93 defer cancel() 94 95 // create network 96 mn := mocknet.New(ctx) 97 mn.SetLinkDefaults(mocknet.LinkOptions{ 98 Latency: conf.NetworkLatency, 99 // TODO add to conf. This is tricky because we want 0 values to be functional. 100 Bandwidth: math.MaxInt32, 101 }) 102 103 adder, err := core.NewNode(ctx, &core.BuildCfg{ 104 Online: true, 105 Host: mock.MockHostOption(mn), 106 }) 107 if err != nil { 108 return err 109 } 110 defer adder.Close() 111 112 catter, err := core.NewNode(ctx, &core.BuildCfg{ 113 Online: true, 114 Host: mock.MockHostOption(mn), 115 }) 116 if err != nil { 117 return err 118 } 119 defer catter.Close() 120 121 err = mn.LinkAll() 122 if err != nil { 123 return err 124 } 125 126 bs1 := []peer.PeerInfo{adder.Peerstore.PeerInfo(adder.Identity)} 127 bs2 := []peer.PeerInfo{catter.Peerstore.PeerInfo(catter.Identity)} 128 129 if err := catter.Bootstrap(core.BootstrapConfigWithPeers(bs1)); err != nil { 130 return err 131 } 132 if err := adder.Bootstrap(core.BootstrapConfigWithPeers(bs2)); err != nil { 133 return err 134 } 135 136 added, err := coreunix.Add(adder, bytes.NewReader(data)) 137 if err != nil { 138 return err 139 } 140 141 readerCatted, err := coreunix.Cat(catter, added) 142 if err != nil { 143 return err 144 } 145 146 // verify 147 bufout := new(bytes.Buffer) 148 io.Copy(bufout, readerCatted) 149 if 0 != bytes.Compare(bufout.Bytes(), data) { 150 return errors.New("catted data does not match added data") 151 } 152 return nil 153 } 154 155 func SkipUnlessEpic(t *testing.T) { 156 if os.Getenv("IPFS_EPIC_TEST") == "" { 157 t.SkipNow() 158 } 159 }