11/* eslint-env mocha */
22'use strict'
33
4+ const hat = require ( 'hat' )
45const waterfall = require ( 'async/waterfall' )
56const { spawnNodesWithId } = require ( '../utils/spawn' )
67const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
@@ -30,7 +31,6 @@ module.exports = (createCommon, options) => {
3031
3132 nodeA = nodes [ 0 ]
3233 nodeB = nodes [ 1 ]
33-
3434 connect ( nodeA , nodeB . peerId . addresses [ 0 ] , done )
3535 } )
3636 } )
@@ -39,29 +39,54 @@ module.exports = (createCommon, options) => {
3939 after ( ( done ) => common . teardown ( done ) )
4040
4141 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 ) => {
4343 expect ( err ) . to . be . an . instanceof ( Error )
4444 done ( )
4545 } )
4646 } )
4747
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 ) {
4949 this . timeout ( 80 * 1000 )
5050
51- // TODO - this test needs to keep tryingl instead of the setTimeout
5251 waterfall ( [
53- ( cb ) => nodeB . object . new ( 'unixfs-dir' , cb ) ,
52+ ( cb ) => nodeB . object . put ( Buffer . from ( hat ( ) ) , cb ) ,
5453 ( dagNode , cb ) => setTimeout ( ( ) => cb ( null , dagNode ) , 20000 ) ,
5554 ( dagNode , cb ) => {
5655 const multihash = dagNode . toJSON ( ) . multihash
5756
58- nodeA . dht . get ( multihash , cb )
57+ nodeA . object . get ( multihash , cb )
5958 } ,
6059 ( result , cb ) => {
61- expect ( result ) . to . eql ( '' )
60+ expect ( result ) . to . exist ( )
6261 cb ( )
6362 }
6463 ] , done )
6564 } )
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+ } )
6691 } )
6792}
0 commit comments