@cryptotaxi247 / kubo / commits / 81a3ba067

tests(bitswap) share constructor between tests

@whyrusleeping i hope this makes it a bit easier to work with tests License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>

Brian Tiger Chow committed Nov 19, 2014 at 22:51 UTC 81a3ba067720496d816aa91cc8fecf5eaa5776f9
3 files changed +15 -24
core/core.go
+5 -3
@@ -9,6 +9,7 @@ import (
9 ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
10
11 bserv "github.com/jbenet/go-ipfs/blockservice"
12 + blockstore "github.com/jbenet/go-ipfs/blockstore"
13 config "github.com/jbenet/go-ipfs/config"
14 diag "github.com/jbenet/go-ipfs/diagnostics"
15 exchange "github.com/jbenet/go-ipfs/exchange"
@@ -28,8 +29,7 @@ import (
29 dht "github.com/jbenet/go-ipfs/routing/dht"
30 u "github.com/jbenet/go-ipfs/util"
31 ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
31 - "github.com/jbenet/go-ipfs/util/debugerror"
32 - "github.com/jbenet/go-ipfs/util/eventlog"
32 + debugerror "github.com/jbenet/go-ipfs/util/debugerror"
33 )
34
35 const IpnsValidatorTag = "ipns"
@@ -169,7 +169,9 @@ func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
169 // setup exchange service
170 const alwaysSendToPeer = true // use YesManStrategy
171 bitswapNetwork := bsnet.NewFromIpfsNetwork(exchangeService, n.Network)
172 - n.Exchange = bitswap.New(ctx, n.Identity, bitswapNetwork, n.Routing, n.Datastore, alwaysSendToPeer)
172 + bstore := blockstore.NewBlockstore(n.Datastore)
173 +
174 + n.Exchange = bitswap.New(ctx, n.Identity, bitswapNetwork, n.Routing, bstore, alwaysSendToPeer)
175
176 go initConnections(ctx, n.Config, n.Peerstore, dhtRouting)
177 }
exchange/bitswap/bitswap.go
+3 -5
@@ -7,7 +7,6 @@ import (
7 "time"
8
9 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
10 - ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
10
11 blocks "github.com/jbenet/go-ipfs/blocks"
12 blockstore "github.com/jbenet/go-ipfs/blockstore"
@@ -27,9 +26,8 @@ var log = eventlog.Logger("bitswap")
26 // provided BitSwapNetwork. This function registers the returned instance as
27 // the network delegate.
28 // Runs until context is cancelled
30 -func New(ctx context.Context, p peer.Peer,
31 - network bsnet.BitSwapNetwork, routing bsnet.Routing,
32 - d ds.ThreadSafeDatastore, nice bool) exchange.Interface {
29 +func New(ctx context.Context, p peer.Peer, network bsnet.BitSwapNetwork, routing bsnet.Routing,
30 + bstore blockstore.Blockstore, nice bool) exchange.Interface {
31
32 notif := notifications.New()
33 go func() {
@@ -38,7 +36,7 @@ func New(ctx context.Context, p peer.Peer,
36 }()
37
38 bs := &bitswap{
41 - blockstore: blockstore.NewBlockstore(d),
39 + blockstore: bstore,
40 notifications: notif,
41 strategy: strategy.New(nice),
42 routing: routing,
exchange/bitswap/bitswap_test.go
+7 -16
@@ -11,14 +11,12 @@ import (
11 ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
12 ds_sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
13 blocks "github.com/jbenet/go-ipfs/blocks"
14 + blockstore "github.com/jbenet/go-ipfs/blockstore"
15 bstore "github.com/jbenet/go-ipfs/blockstore"
16 exchange "github.com/jbenet/go-ipfs/exchange"
16 - notifications "github.com/jbenet/go-ipfs/exchange/bitswap/notifications"
17 - strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy"
17 tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
18 peer "github.com/jbenet/go-ipfs/peer"
19 mock "github.com/jbenet/go-ipfs/routing/mock"
21 - util "github.com/jbenet/go-ipfs/util"
20 )
21
22 func TestGetBlockTimeout(t *testing.T) {
@@ -335,23 +333,16 @@ func session(net tn.Network, rs mock.RoutingServer, id peer.ID) instance {
333
334 adapter := net.Adapter(p)
335 htc := rs.Client(p)
336 + bstore := blockstore.NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
337
339 - blockstore := bstore.NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
338 const alwaysSendToPeer = true
341 - bs := &bitswap{
342 - blockstore: blockstore,
343 - notifications: notifications.New(),
344 - strategy: strategy.New(alwaysSendToPeer),
345 - routing: htc,
346 - sender: adapter,
347 - wantlist: util.NewKeySet(),
348 - batchRequests: make(chan []util.Key, 32),
349 - }
350 - adapter.SetDelegate(bs)
351 - go bs.run(context.TODO())
339 + ctx := context.TODO()
340 +
341 + bs := New(ctx, p, adapter, htc, bstore, alwaysSendToPeer)
342 +
343 return instance{
344 peer: p,
345 exchange: bs,
355 - blockstore: blockstore,
346 + blockstore: bstore,
347 }
348 }