2
2
3
3
const chai = require ( 'chai' )
4
4
const dirtyChai = require ( 'dirty-chai' )
5
+ const series = require ( 'async/series' )
5
6
const expect = chai . expect
6
7
const statsTests = require ( './utils/stats' )
7
8
chai . use ( dirtyChai )
8
9
const CID = require ( 'cids' )
9
10
10
11
module . exports = ( common ) => {
11
12
describe ( '.bitswap online' , ( ) => {
12
- let ipfs
13
+ let ipfsA
14
+ let ipfsB
15
+ let ipfsBId
13
16
const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR'
14
17
15
18
before ( function ( done ) {
@@ -19,38 +22,68 @@ module.exports = (common) => {
19
22
20
23
common . setup ( ( err , factory ) => {
21
24
expect ( err ) . to . not . exist ( )
22
- factory . spawnNode ( ( err , node ) => {
23
- expect ( err ) . to . not . exist ( )
24
- ipfs = node
25
- ipfs . block . get ( new CID ( key ) )
26
- . then ( ( ) => { } )
27
- . catch ( ( ) => { } )
28
- setTimeout ( done , 250 )
29
- } )
25
+ series ( [
26
+ ( cb ) => factory . spawnNode ( ( err , node ) => {
27
+ expect ( err ) . to . not . exist ( )
28
+ ipfsA = node
29
+ cb ( )
30
+ } ) ,
31
+ ( cb ) => factory . spawnNode ( ( err , node ) => {
32
+ expect ( err ) . to . not . exist ( )
33
+ ipfsB = node
34
+ cb ( )
35
+ } ) ,
36
+ ( cb ) => {
37
+ ipfsB . id ( ( err , id ) => {
38
+ expect ( err ) . to . not . exist ( )
39
+ const ipfsBAddr = id . addresses [ 0 ]
40
+ ipfsBId = id . id
41
+ ipfsA . swarm . connect ( ipfsBAddr , cb )
42
+ } )
43
+ } ,
44
+ ( cb ) => {
45
+ //Ask for a block so we can check that it shows up in our peer's wantlist
46
+ ipfsB . block . get ( new CID ( key ) )
47
+ . then ( ( ) => { } )
48
+ . catch ( ( ) => { } )
49
+ //Wait a short amount of time for the block to show up in our peer's wantlist
50
+ setTimeout ( cb , 500 )
51
+ }
52
+ ] , done )
30
53
} )
31
54
} )
32
55
33
56
after ( ( done ) => common . teardown ( done ) )
34
57
35
58
it ( '.stat' , ( done ) => {
36
59
37
- ipfs . bitswap . stat ( ( err , stats ) => {
60
+ ipfsA . bitswap . stat ( ( err , stats ) => {
61
+ expect ( err ) . to . not . exist ( )
38
62
statsTests . expectIsBitswap ( err , stats )
39
63
done ( )
40
64
} )
41
65
} )
42
66
43
67
it ( '.wantlist' , ( done ) => {
44
- ipfs . bitswap . wantlist ( ( err , list ) => {
68
+ ipfsB . bitswap . wantlist ( ( err , list ) => {
69
+ expect ( err ) . to . not . exist ( )
70
+ expect ( list [ 0 ] . cid . toBaseEncodedString ( ) ) . to . equal ( key )
71
+ done ( )
72
+ } )
73
+ } )
74
+
75
+ it ( '.wantlist peerid' , ( done ) => {
76
+ ipfsA . bitswap . wantlist ( ipfsBId , ( err , list ) => {
45
77
expect ( err ) . to . not . exist ( )
46
78
expect ( list [ 0 ] . cid . toBaseEncodedString ( ) ) . to . equal ( key )
47
79
done ( )
48
80
} )
49
81
} )
50
82
51
83
it ( '.unwant' , ( done ) => {
52
- ipfs . bitswap . unwant ( new CID ( key ) , ( err ) => {
53
- ipfs . bitswap . wantlist ( ( err , list ) => {
84
+ ipfsA . bitswap . unwant ( new CID ( key ) , ( err ) => {
85
+ ipfsA . bitswap . wantlist ( ( err , list ) => {
86
+ expect ( err ) . to . not . exist ( )
54
87
expect ( list ) . to . be . empty ( )
55
88
done ( )
56
89
} )
@@ -87,21 +120,21 @@ module.exports = (common) => {
87
120
88
121
it ( '.stat gives error while offline' , ( ) => {
89
122
ipfs . bitswap . stat ( ( err , stats ) => {
90
- expect ( err ) . to . exist ( )
123
+ expect ( err ) . to . match ( / o n l i n e m o d e / )
91
124
expect ( stats ) . to . not . exist ( )
92
125
} )
93
126
} )
94
127
95
128
it ( '.wantlist gives error if offline' , ( ) => {
96
129
ipfs . bitswap . wantlist ( ( err , list ) => {
97
- expect ( err ) . to . exist ( )
130
+ expect ( err ) . to . match ( / o n l i n e m o d e / )
98
131
expect ( list ) . to . not . exist ( )
99
132
} )
100
133
} )
101
134
102
135
it ( '.unwant gives error if offline' , ( ) => {
103
136
expect ( ( ) => ipfs . bitswap . unwant ( new CID ( key ) , ( err ) => {
104
- expect ( err ) . to . exist ( )
137
+ expect ( err ) . to . match ( / o n l i n e m o d e / )
105
138
} ) )
106
139
} )
107
140
} )
0 commit comments