github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/test/integration/grandcentral_test.go (about) 1 package integrationtest 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "io" 8 "math" 9 "testing" 10 11 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore" 12 syncds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync" 13 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" 14 15 key "github.com/ipfs/go-ipfs/blocks/key" 16 core "github.com/ipfs/go-ipfs/core" 17 "github.com/ipfs/go-ipfs/core/corerouting" 18 "github.com/ipfs/go-ipfs/core/coreunix" 19 mock "github.com/ipfs/go-ipfs/core/mock" 20 mocknet "github.com/ipfs/go-ipfs/p2p/net/mock" 21 "github.com/ipfs/go-ipfs/p2p/peer" 22 "github.com/ipfs/go-ipfs/thirdparty/unit" 23 ds2 "github.com/ipfs/go-ipfs/util/datastore2" 24 testutil "github.com/ipfs/go-ipfs/util/testutil" 25 ) 26 27 func TestSupernodeBootstrappedAddCat(t *testing.T) { 28 // create 8 supernode-routing bootstrap nodes 29 // create 2 supernode-routing clients both bootstrapped to the bootstrap nodes 30 // let the bootstrap nodes share a single datastore 31 // add a large file on one node then cat the file from the other 32 conf := testutil.LatencyConfig{ 33 NetworkLatency: 0, 34 RoutingLatency: 0, 35 BlockstoreLatency: 0, 36 } 37 if err := RunSupernodeBootstrappedAddCat(RandomBytes(100*unit.MB), conf); err != nil { 38 t.Fatal(err) 39 } 40 } 41 42 func RunSupernodeBootstrappedAddCat(data []byte, conf testutil.LatencyConfig) error { 43 ctx, cancel := context.WithCancel(context.Background()) 44 defer cancel() 45 46 servers, clients, err := InitializeSupernodeNetwork(ctx, 8, 2, conf) 47 if err != nil { 48 return err 49 } 50 for _, n := range append(servers, clients...) { 51 defer n.Close() 52 } 53 54 adder := clients[0] 55 catter := clients[1] 56 57 log.Info("adder is", adder.Identity) 58 log.Info("catter is", catter.Identity) 59 60 keyAdded, err := coreunix.Add(adder, bytes.NewReader(data)) 61 if err != nil { 62 return err 63 } 64 65 readerCatted, err := coreunix.Cat(catter, keyAdded) 66 if err != nil { 67 return err 68 } 69 70 // verify 71 bufout := new(bytes.Buffer) 72 io.Copy(bufout, readerCatted) 73 if 0 != bytes.Compare(bufout.Bytes(), data) { 74 return errors.New("catted data does not match added data") 75 } 76 return nil 77 } 78 79 func InitializeSupernodeNetwork( 80 ctx context.Context, 81 numServers, numClients int, 82 conf testutil.LatencyConfig) ([]*core.IpfsNode, []*core.IpfsNode, error) { 83 84 // create network 85 mn := mocknet.New(ctx) 86 87 mn.SetLinkDefaults(mocknet.LinkOptions{ 88 Latency: conf.NetworkLatency, 89 Bandwidth: math.MaxInt32, 90 }) 91 92 routingDatastore := ds2.CloserWrap(syncds.MutexWrap(datastore.NewMapDatastore())) 93 var servers []*core.IpfsNode 94 for i := 0; i < numServers; i++ { 95 bootstrap, err := core.NewNode(ctx, &core.BuildCfg{ 96 Online: true, 97 Host: mock.MockHostOption(mn), 98 Routing: corerouting.SupernodeServer(routingDatastore), 99 }) 100 if err != nil { 101 return nil, nil, err 102 } 103 servers = append(servers, bootstrap) 104 } 105 106 var bootstrapInfos []peer.PeerInfo 107 for _, n := range servers { 108 info := n.Peerstore.PeerInfo(n.PeerHost.ID()) 109 bootstrapInfos = append(bootstrapInfos, info) 110 } 111 112 var clients []*core.IpfsNode 113 for i := 0; i < numClients; i++ { 114 n, err := core.NewNode(ctx, &core.BuildCfg{ 115 Online: true, 116 Host: mock.MockHostOption(mn), 117 Routing: corerouting.SupernodeClient(bootstrapInfos...), 118 }) 119 if err != nil { 120 return nil, nil, err 121 } 122 clients = append(clients, n) 123 } 124 mn.LinkAll() 125 126 bcfg := core.BootstrapConfigWithPeers(bootstrapInfos) 127 for _, n := range clients { 128 if err := n.Bootstrap(bcfg); err != nil { 129 return nil, nil, err 130 } 131 } 132 return servers, clients, nil 133 } 134 135 func TestSupernodePutRecordGetRecord(t *testing.T) { 136 // create 8 supernode-routing bootstrap nodes 137 // create 2 supernode-routing clients both bootstrapped to the bootstrap nodes 138 // let the bootstrap nodes share a single datastore 139 // add a large file on one node then cat the file from the other 140 conf := testutil.LatencyConfig{ 141 NetworkLatency: 0, 142 RoutingLatency: 0, 143 BlockstoreLatency: 0, 144 } 145 if err := RunSupernodePutRecordGetRecord(conf); err != nil { 146 t.Fatal(err) 147 } 148 } 149 150 func RunSupernodePutRecordGetRecord(conf testutil.LatencyConfig) error { 151 ctx, cancel := context.WithCancel(context.Background()) 152 defer cancel() 153 154 servers, clients, err := InitializeSupernodeNetwork(ctx, 2, 2, conf) 155 if err != nil { 156 return err 157 } 158 for _, n := range append(servers, clients...) { 159 defer n.Close() 160 } 161 162 putter := clients[0] 163 getter := clients[1] 164 165 k := key.Key("key") 166 note := []byte("a note from putter") 167 168 if err := putter.Routing.PutValue(ctx, k, note); err != nil { 169 return fmt.Errorf("failed to put value: %s", err) 170 } 171 172 received, err := getter.Routing.GetValue(ctx, k) 173 if err != nil { 174 return fmt.Errorf("failed to get value: %s", err) 175 } 176 177 if 0 != bytes.Compare(note, received) { 178 return errors.New("record doesn't match") 179 } 180 return nil 181 }