1
1
/* eslint-env mocha */
2
2
'use strict'
3
3
4
+ const hat = require ( 'hat' )
4
5
const waterfall = require ( 'async/waterfall' )
5
6
const { spawnNodesWithId } = require ( '../utils/spawn' )
6
7
const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
@@ -30,7 +31,6 @@ module.exports = (createCommon, options) => {
30
31
31
32
nodeA = nodes [ 0 ]
32
33
nodeB = nodes [ 1 ]
33
-
34
34
connect ( nodeA , nodeB . peerId . addresses [ 0 ] , done )
35
35
} )
36
36
} )
@@ -39,29 +39,54 @@ module.exports = (createCommon, options) => {
39
39
after ( ( done ) => common . teardown ( done ) )
40
40
41
41
it ( 'should error when getting a non-existent key from the DHT' , ( done ) => {
42
- nodeA . dht . get ( 'non-existing' , { timeout : '100ms' } , ( err , value ) => {
42
+ nodeA . dht . get ( 'non-existing' , { timeout : '100ms' } , ( err ) => {
43
43
expect ( err ) . to . be . an . instanceof ( Error )
44
44
done ( )
45
45
} )
46
46
} )
47
47
48
- it ( 'should get a value after it was put on another node' , function ( done ) {
48
+ it ( 'should get a value after it was added on another node' , function ( done ) {
49
49
this . timeout ( 80 * 1000 )
50
50
51
- // TODO - this test needs to keep tryingl instead of the setTimeout
52
51
waterfall ( [
53
- ( cb ) => nodeB . object . new ( 'unixfs-dir' , cb ) ,
52
+ ( cb ) => nodeB . object . put ( Buffer . from ( hat ( ) ) , cb ) ,
54
53
( dagNode , cb ) => setTimeout ( ( ) => cb ( null , dagNode ) , 20000 ) ,
55
54
( dagNode , cb ) => {
56
- const multihash = dagNode . toJSON ( ) . multihash
55
+ const hash = dagNode . toJSON ( ) . hash
57
56
58
- nodeA . dht . get ( multihash , cb )
57
+ nodeA . object . get ( hash , cb )
59
58
} ,
60
59
( result , cb ) => {
61
- expect ( result ) . to . eql ( '' )
60
+ expect ( result ) . to . exist ( )
62
61
cb ( )
63
62
}
64
63
] , done )
65
64
} )
65
+
66
+ it ( 'should get a value after it was put on another node' , function ( done ) {
67
+ this . timeout ( 80 * 1000 )
68
+ const multihash = Buffer . from ( '/v/hello' )
69
+ const data = Buffer . from ( 'data' )
70
+
71
+ // Rewrite validators to simply validate the record
72
+ nodeA . _libp2pNode . _dht . validators . v = nodeB . _libp2pNode . _dht . validators . v = {
73
+ func ( key , publicKey , callback ) {
74
+ setImmediate ( callback )
75
+ } ,
76
+ sign : false
77
+ }
78
+
79
+ // Rewrite selectors to select first received record
80
+ nodeA . _libp2pNode . _dht . selectors . v = ( ) => 0
81
+
82
+ waterfall ( [
83
+ ( cb ) => nodeB . dht . put ( multihash , data , cb ) ,
84
+ ( cb ) => nodeA . dht . get ( multihash , ( err , res ) => {
85
+ expect ( err ) . to . not . exist ( )
86
+ expect ( res ) . to . eql ( data )
87
+ cb ( )
88
+ } )
89
+ ] , done )
90
+ } )
66
91
} )
67
92
}
0 commit comments