Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist DHT nodes #1431

Open
wants to merge 9 commits into
base: master
from

Add save DHT state test

  • Loading branch information
bookmoons committed Jul 30, 2018
commit c2f594385e7c0f27a4a373ebdd1adf632819cb58
@@ -86,6 +86,7 @@
"serve-static": "^1.11.1",
"standard": "*",
"tape": "^4.6.0",
"tmp": "0.0.33",
"webtorrent-fixtures": "^1.5.0"
},
"engines": {
@@ -0,0 +1,41 @@
var test = require('tape')
var tmp = require('tmp')
var fs = require('fs')
var DHT = require('bittorrent-dht/server')
var WebTorrent = require('../../')

var localHost = '127.0.0.1'
var port = 9999

test('Save DHT state', function (t) {
t.plan(4)
var saveFile = tmp.tmpNameSync()
var dhtServer = new DHT({ bootstrap: false })
dhtServer.on('error', function (err) { t.fail(err) })
dhtServer.on('warning', function (err) { t.fail(err) })
dhtServer.listen(port, function handleServerListening () {
var client = new WebTorrent({
dht: { bootstrap: false },
dhtState: saveFile
})
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })
client.dht.addNode({ host: localHost, port: port })
client.dht.on('node', function handleNodeAdded () {
client.saveDhtState(function handleDhtStateSaved () {
var dhtStateJson = fs.readFileSync(saveFile)
var dhtState = JSON.parse(dhtStateJson)
var nodes = dhtState.nodes
var node = nodes[0]
t.equal(node.host, localHost)
t.equal(node.port, port)
client.destroy(function handleClientDestroyed (err) {
t.error(err, 'client destroyed')
})
dhtServer.destroy(function handleDhtServerDestroyed (err) {
t.error(err, 'dht server destroyed')
})
})
})
})
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.