Skip to content

Instantly share code, notes, and snippets.

@pszabop
Last active January 28, 2020 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pszabop/064fde212ef02084b9cc46155283776d to your computer and use it in GitHub Desktop.
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.
'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