@cryptotaxi247 / kubo / commits / abd971d39

extract initialization

refactor(epictest) Core refactor: extract repo fix move core

Brian Tiger Chow committed Dec 25, 2014 at 01:37 UTC abd971d3931454551c73e09d1c42ed79eda3c16e
2 files changed +161 -95
epictest/addcat_test.go
+13 -95
@@ -10,27 +10,9 @@ import (
10 "time"
11
12 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
13 - datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
14 - sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
13 random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
16 - blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
17 - blockservice "github.com/jbenet/go-ipfs/blockservice"
18 - exchange "github.com/jbenet/go-ipfs/exchange"
19 - bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
20 - bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
21 - importer "github.com/jbenet/go-ipfs/importer"
22 - chunk "github.com/jbenet/go-ipfs/importer/chunk"
23 - merkledag "github.com/jbenet/go-ipfs/merkledag"
24 - net "github.com/jbenet/go-ipfs/net"
14 mocknet "github.com/jbenet/go-ipfs/net/mock"
26 - path "github.com/jbenet/go-ipfs/path"
27 - peer "github.com/jbenet/go-ipfs/peer"
28 - dht "github.com/jbenet/go-ipfs/routing/dht"
29 - uio "github.com/jbenet/go-ipfs/unixfs/io"
30 - util "github.com/jbenet/go-ipfs/util"
31 - "github.com/jbenet/go-ipfs/util/datastore2"
15 errors "github.com/jbenet/go-ipfs/util/debugerror"
33 - delay "github.com/jbenet/go-ipfs/util/delay"
16 )
17
18 const kSeed = 1
@@ -42,7 +24,7 @@ func Test1KBInstantaneous(t *testing.T) {
24 BlockstoreLatency: 0,
25 }
26
45 - if err := AddCatBytes(RandomBytes(100*MB), conf); err != nil {
27 + if err := AddCatBytes(RandomBytes(1*KB), conf); err != nil {
28 t.Fatal(err)
29 }
30 }
@@ -96,23 +78,10 @@ func RandomBytes(n int64) []byte {
78 return data.Bytes()
79 }
80
99 -type instance struct {
100 - ID peer.ID
101 - Network net.Network
102 - Blockstore blockstore.Blockstore
103 - Datastore datastore.ThreadSafeDatastore
104 - DHT *dht.IpfsDHT
105 - Exchange exchange.Interface
106 - BitSwapNetwork bsnet.BitSwapNetwork
107 -
108 - datastoreDelay delay.D
109 -}
110 -
81 func AddCatBytes(data []byte, conf Config) error {
82 ctx, cancel := context.WithCancel(context.Background())
83 defer cancel()
84 const numPeers = 2
115 - instances := make(map[peer.ID]*instance, numPeers)
85
86 // create network
87 mn, err := mocknet.FullMeshLinked(ctx, numPeers)
@@ -124,57 +93,28 @@ func AddCatBytes(data []byte, conf Config) error {
93 // TODO add to conf. This is tricky because we want 0 values to be functional.
94 Bandwidth: math.MaxInt32,
95 })
127 - for _, p := range mn.Peers() {
128 - instances[p] = &instance{
129 - ID: p,
130 - Network: mn.Net(p),
131 - }
132 - }
96
134 - // create dht network
135 - for _, p := range mn.Peers() {
136 - dsDelay := delay.Fixed(conf.BlockstoreLatency)
137 - instances[p].Datastore = sync.MutexWrap(datastore2.WithDelay(datastore.NewMapDatastore(), dsDelay))
138 - instances[p].datastoreDelay = dsDelay
139 - }
140 - for _, p := range mn.Peers() {
141 - instances[p].DHT = dht.NewDHT(ctx, p, instances[p].Network, instances[p].Datastore)
97 + if len(mn.Peers()) < numPeers {
98 + return errors.New("test initialization error")
99 }
143 - // create two bitswap network clients
144 - for _, p := range mn.Peers() {
145 - instances[p].BitSwapNetwork = bsnet.NewFromIpfsNetwork(instances[p].Network, instances[p].DHT)
146 - }
147 - for _, p := range mn.Peers() {
148 - const kWriteCacheElems = 100
149 - const alwaysSendToPeer = true
150 - adapter := instances[p].BitSwapNetwork
151 - dstore := instances[p].Datastore
152 - instances[p].Blockstore, err = blockstore.WriteCached(blockstore.NewBlockstore(dstore), kWriteCacheElems)
153 - if err != nil {
154 - return err
155 - }
156 - instances[p].Exchange = bitswap.New(ctx, p, adapter, instances[p].Blockstore, alwaysSendToPeer)
100 + adder, err := makeCore(ctx, MocknetTestRepo(mn.Peers()[0], mn.Net(mn.Peers()[0]), conf))
101 + if err != nil {
102 + return err
103 }
158 - var peers []peer.ID
159 - for _, p := range mn.Peers() {
160 - peers = append(peers, p)
104 + catter, err := makeCore(ctx, MocknetTestRepo(mn.Peers()[1], mn.Net(mn.Peers()[1]), conf))
105 + if err != nil {
106 + return err
107 }
108
163 - adder := instances[peers[0]]
164 - catter := instances[peers[1]]
165 -
166 - // bootstrap the DHTs
167 - adder.DHT.Connect(ctx, catter.ID)
168 - catter.DHT.Connect(ctx, adder.ID)
109 + adder.Bootstrap(ctx, catter.ID())
110 + catter.Bootstrap(ctx, adder.ID())
111
170 - adder.datastoreDelay.Set(0) // disable blockstore latency during add operation
171 - keyAdded, err := add(adder, bytes.NewReader(data))
112 + keyAdded, err := adder.Add(bytes.NewReader(data))
113 if err != nil {
114 return err
115 }
175 - adder.datastoreDelay.Set(conf.BlockstoreLatency) // add some blockstore delay to make the catter wait
116
177 - readerCatted, err := cat(catter, keyAdded)
117 + readerCatted, err := catter.Cat(keyAdded)
118 if err != nil {
119 return err
120 }
@@ -188,28 +128,6 @@ func AddCatBytes(data []byte, conf Config) error {
128 return nil
129 }
130
191 -func cat(catter *instance, k util.Key) (io.Reader, error) {
192 - catterdag := merkledag.NewDAGService(&blockservice.BlockService{catter.Blockstore, catter.Exchange})
193 - nodeCatted, err := (&path.Resolver{catterdag}).ResolvePath(k.String())
194 - if err != nil {
195 - return nil, err
196 - }
197 - return uio.NewDagReader(nodeCatted, catterdag)
198 -}
199 -
200 -func add(adder *instance, r io.Reader) (util.Key, error) {
201 - nodeAdded, err := importer.BuildDagFromReader(
202 - r,
203 - merkledag.NewDAGService(&blockservice.BlockService{adder.Blockstore, adder.Exchange}),
204 - nil,
205 - chunk.DefaultSplitter,
206 - )
207 - if err != nil {
208 - return "", err
209 - }
210 - return nodeAdded.Key()
211 -}
212 -
131 func SkipUnlessEpic(t *testing.T) {
132 if os.Getenv("IPFS_EPIC_TEST") == "" {
133 t.SkipNow()
epictest/core.go new
+148
@@ -0,0 +1,148 @@
1 +package epictest
2 +
3 +import (
4 + "io"
5 +
6 + context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7 + datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
8 + sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
9 + blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
10 + blockservice "github.com/jbenet/go-ipfs/blockservice"
11 + exchange "github.com/jbenet/go-ipfs/exchange"
12 + bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
13 + bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
14 + importer "github.com/jbenet/go-ipfs/importer"
15 + chunk "github.com/jbenet/go-ipfs/importer/chunk"
16 + merkledag "github.com/jbenet/go-ipfs/merkledag"
17 + net "github.com/jbenet/go-ipfs/net"
18 + path "github.com/jbenet/go-ipfs/path"
19 + peer "github.com/jbenet/go-ipfs/peer"
20 + dht "github.com/jbenet/go-ipfs/routing/dht"
21 + uio "github.com/jbenet/go-ipfs/unixfs/io"
22 + util "github.com/jbenet/go-ipfs/util"
23 + "github.com/jbenet/go-ipfs/util/datastore2"
24 + delay "github.com/jbenet/go-ipfs/util/delay"
25 +)
26 +
27 +// TODO merge with core.IpfsNode
28 +type core struct {
29 + repo Repo
30 +
31 + blockService *blockservice.BlockService
32 + blockstore blockstore.Blockstore
33 + dag merkledag.DAGService
34 + id peer.ID
35 +}
36 +
37 +func (c *core) ID() peer.ID {
38 + return c.repo.ID()
39 +}
40 +
41 +func (c *core) Bootstrap(ctx context.Context, p peer.ID) error {
42 + return c.repo.Bootstrap(ctx, p)
43 +}
44 +
45 +func (c *core) Cat(k util.Key) (io.Reader, error) {
46 + catterdag := c.dag
47 + nodeCatted, err := (&path.Resolver{catterdag}).ResolvePath(k.String())
48 + if err != nil {
49 + return nil, err
50 + }
51 + return uio.NewDagReader(nodeCatted, catterdag)
52 +}
53 +
54 +func (c *core) Add(r io.Reader) (util.Key, error) {
55 + nodeAdded, err := importer.BuildDagFromReader(
56 + r,
57 + c.dag,
58 + nil,
59 + chunk.DefaultSplitter,
60 + )
61 + if err != nil {
62 + return "", err
63 + }
64 + return nodeAdded.Key()
65 +}
66 +
67 +func makeCore(ctx context.Context, rf RepoFactory) (*core, error) {
68 + repo, err := rf(ctx)
69 + if err != nil {
70 + return nil, err
71 + }
72 +
73 + bss := &blockservice.BlockService{repo.Blockstore(), repo.Exchange()}
74 + dag := merkledag.NewDAGService(bss)
75 + // to make sure nothing is omitted, init each individual field and assign
76 + // all at once at the bottom.
77 + return &core{
78 + repo: repo,
79 + blockService: bss,
80 + dag: dag,
81 + }, nil
82 +}
83 +
84 +type RepoFactory func(ctx context.Context) (Repo, error)
85 +
86 +type Repo interface {
87 + ID() peer.ID
88 + Blockstore() blockstore.Blockstore
89 + Exchange() exchange.Interface
90 +
91 + Bootstrap(ctx context.Context, peer peer.ID) error
92 +}
93 +
94 +type repo struct {
95 + // DHT, Exchange, Network,Datastore
96 + bitSwapNetwork bsnet.BitSwapNetwork
97 + blockstore blockstore.Blockstore
98 + exchange exchange.Interface
99 + datastore datastore.ThreadSafeDatastore
100 + network net.Network
101 + dht *dht.IpfsDHT
102 + id peer.ID
103 +}
104 +
105 +func (r *repo) ID() peer.ID {
106 + return r.id
107 +}
108 +
109 +func (c *repo) Bootstrap(ctx context.Context, p peer.ID) error {
110 + return c.dht.Connect(ctx, p)
111 +}
112 +
113 +func (r *repo) Datastore() datastore.ThreadSafeDatastore {
114 + return r.datastore
115 +}
116 +
117 +func (r *repo) Blockstore() blockstore.Blockstore {
118 + return r.blockstore
119 +}
120 +
121 +func (r *repo) Exchange() exchange.Interface {
122 + return r.exchange
123 +}
124 +
125 +func MocknetTestRepo(p peer.ID, n net.Network, conf Config) RepoFactory {
126 + return func(ctx context.Context) (Repo, error) {
127 + const kWriteCacheElems = 100
128 + const alwaysSendToPeer = true
129 + dsDelay := delay.Fixed(conf.BlockstoreLatency)
130 + ds := sync.MutexWrap(datastore2.WithDelay(datastore.NewMapDatastore(), dsDelay))
131 + dhtt := dht.NewDHT(ctx, p, n, ds)
132 + bsn := bsnet.NewFromIpfsNetwork(n, dhtt)
133 + bstore, err := blockstore.WriteCached(blockstore.NewBlockstore(ds), kWriteCacheElems)
134 + if err != nil {
135 + return nil, err
136 + }
137 + exch := bitswap.New(ctx, p, bsn, bstore, alwaysSendToPeer)
138 + return &repo{
139 + bitSwapNetwork: bsn,
140 + blockstore: bstore,
141 + exchange: exch,
142 + datastore: ds,
143 + network: n,
144 + dht: dhtt,
145 + id: p,
146 + }, nil
147 + }
148 +}