Last active
January 28, 2020 07:35
-
-
Save pszabop/064fde212ef02084b9cc46155283776d to your computer and use it in GitHub Desktop.
Show that a load of a non-existant key hangs gun database, but a get does not.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const tape = require('tape') | |
const _test = require('tape-promise').default // <---- notice 'default' | |
const test = _test(tape) // decorate tape | |
const Gun = require('gun'); | |
const GunLoad = require('gun/lib/load.js'); | |
const mkdirp = require('mkdirp'); | |
const { Random } = require('random-js'); | |
const random = new Random(); | |
const _ = require('underscore'); | |
test('read a non-existant key using get', function (t) { | |
// gundb doesn't do this correctly, it errors out with a mkdir message | |
mkdirp.sync('testdb'); | |
// don't use gun's default database, else state is saved across tests, which is an anti-pattern | |
const dbfilename = 'testdb/' + random.string(16) + '.json'; | |
const gun = Gun({peers: [], multicast: false, file: dbfilename, web: undefined}); | |
const nosuchkey = 'thekey'; | |
const root = 'theroot'; | |
gun.get(root).get(nosuchkey).once(function(data, key) { | |
t.equal(data, undefined, 'non-existant key should be undefined when read with get'); | |
console.log(data); | |
t.end(); | |
}); | |
}); | |
test('read a non-existant key using load', function (t) { | |
// gundb doesn't do this correctly, it errors out with a mkdir message | |
mkdirp.sync('testdb'); | |
// don't use gun's default database, else state is saved across tests, which is an anti-pattern | |
const dbfilename = 'testdb/' + random.string(16) + '.json'; | |
const gun = Gun({peers: [], multicast: false, file: dbfilename, web: undefined}); | |
const nosuchkey = 'thekey'; | |
const root = 'theroot'; | |
gun.get(root).get(nosuchkey).load(function(data, key) { | |
console.log(key); | |
console.log(data); | |
t.equal(data, undefined, 'non-existant key should be undefined when read with load'); | |
t.end(); | |
}); | |
}); | |
// gundb doesn't unref its internal timers and also multicast | |
// so the process to exit. | |
test('force test to end', function(t) { | |
t.ok(true, 'forcing test to end with process.exit'); | |
t.end(); | |
process.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment