gun/test/panic/axe/1no_self.js
Martti Malmi 7123207c66
Panic tests for rod & radix.js try / catch (#1269)
* try/catch radix.js

* rod test/panic/chat.js

rod test/panic/holy-grail.js

rod panic tests
2022-08-13 11:58:00 -07:00

128 lines
3.7 KiB
JavaScript

/*
It is possible to connect to yourself, which just wastes bandwidth, so this test checks that we disconnect from ourselves.
*/
// <-- PANIC template, copy & paste, tweak a few settings if needed...
var ip; try{ ip = require('ip').address() }catch(e){}
var config = {
IP: ip || 'localhost',
port: 8765,
relays: 1
}
var panic; try{ panic = require('panic-server') } catch(e){ console.log("PANIC not installed! `npm install panic-server panic-manager panic-client`") }
panic.server().on('request', function(req, res){
//config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res);
}).listen(config.port);
var clients = panic.clients;
var manager = require('panic-manager')();
manager.start({
clients: Array(config.relays).fill().map(function(u, i){
return {
type: 'node',
port: config.port + (i + 1)
}
}),
panic: 'http://' + config.IP + ':' + config.port
});
var relays = clients.filter('Node.js');
var alice = relays.pluck(1);
// continue boiler plate, tweak a few defaults if needed, but give descriptive test names...
describe("Do not connect to self", function(){
//this.timeout(5 * 60 * 1000);
this.timeout(10 * 60 * 1000);
it("Relays have joined!", function(){
return relays.atLeast(config.relays);
});
it("GUN started!", function(){
var tests = [], i = 0;
relays.each(function(client){
tests.push(client.run(function(test){
var env = test.props;
test.async();
try{ require('fs').unlinkSync(env.i+'data') }catch(e){}
try{ require('gun/lib/fsrm')(env.i+'data') }catch(e){}
var server = require('http').createServer(function(req, res){
res.end("I am "+ env.i +"!");
});
var port = env.config.port + env.i;
var Gun; try{ Gun = require('gun') }catch(e){ console.log("GUN not found! You need to link GUN to PANIC. Nesting the `gun` repo inside a `node_modules` parent folder often fixes this.") }
var peers = [], i = env.config.relays;
global.self_url = 'http://'+ env.config.IP + ':' + port + '/gun';
// make sure to connect to self/same.
peers.push(self_url);
console.log(port, " connect to ", peers);
if (process.env.ROD_PATH) {
console.log('testing with rod');
var args = ['start', '--port', port, '--sled-storage=false'];
if (peers.length) {
args.push('--peers=' + peers.join(',').replaceAll('http', 'ws'));
}
const sp = require('child_process').spawn(process.env.ROD_PATH, args);
sp.stdout.on('data', function(data){
console.log(data.toString());
});
sp.stderr.on('data', function(data){
console.log(data.toString());
});
test.done();
return;
}
var gun = Gun({file: env.i+'data', peers: peers, web: server, multicast: false});
global.gun = gun;
server.listen(port, function(){
test.done();
});
}, {i: i += 1, config: config}));
});
return Promise.all(tests);
});
// end PANIC template -->
it("Drop self", function(){
var tests = [], i = 0;
relays.each(function(client){
tests.push(client.run(function(test){
var env = test.props;
test.async();
var peers = gun.back('opt.peers');
var peer = peers[self_url];
gun.get('test').on(function(a){ });
setTimeout(function(){
if(peers[self_url] || peer.wire){
console.log("FAIL: should_not_have_self_anymore");
should_not_have_self_anymore;
return;
}
test.done();
},99);
}, {i: i += 1, config: config}));
});
return Promise.all(tests);
});
it("All finished!", function(done){
console.log("Done! Cleaning things up...");
setTimeout(function(){
done();
},1);
});
after("Everything shut down.", function(){
require('../util/open').cleanup();
return relays.run(function(){
process.exit();
});
});
});