update on-recovery PANIC test to run default & radisk configurations (#1078)

This commit is contained in:
nsreed 2021-06-16 14:56:34 -04:00 committed by GitHub
parent fc10c250c9
commit 10b42525ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,9 @@
// visible/active throughout the tests, because an inactive tab will NOT // visible/active throughout the tests, because an inactive tab will NOT
// reconnect to the relay peer until the tab is active again. (Chrome 83) // reconnect to the relay peer until the tab is active again. (Chrome 83)
// Uses a pattern described in https://stackoverflow.com/a/39286581/13564512
// to run multiple configurations of a mocha test.
var config = { var config = {
IP: require('ip').address(), IP: require('ip').address(),
port: 8765, port: 8765,
@ -16,111 +19,163 @@ var config = {
route: { route: {
'/': __dirname + '/index.html', '/': __dirname + '/index.html',
'/gun.js': __dirname + '/../../gun.js', '/gun.js': __dirname + '/../../gun.js',
'/jquery.js': __dirname + '/../../examples/jquery.js' '/jquery.js': __dirname + '/../../examples/jquery.js',
'/radix.js': __dirname + '/../../lib/radix.js',
'/radisk.js': __dirname + '/../../lib/radisk.js',
'/store.js': __dirname + '/../../lib/store.js',
'/rindexed.js': __dirname + '/../../lib/rindexed.js'
},
/** Test variants to run */
variants: ['default', 'radisk'],
/** Configuration details for each variant. If unspecified, variant will run with no additional configuration. */
variantConfigs: {
radisk: {
/** Options to pass the Gun constructor */
opts: {
localStorage: false
},
/** Libraries to import before constructing Gun */
imports: [
'radix.js',
'radisk.js',
'store.js',
'rindexed.js'
]
} }
} }
};
var {loadBrowserScripts} = require('./util/load-browser-scripts');
var panic = require('panic-server'); var panic = require('panic-server');
panic.server().on('request', function(req, res){ panic.server().on('request', function (req, res) {
config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res); config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res);
}).listen(config.port); }).listen(config.port);
var clients = panic.clients;
var manager = require('panic-manager')(); var manager = require('panic-manager')();
manager.start({ var clients = panic.clients;
clients: Array(config.servers).fill().map(function(u, i){ var servers = clients.filter('Node.js');
/** The first relay peer */
var bob = servers.pluck(1);
/** The second relay peer */
var carl = servers.excluding(bob).pluck(1);
var browsers = clients.excluding(servers);
/** The "sending" browser */
var alice = browsers.pluck(1);
/** The "receiving" browser */
var dave = browsers.excluding(alice).pluck(1);
// Describe the test itself
describe("gun.on should receive updates after crashed relay peer comes back online", function () {
this.timeout(10 * 1000);
config.variants.forEach((variant) => {
var variantConfig = config.variantConfigs[variant] || {};
// Describe the variant
describe(`with ${variant} plugin configuration`, function () {
before('PANIC manager setup servers', function () {
// we are terminating the gun servers after each test variant, so we need to start them up again before each test variant
manager.start({
clients: Array(config.servers).fill().map(function (u, i) {
return { return {
type: 'node', type: 'node',
port: config.port + (i + 1) port: config.port + (i + 1)
} }
}), }),
panic: 'http://' + config.IP + ':' + config.port panic: 'http://' + config.IP + ':' + config.port
}); });
});
var servers = clients.filter('Node.js'); before("Servers have joined!", function () {
var bob = servers.pluck(1);
var carl = servers.excluding(bob).pluck(1);
var browsers = clients.excluding(servers);
var alice = browsers.pluck(1);
var dave = browsers.excluding(alice).pluck(1);
describe("gun.on should receive updates after crashed relay peer comes back online", function(){
this.timeout(10 * 60 * 1000);
it("Servers have joined!", function(){
return servers.atLeast(config.servers); return servers.atLeast(config.servers);
}); });
it("GUN started!", function(){ it("GUN started!", function () {
return bob.run(function(test){ return bob.run(function (test) {
var env = test.props; var env = test.props;
var filepath = env.dir + '/data'; var filepath = env.dir + '/data';
test.async(); test.async();
var fs = require('fs'); var fs = require('fs');
try { if (fs.existsSync(filepath)) { fs.rmdirSync(filepath, { recursive: true }); } } catch (e) { console.error(e); test.fail(''); } try {if (fs.existsSync(filepath)) {fs.rmdirSync(filepath, {recursive: true});} } catch (e) {console.error(e); test.fail('');}
var server = require('http').createServer(function(req, res){ var server = require('http').createServer(function (req, res) {
res.end("I AM BOB"); res.end("I AM BOB");
}); });
var port = env.config.port + 1; var port = env.config.port + 1;
try { var Gun = require(env.dir + '/../../index.js'); } catch(e) { console.error(e); test.fail(''); } try {var Gun = require(env.dir + '/../../index.js');} catch (e) {console.error(e); test.fail('');}
var gun = Gun({file: filepath, web: server}); var gun = Gun({file: filepath, web: server});
server.listen(port, function(){ server.listen(port, function () {
test.done(); test.done();
}); });
}, {config: config, dir: __dirname}); }, {config: config, dir: __dirname});
}); });
it(config.browsers +" browser(s) have joined!", function(){ it(config.browsers + " browser(s) have joined!", function () {
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); require('./util/open').web(config.browsers, "http://" + config.IP + ":" + config.port, {
headless: true,
});
return browsers.atLeast(config.browsers); return browsers.atLeast(config.browsers);
}); });
it("Browsers initialized gun!", function(){ it(`Browsers loaded ${variant} plugin libraries`, function () {
return loadBrowserScripts(browsers, variantConfig.imports);
});
it("Browsers initialized gun!", function () {
var tests = [], i = 0; var tests = [], i = 0;
browsers.each(function(client, id){ browsers.each(function (client, id) {
tests.push(client.run(function(test){ tests.push(client.run(function (test) {
try{ localStorage.clear() }catch(e){} try {localStorage.clear()} catch (e) { }
try{ indexedDB.deleteDatabase('radata') }catch(e){} try {indexedDB.deleteDatabase('radata')} catch (e) { }
var env = test.props; var env = test.props;
var gun = Gun('http://'+ env.config.IP + ':' + (env.config.port + 1) + '/gun'); var configOpts = env.variantConfig.opts || {};
var opts = {
peers: ['http://' + env.config.IP + ':' + (env.config.port + 1) + '/gun'],
...configOpts
};
console.log('using constructor options: ', JSON.stringify(opts));
var gun = Gun(opts);
window.ref = gun.get('a'); window.ref = gun.get('a');
}, {i: i += 1, config: config})); }, {i: i += 1, config: config, variantConfig: variantConfig}));
}); });
return Promise.all(tests); return Promise.all(tests);
}); });
it("Dave subscribed to updates using gun.on()", function(){ it("Dave subscribed to updates using gun.on()", function () {
return dave.run(function(test){ return dave.run(function (test) {
console.log("I AM DAVE"); console.log("I AM DAVE");
test.async(); test.async();
ref.on(function(data){ ref.on(function (data) {
console.log("Just received data: ", data); console.log("Just received data: ", JSON.stringify(data));
if(data.hello === 'world') { window.receivedFirst = true; } if (data.hello === 'world') {window.receivedFirst = true;}
if(data.foo === 'bar') { window.receivedSecond = true; } if (data.foo === 'bar') {window.receivedSecond = true;}
}); });
test.done(); test.done();
}); });
}); });
it("Alice put first data", function(){ it("Alice put first data", function () {
return alice.run(function(test){ return alice.run(function (test) {
console.log("I AM ALICE"); console.log("I AM ALICE");
test.async(); test.async();
ref.put({hello: 'world'}, function(ack){ ref.put({hello: 'world'}, function (ack) {
if(!ack.err) { if (!ack.err) {
test.done(); test.done();
} else {
console.log('ALICE WAS UNABLE TO PUT DATA!');
test.fail();
} }
}); });
}); });
}); });
it("Dave received first data", function(){ it("Dave received first data", function () {
return dave.run(function(test){ return dave.run(function (test) {
test.async(); test.async();
var myInterval; var myInterval;
myInterval = setInterval(function() { myInterval = setInterval(function () {
if(window.receivedFirst) { if (window.receivedFirst) {
clearInterval(myInterval); clearInterval(myInterval);
test.done(); test.done();
} }
@ -128,22 +183,22 @@ describe("gun.on should receive updates after crashed relay peer comes back onli
}); });
}); });
it("Killed relay peer", function(){ it("Killed relay peer", function () {
return bob.run(function(test){ return bob.run(function (test) {
test.async(); test.async();
process.exit(); process.exit();
}); });
}); });
it("Waited 1 second", function(done){ it("Waited 1 second", function (done) {
setTimeout(done, 1000); setTimeout(done, 1000);
}); });
it("Alice put second data", function(){ it("Alice put second data", function () {
return alice.run(function(test){ return alice.run(function (test) {
test.async(); test.async();
ref.put({foo: 'bar'}, function(ack){ ref.put({foo: 'bar'}, function (ack) {
if(!ack.err) { if (!ack.err) {
test.done(); test.done();
} }
}); });
@ -151,43 +206,43 @@ describe("gun.on should receive updates after crashed relay peer comes back onli
}); });
// FIXME: Don't copy paste the entire block!! // FIXME: Don't copy paste the entire block!!
it("Restored relay peer", function(){ it("Restored relay peer", function () {
return carl.run(function(test){ return carl.run(function (test) {
var env = test.props; var env = test.props;
var filepath = env.dir + '/data'; var filepath = env.dir + '/data';
test.async(); test.async();
var fs = require('fs'); var fs = require('fs');
try { if (fs.existsSync(filepath)) { fs.rmdirSync(filepath, { recursive: true }); } } catch (e) { console.error(e); test.fail(''); } try {if (fs.existsSync(filepath)) {fs.rmdirSync(filepath, {recursive: true});} } catch (e) {console.error(e); test.fail('');}
var server = require('http').createServer(function(req, res){ var server = require('http').createServer(function (req, res) {
res.end("I AM CARL"); res.end("I AM CARL");
}); });
var port = env.config.port + 1; var port = env.config.port + 1;
try { var Gun = require(env.dir + '/../../index.js'); } catch(e) { console.error(e); test.fail(''); } try {var Gun = require(env.dir + '/../../index.js');} catch (e) {console.error(e); test.fail('');}
var gun = Gun({file: filepath, web: server}); var gun = Gun({file: filepath, web: server});
server.listen(port, function(){ server.listen(port, function () {
test.done(); test.done();
}); });
}, {config: config, dir: __dirname}); }, {config: config, dir: __dirname});
}); });
it("Browsers reconnected", function() { it("Browsers reconnected", function () {
var tests = [], i = 0; var tests = [], i = 0;
browsers.each(function(client, id){ browsers.each(function (client, id) {
tests.push(client.run(function(test){ tests.push(client.run(function (test) {
test.async(); test.async();
var config = test.props.config; var config = test.props.config;
var seconds = 15; var seconds = 15;
var timeout = Date.now() + seconds * 1000; var timeout = Date.now() + seconds * 1000;
var url = "http://"+ config.IP +":"+ (config.port+1) +"/gun"; var url = "http://" + config.IP + ":" + (config.port + 1) + "/gun";
var peers = ref.back(1)._.opt.peers; var peers = ref.back(1)._.opt.peers;
var i; var i;
i = setInterval(function() { i = setInterval(function () {
if(peers[url] && peers[url].wire.readyState === 1) { if (peers[url] && peers[url].wire.readyState === 1) {
clearInterval(i); clearInterval(i);
test.done(); test.done();
return; return;
} }
if(Date.now() >= timeout) { if (Date.now() >= timeout) {
test.fail('Timed out after ' + seconds + ' seconds'); test.fail('Timed out after ' + seconds + ' seconds');
return; return;
} }
@ -197,18 +252,18 @@ describe("gun.on should receive updates after crashed relay peer comes back onli
return Promise.all(tests); return Promise.all(tests);
}); });
it("Dave received second data", function(){ it("Dave received second data", function () {
return dave.run(function(test){ return dave.run(function (test) {
test.async(); test.async();
var seconds = 60; var seconds = 60;
var timeout = Date.now() + seconds * 1000; var timeout = Date.now() + seconds * 1000;
var i; var i;
i = setInterval(function() { i = setInterval(function () {
if(window.receivedSecond) { if (window.receivedSecond) {
test.done(); test.done();
return; return;
} }
if(Date.now() >= timeout) { if (Date.now() >= timeout) {
test.fail('Timed out after ' + seconds + ' seconds'); test.fail('Timed out after ' + seconds + ' seconds');
return; return;
} }
@ -216,9 +271,17 @@ describe("gun.on should receive updates after crashed relay peer comes back onli
}); });
}); });
after("Everything shut down.", function(){ it('Closed browsers & servers', function () {
return require('./util/open').cleanup() || bob.run(function(){ var promises = [];
promises.push(bob.run(function () {
process.exit(); process.exit();
}));
promises.push(carl.run(function () {
process.exit();
}));
promises.push(require('./util/open').cleanup());
return Promise.all(promises);
});
}); });
}); });
}); });