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 load DHT state test

  • Loading branch information
bookmoons committed Jul 30, 2018
commit 2a3e7ee02485d163d7e4366bd00c86104945b992
@@ -9,7 +9,7 @@ var loopback = '127.0.0.1'
var localAddress = networkAddress.ipv4()
var port = 9999

test.only('Save DHT state', function (t) {
test('Save DHT state', function (t) {
t.plan(4)
var saveFile = tmp.tmpNameSync()
var dhtServer = new DHT({ bootstrap: false })
@@ -41,3 +41,40 @@ test.only('Save DHT state', function (t) {
})
})
})

test('Load DHT state', function (t) {
t.plan(4)
var saveFile = tmp.tmpNameSync()
var node = {
host: loopback,
port: port
}
var nodes = [ node ]
var dhtState = { nodes: nodes, values: {} }
var dhtStateJson = JSON.stringify(dhtState)
fs.writeFileSync(saveFile, dhtStateJson)
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: { host: localAddress },
dhtState: saveFile
})
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })
client.dht.on('ready', function handleReady () {
var dhtState = client.dht.toJSON()
var nodes = dhtState.nodes
var node = nodes[0]
t.equal(node.host, loopback)
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.