Merge branch 'master' of github.com:amark/gun

This commit is contained in:
Jussi Rytkönen 2020-11-10 13:14:11 +02:00
commit cf5c00d82c
32 changed files with 1502 additions and 109 deletions

View File

@ -5,7 +5,10 @@
}
var fs = require('fs');
var config = { port: process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765 };
var config = {
port: process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765,
peers: process.env.PEERS && process.env.PEERS.split(',') || []
};
var Gun = require('../'); // require('gun')
if(process.env.HTTPS_KEY){
@ -16,8 +19,8 @@
config.server = require('http').createServer(Gun.serve(__dirname));
}
var gun = Gun({web: config.server.listen(config.port)});
var gun = Gun({web: config.server.listen(config.port), peers: config.peers});
console.log('Relay peer started on port ' + config.port + ' with /gun');
module.exports = gun;
}());
}());

View File

@ -0,0 +1,116 @@
const DEFAULT_OPTIONS = {
size: 20,
stickTo: 'top',
};
class ScrollWindow {
constructor(gunNode, opts = {}) {
this.opts = Object.assign(DEFAULT_OPTIONS, opts);
this.elements = new Map();
this.node = gunNode;
this.center = this.opts.startAt;
this.updateSubscriptions();
}
updateSubscriptions() {
this.upSubscription && this.upSubscription.off();
this.downSubscription && this.downSubscription.off();
const subscribe = params => {
this.node.get({ '.': params}).map().on((val, key, a, eve) => {
if (params['-']) {
this.downSubscription = eve;
} else {
this.upSubscription = eve;
}
this._addElement(key, val);
});
};
if (this.center) {
subscribe({ '>': this.center, '<': '\uffff' });
subscribe({'<': this.center, '>' : '', '-': true});
} else {
subscribe({ '<': '\uffff', '>': '', '-': this.opts.stickTo === 'top' });
}
}
_getSortedKeys() {
this.sortedKeys = this.sortedKeys || [...this.elements.keys()].sort();
return this.sortedKeys;
}
_upOrDown(n, up) {
this.opts.stickTo = null;
const keys = this._getSortedKeys();
n = n || (keys.length / 2);
n = up ? n : -n;
const half = Math.floor(keys.length / 2);
const newMiddleIndex = Math.max(Math.min(half + n, keys.length - 1), 0);
if (this.center !== keys[newMiddleIndex]) {
this.center = keys[newMiddleIndex];
this.updateSubscriptions();
}
return this.center;
}
up(n) {
return this._upOrDown(n, true);
}
down(n) {
return this._upOrDown(n, false);
}
_topOrBottom(top) {
this.opts.stickTo = top ? 'top' : 'bottom';
this.center = null;
this.updateSubscriptions();
}
top() {
this._topOrBottom(true);
}
bottom() {
this._topOrBottom(false);
}
_addElement(key, val) {
if (!val || this.elements.has(key)) return;
const add = () => {
this.elements.set(key, val);
this.sortedKeys = [...this.elements.keys()].sort();
const sortedElements = this.sortedKeys.map(k => this.elements.get(k));
this.opts.onChange && this.opts.onChange(sortedElements);
};
const keys = this._getSortedKeys();
if (keys.length < this.opts.size) {
add();
} else {
if (this.opts.stickTo === 'top' && key > keys[0]) {
this.elements.delete(keys[0]);
add();
} else if (this.opts.stickTo === 'bottom' && key < keys[keys.length - 1]) {
this.elements.delete(keys[keys.length - 1]);
add();
} else if (this.center) {
if (keys.indexOf(this.center) < (keys.length / 2)) {
if (key < keys[keys.length - 1]) {
this.elements.delete(keys[keys.length - 1]);
add();
}
} else {
if (key > keys[0]) {
delete this.elements.delete(keys[0]);
add();
}
}
}
}
}
getElements() {
return this.elements;
}
}

View File

@ -0,0 +1,30 @@
<html>
<head>
<title>Infinite scroll example</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
<link rel="stylesheet" type="text/css" href="./style.css">
<script src="/jquery.js"></script>
<script src="/gun.js"></script>
<script src="./ScrollWindow.js"></script>
</head>
<body>
<header>
<form id="generate">
<input type="text" id="number" placeholder="Number of posts"/>
<button>Generate</button>
</form>
<div id="top-buttons">
<button id="top">Top</button>
</div>
</header>
<div id="top-sentinel" style="padding-top: 0px;"></div>
<div id="container"></div>
<div id="bottom-sentinel" style="padding-top: 0px;"></div>
<button id="bottom">Bottom</button>
<script src="./index.js"></script>
</body>
</html>

View File

@ -0,0 +1,167 @@
const gun = new Gun();
const size = 20;
const gunNode = gun.get('posts');
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
let topSentinelPreviousY = 0;
let topSentinelPreviousRatio = 0;
let bottomSentinelPreviousY = 0;
let bottomSentinelPreviousRatio = 0;
let previousUpIndex = previousDownIndex = -1;
const render = elements => {
const t = new Date();
elements.reverse().forEach((data, j) => {
var date = new Date(data.date);
$('#date' + j).text(date.toLocaleDateString() + ' ' + date.toLocaleTimeString());
$('#text' + j).text(data.text);
$('#img' + j).attr('src', '');
$('#img' + j).attr('src', _getCatImg(date.getTime()));
$('#post' + j).css({visibility: 'visible'});
});
console.log('rendering took', new Date().getTime() - t.getTime(), 'ms');
window.onRender && window.onRender(elements);
};
const onChange = debounce(render, 20);
const scroller = new ScrollWindow(gunNode, {size, stickTo: 'top', onChange});
const initList = () => {
for (var n = 0; n < size; n++) {
var el = $("<div>").addClass('post').attr('id', 'post' + n).css({visibility: 'hidden'});
el.append($('<b>').attr('id', 'date' + n));
el.append($('<span>').attr('id', 'text' + n));
el.append($('<img>').attr('id', 'img' + n).attr('height', 100).attr('width', 100));
$('#container').append(el);
}
}
const _getCatImg = (n) => {
const url = "https://source.unsplash.com/collection/139386/100x100/?sig=";
return url + n % 999999;
};
const getNumFromStyle = numStr => Number(numStr.substring(0, numStr.length - 2));
const adjustPaddings = isScrollDown => {
const container = document.getElementById("container");
const currentPaddingTop = getNumFromStyle(container.style.paddingTop);
const currentPaddingBottom = getNumFromStyle(container.style.paddingBottom);
const remPaddingsVal = 198 * (size / 2); // TODO: calculate actual element heights
if (isScrollDown) {
container.style.paddingTop = currentPaddingTop + remPaddingsVal + "px";
container.style.paddingBottom = currentPaddingBottom === 0 ? "0px" : currentPaddingBottom - remPaddingsVal + "px";
} else {
container.style.paddingBottom = currentPaddingBottom + remPaddingsVal + "px";
if (currentPaddingTop === 0) {
$(window).scrollTop($('#post0').offset().top + remPaddingsVal);
} else {
container.style.paddingTop = currentPaddingTop - remPaddingsVal + "px";
}
}
}
const topSentCallback = entry => {
const container = document.getElementById("container");
const currentY = entry.boundingClientRect.top;
const currentRatio = entry.intersectionRatio;
const isIntersecting = entry.isIntersecting;
// conditional check for Scrolling up
if (
currentY > topSentinelPreviousY &&
isIntersecting &&
currentRatio >= topSentinelPreviousRatio &&
scroller.center !== previousUpIndex && // stop if no new results were received
scroller.opts.stickTo !== 'top'
) {
previousUpIndex = scroller.center;
adjustPaddings(false);
scroller.up(size / 2);
}
topSentinelPreviousY = currentY;
topSentinelPreviousRatio = currentRatio;
}
const botSentCallback = entry => {
const currentY = entry.boundingClientRect.top;
const currentRatio = entry.intersectionRatio;
const isIntersecting = entry.isIntersecting;
// conditional check for Scrolling down
if (
currentY < bottomSentinelPreviousY &&
currentRatio > bottomSentinelPreviousRatio &&
isIntersecting &&
scroller.center !== previousDownIndex && // stop if no new results were received
scroller.opts.stickTo !== 'bottom'
) {
previousDownIndex = scroller.center;
adjustPaddings(true);
scroller.down(size / 2);
}
bottomSentinelPreviousY = currentY;
bottomSentinelPreviousRatio = currentRatio;
}
const initIntersectionObserver = () => {
const options = {
//rootMargin: '190px',
}
const callback = entries => {
entries.forEach(entry => {
if (entry.target.id === 'post0') {
topSentCallback(entry);
} else if (entry.target.id === `post${size - 1}`) {
botSentCallback(entry);
}
});
}
var observer = new IntersectionObserver(callback, options); // TODO: It's possible to quickly scroll past the sentinels without them firing. Top and bottom sentinels should extend to page top & bottom?
observer.observe(document.querySelector("#post0"));
observer.observe(document.querySelector(`#post${size - 1}`));
}
initList(size);
initIntersectionObserver();
$('#top').click(() => {
scroller.top();
$('#container').css({'padding-top': 0, 'padding-bottom': 0});
$(document.body).animate({ scrollTop: 0 }, 500);
});
$('#bottom').click(() => {
scroller.bottom();
$('#container').css({'padding-top': 0, 'padding-bottom': 0});
$(document.body).animate({ scrollTop: $("#container").height() }, 500);
});
$('#generate').submit(e => {
e.preventDefault();
const day = 24 * 60 * 60 * 1000;
const year = 365 * day;
const n = Number($('#number').val());
for (let i = 0; i < n; i++) {
const d = new Date(40 * year + i * day).toISOString();
gunNode.get(d).put({text: 'Hello world!', date: d});
}
});

View File

@ -0,0 +1,77 @@
html, body {
margin: 0;
padding: 0;
}
body {
padding-top: 65px;
}
header {
background: rgba(255,255,255,0.75);
padding: 15px;
}
header {
position: fixed;
top: 0; left: 0; right: 0;
}
#bottom {
position: fixed;
right: 15px;
bottom: 15px;
}
input {
border: 0;
background-color: #efefef;
padding: 15px;
border-radius: 50px;
}
button {
background-color: #4a4f9d;
border: 0;
padding: 15px;
color: white;
border-radius: 50px;
}
input, button {
outline: none;
opacity: 1;
}
input:focus, button:focus, button:hover {
opacity: 0.8;
}
button:hover {
cursor: pointer;
}
#top-buttons, form {
display: inline-block;
margin: 0;
}
#top-buttons {
float: right;
}
.post {
margin: 15px;
padding: 15px;
background-color: #9de1fe;
border-radius: 5px;
}
.post b {
margin-right: 5;
}
.post img {
display: block;
margin: 10px 0;
}

4
gun.js
View File

@ -1349,6 +1349,10 @@
Gun.chain.get = function(key, cb, as){
var gun, tmp;
if(typeof key === 'string'){
if(key.length == 0) {
(as = this.chain())._.err = {err: Gun.log('Invalid zero length string key!', key)};
return null
}
var back = this, cat = back._;
var next = cat.next || empty;
if(!(gun = next[key])){

96
lib/level.js Normal file
View File

@ -0,0 +1,96 @@
// CAUTION: This adapter does NOT handle encoding. an encoding mechanism like the encoding-down package will need to be included
// Based on localStorage adapter in 05349a5
var Gun = ('undefined' !== typeof window) ? window.Gun : require('../gun');
var debug = false;
Gun.on('opt', function(ctx) {
var opt = ctx.opt;
var ev = this.to;
if (debug) debug.emit('create');
if (ctx.once) return ev.next(ctx);
// Check if the given 'level' argument implements all the components we need
// Intentionally doesn't check for levelup explicitly, to allow different handlers implementing the same api
if (
(!opt.level) ||
('object' !== typeof opt.level) ||
('function' !== typeof opt.level.get) ||
('function' !== typeof opt.level.put)
) {
return;
}
ctx.on('put', function(msg) {
this.to.next(msg);
// Extract data from message
var put = msg.put;
var soul = put['#'];
var key = put['.'];
var val = put[':'];
var state = put['>'];
if (debug) debug.emit('put', soul, val);
// Fetch previous version
opt.level.get(soul, function(err, data) {
if (err && (err.name === 'NotFoundError')) err = undefined;
if (debug && err) debug.emit('error', err);
if (err) return;
// Unclear required transformation
data = Gun.state.ify(data, key, state, val, soul);
// Write into storage
opt.level.put(soul, data, function(err) {
if (err) return;
if (debug) debug.emit('put', soul, val);
// Bail if message was an ack
if (msg['@']) return;
// Send ack back
ctx.on('in', {
'@' : msg['@'],
ok : 0,
});
});
});
});
ctx.on('get', function(msg) {
this.to.next(msg);
// Extract soul from message
var lex = msg.get;
if (!lex || !(soul = lex['#'])) return;
var has = lex['.'];
if (debug) debug.emit('get', soul);
// Fetch data from storage
opt.level.get(soul, function(err, data) {
if (err) return;
// Another unclear transformation
if (data && has) {
data = Gun.state.to(data, has);
}
// Emulate incoming ack
ctx.on('in', {
'@' : msg['#'],
put : Gun.graph.node(data),
});
});
});
});
// Export debug interface
if ('undefined' === typeof window) {
var EventEmitter = require('events').EventEmitter;
module.exports = debug = new EventEmitter();
}

View File

@ -65,7 +65,6 @@
"buffer": "^5.4.3"
},
"peerDependencies": {
"@gooddollar/react-native-webview-crypto": "^0.*",
"bufferutil": "^4.0.1",
"utf-8-validate": "^5.0.2",
"text-encoding": "^0.7.0"

View File

@ -80,7 +80,7 @@ describe("The Holy Grail AXE Test!", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -413,11 +413,7 @@ describe("The Holy Grail AXE Test!", function(){
},1000);
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -82,7 +82,7 @@ describe("Put ACK", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -188,11 +188,7 @@ describe("Put ACK", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -83,7 +83,7 @@ describe("Put ACK", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -153,11 +153,7 @@ describe("Put ACK", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -82,7 +82,7 @@ describe("Put ACK", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -125,11 +125,7 @@ describe("Put ACK", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -123,7 +123,7 @@ describe("Put ACK", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -179,11 +179,7 @@ describe("Put ACK", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -73,7 +73,7 @@ describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +"
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
browsers.atLeast(1).then(function(){
browsers.run(function(test){
var env = test.props;
@ -166,11 +166,7 @@ describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +"
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -82,7 +82,7 @@ describe("Put ACK", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -125,11 +125,7 @@ describe("Put ACK", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -65,7 +65,7 @@ describe("The Holy Grail Test!", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -267,11 +267,7 @@ describe("The Holy Grail Test!", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -0,0 +1,30 @@
<html>
<head>
<title>Infinite scroll example</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
<link rel="stylesheet" type="text/css" href="./style.css">
<script src="/jquery.js"></script>
<script src="/gun.js"></script>
<script src="./ScrollWindow.js"></script>
<script src='panic.js'></script>
<script>panic.server(location.origin)</script>
</head>
<body>
<header>
<form id="generate">
<input type="text" id="number" placeholder="Number of posts"/>
<button>Generate</button>
</form>
<div id="top-buttons">
<button id="top">Top</button>
</div>
</header>
<div id="container" style="padding-top: 0px; padding-bottom: 0px"></div>
<button id="bottom">Bottom</button>
<script src="./index.js"></script>
</body>
</html>

View File

@ -0,0 +1,226 @@
var config = {
IP: require('ip').address(),
port: 8765,
servers: 1,
browsers: 1,
each: 1500,
wait: 1,
route: {
'/': __dirname + '/index.html',
'/ScrollWindow.js': __dirname + '/../../../examples/infinite-scroll/ScrollWindow.js',
'/index.js': __dirname + '/../../../examples/infinite-scroll/index.js',
'/style.css': __dirname + '/../../../examples/infinite-scroll/style.css',
'/gun.js': __dirname + '/../../../gun.js',
'/jquery.js': __dirname + '/../../../examples/jquery.js'
}
}
/*
Welcome, person!
You have found the test that causes gun to PANIC with load!
Above are options to configure, the only ones useful are:
- browsers // number of browsers you want to load test across.
- each // the number of messages each browser should sync.
This test is less than 200 lines of code (without comments)!
However, if you aren't familiar with PANIC - you are in for a surprise!
I'm Plublious, and I shall be your guide!
*/
// First we need to create a PANIC server.
// Each device/browser in the distributed system we are testing connects to it.
// It then coordinates these clients to cause chaos in the distributed system.
// Cool huh?
var panic = require('panic-server');
panic.server().on('request', function(req, res){ // Static server
config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res);
}).listen(config.port); // Start panic server.
// In order to tell the clients what to do,
// We need a way to reference all of them.
var clients = panic.clients;
// Some of the clients may be NodeJS servers on different machines.
// PANIC manager is a nifty tool that lets us remotely spawn them.
var manager = require('panic-manager')();
manager.start({
clients: Array(config.servers).fill().map(function(u, i){ // Create a bunch of servers.
return {
type: 'node',
port: config.port + (i + 1) // They'll need unique ports to start their servers on, if we run the test on 1 machine.
}
}),
panic: 'http://' + config.IP + ':' + config.port // Auto-connect to our panic server.
});
// Now lets divide our clients into "servers" and "browsers".
var servers = clients.filter('Node.js');
var browsers = clients.excluding(servers);
// Sweet! Now we can start the tests.
// PANIC works with Mocha and other testing libraries!
// So it is easy to use PANIC.
describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +" server(s)!", function(){
// We'll have to manually launch the browsers,
// So lets up the timeout so we have time to do that.
this.timeout(5 * 60 * 1000);
it("Servers have joined!", function(){
// Alright, lets wait until enough gun server peers are connected.
return servers.atLeast(config.servers);
});
it("GUN has spawned!", function(){
// Once they are, we need to actually spin up the gun server.
var tests = [], i = 0;
servers.each(function(client){
// for each server peer, tell it to run this code:
tests.push(client.run(function(test){
// NOTE: Despite the fact this LOOKS like we're in a closure...
// it is not! This code is actually getting run
// in a DIFFERENT machine or process!
var env = test.props;
// As a result, we have to manually pass it scope.
test.async();
// Clean up from previous test.
try{ require('fs').unlinkSync(env.i+'data.json') }catch(e){}
var server = require('http').createServer(function(req, res){
res.end("I am "+ env.i +"!");
});
// Launch the server and start gun!
var Gun = require('gun');
// Attach the server to gun.
var gun = Gun({file: env.i+'data', web: server, localStorage: false});
server.listen(env.config.port + env.i, function(){
// This server peer is now done with the test!
// It has successfully launched.
test.done();
});
}, {i: i += 1, config: config}));
});
// NOW, this is very important:
// Do not proceed to the next test until
// every single server (in different machines/processes)
// have ALL successfully launched.
return Promise.all(tests);
});
it(config.browsers +" browser(s) have joined!", function(){
// Okay! Cool. Now we can move on to the next step...
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
// Which is to manually open up a bunch of browser tabs
// and connect to the PANIC server in the same way
// the NodeJS servers did.
// However! We're gonna cheat...
browsers.atLeast(1).then(function(){
// When there is at least one browser opened, tell it to run this code:
browsers.run(function(test){
// NOTE: This closure is now being run IN THE BROWSER.
// This code is not server side code, despite the fact
// that we've written it on the server. It is not.
// Mind blowing, right?
var env = test.props;
}, {config: config});
});
// Cool! Once that is done...
// WAIT until all those browser tabs
// have connected to the PANIC server
// THEN move onto the next step
// where we will cause chaos!
return browsers.atLeast(config.browsers);
});
it("Data was saved and synced across all browsers!", function(){
// This is where it gets good!
var tests = [], ids = {}, i = 0;
// Let us create a list of all the browsers IDs connected.
// This will later let each browser check against every other browser.
browsers.each(function(client, id){
ids[id] = 1;
});
browsers.each(function(client, id){
// for every browser, run the following code:
tests.push(client.run(function(test){
//var audio = new Audio('https://www.nasa.gov/mp3/640170main_Roger%20Roll.mp3');audio.addEventListener('ended', function() {this.currentTime = 0;this.play();}, false);audio.play(); // testing if audio prevents Chrome throttle?
localStorage.clear(); // Clean up anything from before.
var env = test.props;
// Get access to the "outer scope" which has the browser IDs
// as well as other configuration information.
test.async();
// Now we want to connect to every gun server peer...
var peers = [], i = env.config.servers;
while(i--){
// For the total number of servers listed in the configuration
// Add their URL into an array.
peers.push('http://'+ env.config.IP + ':' + (env.config.port + (i + 1)) + '/gun');
}
// Pass all the servers we want to connect to into gun.
//var gun = Gun();
var gun = Gun(peers);
// Now we want to create a list
// of all the messages that WILL be sent
// according to the expected configuration.
// This is equal to...
var num = 0, total = env.config.each, check = {};
var report = $("<div>").css({position: 'fixed', top: 0, right: 0, background: 'white', padding: 10}).text(num +" / "+ total +" Verified").prependTo('body');
// Add a nifty UI that tells us how many messages have been verified.
// FINALLY, tell gun to subscribe to every record
// that is is/will be saved to this table.
var countAndScroll = () => {
$('.post b').each(function() {
var t = $(this).text();
if (check[t]) return;
num += 1;
report.text(num +" / "+ total +" Verified");
if (num === total) {
test.done();
}
check[t] = true;
});
$(window).scrollTop($(window).height());
}
window.onRender = elements => {
countAndScroll();
};
$('#number').val(env.config.each);
$('#generate button').click();
countAndScroll();
}, {i: i += 1, id: id, ids: ids, config: config}));
});
// YAY! We're finally done.
// IF AND ONLY IF
// EVERY SINGLE BROWSER
// HAS VERIFIED
// EVERY OTHER BROWSERS' data.
// If they are ALL done, go to the next step.
return Promise.all(tests);
});
after("Everything shut down.", function(){
// which is to shut down all the browsers.
browsers.run(function(){
setTimeout(function(){
location.reload();
}, 15 * 1000);
});
// And shut down all the servers.
return servers.run(function(){
process.exit();
});
});
})
// THE END!
// Congrats, wasn't that epic?
// Or still confused how a single 200 LOC test file
// Is running correctness verification tests
// across an entire distributed system of devices/browsers?
// Well, jump on https://gitter.im/amark/gun !
// Think adding tests like this to your work place would be bomb awesome?
// We totally sell PANIC training, licenses, and support!
// Please reach out to hi@gunDB.io if you are interested
// in purchasing consulting or services for PANIC.

216
test/panic/level.js Normal file
View File

@ -0,0 +1,216 @@
const panic = require('panic-server');
const clients = panic.clients;
const manager = require('panic-manager')();
const opts = { radisk: false, localStorage: false, file: false };
require('events').EventEmitter.defaultMaxListeners = Infinity;
const config = {
ip : require('ip').address(),
port : 8765,
servers : 3,
route : {
'/' : __dirname + '/index.html',
'/gun.js' : __dirname + '/../../gun.js',
'/jquery.js' : __dirname + '/../../examples/jquery.js'
}
};
const srv = panic.server();
srv.on('request', (req, res) => {
config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res);
}).listen(config.port);
manager.start({
clients: Array(config.servers).fill().map((u,i) => ({
type: 'node',
port: config.port + i + 1,
})),
panic: `http://${config.ip}:${config.port}`
});
const servers = clients.filter('Node.js');
const server = servers.pluck(1);
const alice = servers.excluding(server).pluck(1);
const bob = servers.excluding(server).excluding(alice).pluck(1);
describe('Make sure the leveldb storage engine works', function() {
this.timeout(5 * 60 * 60 * 1000);
it("servers have joined!", function() {
return servers.atLeast(config.servers);
});
it("GUN started!", function() {
return server.run(function(test) {
test.async();
const {config,opts} = test.props;
const leveldown = require('leveldown');
const encode = require('encoding-down');
const levelup = require('levelup');
if (require('fs').existsSync('./lvldata')) {
console.error('Please delete previous data first!');
return;
}
// Initialize leveldb
// const level = global.level = levelup(leveldown('./lvldata'));
const level = global.level = levelup(encode(leveldown('./lvldata'), { valueEncoding: 'json' }));
// Load the libraries under test
const Gun = require('../../../index');
const debug = require('../../../lib/level');
// // Add debug message
// debug.on('create', () => console.log('LEVEL CREATE'));
// debug.on('get' , key => console.log('LEVEL GET', key));
// debug.on('put' , (key, value) => console.log('LEVEL PUT', key, value));
// // debug.on('list', () => console.log('LEVEL LIST'));
// debug.on('error' , err => console.log('LEVEL ERROR', err));
// Track state (so we can wait on put, it's called late by radisk)
global.state = 0;
debug.on('put', () => global.state++);
// Create server
opts.web = require('http').createServer(function(req, res) {
res.end("Number five is alive!");
});
// Initialize gun & start server
const gun = global.gun = Gun({ ...opts, level });
opts.web.listen(config.port + 1, function() {
test.done();
});
}, {config,opts});
});
it("Alice saves data", function() {
return alice.run(function(test) {
test.async();
const {config,opts} = test.props;
const Gun = require('../../../index');
// Start gun
const gun = global.gun = Gun({
...opts,
peers: 'http://'+ config.ip + ':' + (config.port + 1) + '/gun',
lack : 1000 * 60 * 60,
});
// Save data
// Timeout allows callbacks to fire before server read
const ref = gun.get('asdf');
ref.put({ hello: 'world' });
setTimeout(() => {
test.done();
}, 1);
}, {config,opts});
});
it('Server read data', function() {
return server.run(function(test) {
test.async();
// Read data (triggers fetch from alice + write to disk)
const ref = gun.get('asdf');
ref.on(data => {
if (data.hello !== 'world') {
return test.fail('Invalid data returned');
}
ref.off();
test.done();
});
});
});
it('Wait for server to store', function() {
return server.run(function(test) {
test.async();
setTimeout(function awaitState() {
if (global.state < 2) return setTimeout(awaitState, 50);
test.done();
}, 50);
});
});
it('Close all original running nodes', function() {
clients.pluck(2).run(function() {
if (global.level) {
global.level.close(function() {
process.exit();
});
} else {
process.exit();
}
});
});
it('Start bob', function() {
return bob.run(function(test) {
test.async();
const {config,opts} = test.props;
const leveldown = require('leveldown');
const encode = require('encoding-down');
const levelup = require('levelup');
// Initialize gun opts
const level = global.level = levelup(encode(leveldown('./lvldata'), { valueEncoding: 'json' }));
// Load the libraries under test
const Gun = require('../../../index');
const debug = require('../../../lib/level');
// // Add debug messages
// debug.on('get', key => console.log('LEVEL GET', key));
// debug.on('put', (key, value) => console.log('LEVEL PUT', key, value));
// // debug.on('list', () => console.log('LEVEL LIST'));
// debug.on('error', err => console.log('LEVEL ERROR', err));
// Create server
opts.web = require('http').createServer((req, res) => {
res.end("Number five is alive!");
});
// Initialize gun & start server
const gun = global.gun = Gun({ ...opts, level });
opts.web.listen(config.port + 1, () => {
test.done();
});
}, {config,opts});
});
it('Bob read', function() {
return bob.run(function(test) {
test.async();
// Read data
const ref = gun.get('asdf');
ref.on(data => {
if (data.hello !== 'world') {
return test.fail('Invalid data returned');
}
ref.off();
test.done();
});
});
});
it('Shutdown bob', function() {
return clients.run(function() {
process.exit()
});
});
it("All finished!", function(done) {
srv.close();
done();
});
});

452
test/panic/lexical.js Normal file
View File

@ -0,0 +1,452 @@
/**
* RAD Lexical search test
*
* What we want here: (1) Superpeer and (n) peers
* - The Superpeer have a graph with `key(timestamp)->value(value)`
* - The peer will run an amount of queries and the total of results are the 'expected results'.
*
* Tip: to run this `mocha test/panic/lexical`
*
*/
var config = {
IP: require('ip').address(),
port: process.env.PORT ? parseInt(process.env.PORT) : 8765,
servers: 1,
browsers: 1,
route: {
'/': __dirname + '/index.html',
'/gun.js': __dirname + '/../../gun.js',
'/gun/lib/radix.js': __dirname + '/../../lib/radix.js',
'/gun/lib/radisk.js': __dirname + '/../../lib/radisk.js',
'/gun/lib/store.js': __dirname + '/../../lib/store.js',
'/gun/lib/rindexed.js': __dirname + '/../../lib/rindexed.js',
'/jquery.js': __dirname + '/../../examples/jquery.js'
},
wait_map: 700
}
var panic = require('panic-server');
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.servers).fill().map(function(u, i) {
return {
type: 'node',
port: config.port + (i + 1)
}
}),
panic: 'http://' + config.IP + ':' + config.port
});
var servers = clients.filter('Node.js');
var server = global.server = servers.pluck(1);
var server2 = servers.excluding(server).pluck(1);
var browsers = clients.excluding(servers);
var alice = browsers.pluck(1);
var bob = browsers.excluding(alice).pluck(1);
var john = browsers.excluding(alice).excluding(bob).pluck(1);
describe('RAD Lexical search Test! ', function() {
this.timeout(5 * 60 * 1000);
// this.timeout(10 * 60 * 1000);
it('Servers have joined! ', function() {
return servers.atLeast(config.servers);
});
it('GUN server started! ', function() {
return server.run(function(test) {
var env = test.props;
test.async();
try { require('fs').unlinkSync('radata_test_lexical_' + env.i) } catch (e) {}
try { require('fs').unlinkSync('radata_test_lexical_' + (env.i + 1)) } catch (e) {}
var port = env.config.port + env.i;
var server = require('http').createServer(function(req, res) { res.end("I am " + env.i + "!"); });
var Gun = global.Gun = require('gun');
var gun = global.gun = Gun({
axe: false,
multicast: false,
web: server,
file: 'radata_test_lexical_' + env.i,
//radisk:false, localStorage:false
});
server.listen(port, function() {
test.done();
});
}, {
i: 1,
config: config
});
});
it('Create graph with all days of year 2020', function() {
return server.run(function(test) {
test.async();
var j = 0;
graph = gun._.graph, timenode = {}, start = new Date('2020-01-01T00:00:00Z'), end = new Date('2020-12-31T23:59:59Z'), startt = start.getTime(), endt = end.getTime(), p = startt;
var ref = global.ref = gun.get('timenode');
while (p <= endt) { //console.log('.... >>> ', p, ++j, new Date(p).toISOString());
var t = new Date(p);
var ts = t.toISOString();
var soul = 'timeline_' + ts;
var d = { msg: ' datetime ' + ts };
var n = gun.get(soul).put(d);
ref.get(ts).put(n);
p = p + (24 * 60 * 60 * 1000); //24h
}
setTimeout(function() { /// wait a while for the .put
test.done();
}, 1000);
});
});
it('Query server - Beetween(< item <) ', function() {
var i=0;
return server.run(function(test) {
var env = test.props;
var t = setTimeout(function() { test.fail('Error: No response.');}, 5000);
var results = [];
var query = { '%': 100000, '.': { '>': '2020-02-01', '<': '2020-02-26' } };
test.async();
ref.get(query).map().once(function(v, k) {
if (k && v) { results.push(k); }
});
var t2 = setTimeout(function() {
var len = 25; /// expected number of results
var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort();
clearTimeout(t);
if (results_unique.length === len) {
test.done();
} else {
console.log('RESULTS Query server - Beetween(< item <): ', results_unique.length, results_unique.join(', '));
test.fail('Error: get ' + results_unique.length + ' attributes.');
}
}, env.config.wait_map);
}, {
i: i += 1,
config: config
});
});
it('Query server - Higher(>) ', function() {
var i=0;
return server.run(function(test) {
var env = test.props;
var t = setTimeout(function() { test.fail('Error: No response.');}, 5000);
var results = [];
var query = { '%': 100000, '.': { '>': '2020-12-01' } };
test.async();
ref.get(query).map().once(function(v, k) {
if (k && v) { results.push(k); }
});
var t2 = setTimeout(function() {
var len = 31; /// expected number of results
var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort();
clearTimeout(t);
if (results_unique.length === len) {
test.done();
} else {
console.log('RESULTS Query server - Higher(>): ', results_unique.length, results_unique.join(', '));
test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len);
}
}, env.config.wait_map);
}, {
i: i += 1,
config: config
});
});
it('Query server - Lower(<) ', function() {
var i=0;
return server.run(function(test) {
test.async();
var env = test.props;
var t = setTimeout(function() { test.fail('Error: No response.');}, 5000);
var results = [];
var query = { '%': 100000, '.': { '<': '2020-02-06' } };
ref.get(query).map().once(function(v, k) {
if (k && v) { results.push(k); }
});
var t2 = setTimeout(function() {
var len = 37; /// expected number of results
var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort();
clearTimeout(t);
if (results_unique.length === len) {
test.done();
} else {
console.log('RESULTS Query server - Lower(<): ', results_unique.length, results_unique.join(', '));
test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len);
}
}, env.config.wait_map);
}, {
i: i += 1,
config: config
});
});
it('Query server - Exact match(=) ', function() {
var i=0;
return server.run(function(test) {
var env = test.props;
var t = setTimeout(function() { test.fail('Error: No response.');}, 5000);
var results = [];
var query = { '%': 100000, '.': { '=': '2020-03-01T00:00:00.000Z' } };
test.async();
ref.get(query).map().once(function(v, k) {
if (k && v) { results.push(k); }
});
var t2 = setTimeout(function() {
var len = 1; /// expected number of results
var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort();
clearTimeout(t);
if (results_unique.length === len) {
test.done();
} else {
console.log('RESULTS Query server - Exact match(=): ', results_unique.length, results_unique.join(', '));
test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len);
}
}, env.config.wait_map);
}, {
i: i += 1,
config: config
});
});
it('Query server - Prefix match(*) ', function() {
var i=0;
return server.run(function(test) {
var env = test.props;
var t = setTimeout(function() { test.fail('Error: No response.');}, 5000);
var results = [];
var query = { '%': 100000, '.': { '*': '2020-10-' } };
test.async();
ref.get(query).map().once(function(v, k) {
if (k && v) { results.push(k); }
});
var t2 = setTimeout(function() {
var len = 31; /// expected number of results
var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort();
clearTimeout(t);
if (results_unique.length === len) {
test.done();
} else {
console.log('RESULTS Query server - Prefix match(*): ', results_unique.length, results_unique);
test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len);
}
}, env.config.wait_map);
}, {
i: i += 1,
config: config
});
});
it(config.browsers + ' browser(s) have joined! ', function() {
console.log('PLEASE OPEN http://' + config.IP + ':' + config.port + ' IN ' + config.browsers + ' BROWSER(S)!');
return browsers.atLeast(config.browsers);
});
it('Browsers initialized gun! ', function() {
var tests = [], i = 0;
browsers.each(function(client, id) {
tests.push(client.run(function(test) {
test.async();
localStorage.clear();
console.log('Clear localStorage!!!');
// ['/gun/lib/radix.js', '/gun/lib/radisk.js', '/gun/lib/store.js', '/gun/lib/rindexed.js'].map(function(src) {
// var script = document.createElement('script');
// script.setAttribute('src', src);
// document.head.appendChild(script);
// });
var env = test.props;
var opt = {
peers: ['http://' + env.config.IP + ':' + (env.config.port + 1) + '/gun'],
localStorage: false
};
var pid = location.hash.slice(1);
if (pid) {
opt.pid = pid;
}
Gun.on('opt', function(ctx) {
this.to.next(ctx);
ctx.on('hi', function(opt) {
document.title = 'RAD test PID: ' + this.on.opt.pid;
});
});
setTimeout(function() {
var gun = window.gun = Gun(opt);
var ref = window.ref = gun.get('timenode');
test.done();
}, 1000);
}, {
i: i += 1,
config: config
}));
});
return Promise.all(tests);
});
it('Query browser - Beetween(< item <) ', function() {
var tests = [], i = 0;
browsers.each(function(client, id) {
tests.push(client.run(function(test) {
var env = test.props;
var t = setTimeout(function() { test.fail('Error: No response.'); }, 6000);
var results = [];
var query = { '%': 100000, '.': { '>': '2020-06-01', '<': '2020-09-01' } };
test.async();
ref.get(query).map().once(function(v, k) {
if (k && v) { results.push(k); }
});
var t2 = setTimeout(function() {
var len = 30+31+31; /// expected number of results
var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort();
clearTimeout(t);
if (results_unique.length === len) {
test.done();
} else {
console.log('RESULTS Query browser - Beetween(< item <): ', results_unique.length, results_unique.join(', '));
test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len);
}
}, env.config.wait_map);
}, { i: i += 1, config: config }));
});
return Promise.all(tests);
});
it('Query browser - Higher(>) ', function() {
var tests = [], i = 0;
browsers.each(function(client, id) {
tests.push(client.run(function(test) {
var env = test.props;
var t = setTimeout(function() { test.fail('Error: No response.');}, 5000);
var results = [];
var query = { '%': 100000, '.': { '>': '2020-11-01' } };
test.async();
ref.get(query).map().once(function(v, k) {
if (k && v) { results.push(k); }
});
var t2 = setTimeout(function() {
var len = 61; /// expected number of results
var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort();
clearTimeout(t);
if (results_unique.length === len) {
test.done();
} else {
console.log('RESULTS Query browser - Higher(>): ', results_unique.length, results_unique.join(', '));
test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len);
}
}, env.config.wait_map);
}, {
i: i += 1,
config: config
}));
});
return Promise.all(tests);
});
it('Query browser - Lower(<) ', function() {
var tests = [], i = 0;
browsers.each(function(client, id) {
tests.push(client.run(function(test) {
var env = test.props;
var t = setTimeout(function() { test.fail('Error: No response.');}, 5000);
var results = [];
var query = { '%': 100000, '.': { '<': '2020-02-05' } };
test.async();
ref.get(query).map().once(function(v, k) {
if (k && v) { results.push(k); }
});
var t2 = setTimeout(function() {
var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort();
var len = 35; /// expected number of results
clearTimeout(t);
if (results_unique.length === len) {
test.done();
} else {
console.log('RESULTS Query browser - Lower(<): ', results_unique.length, results_unique.join(', '));
test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len);
}
}, env.config.wait_map);
}, {
i: i += 1,
config: config
}));
});
return Promise.all(tests);
});
it('Query browser - Exact match(=) ', function() {
var tests = [], i = 0;
browsers.each(function(client, id) {
tests.push(client.run(function(test) {
var env = test.props;
var t = setTimeout(function() { test.fail('Error: No response.');}, 5000);
var results = [];
var query = { '%': 100000, '.': { '=': '2020-06-01T00:00:00.000Z' } };
test.async();
ref.get(query).map().once(function(v, k) {
if (k && v) { results.push(k); }
});
var t2 = setTimeout(function() {
var len = 1; /// expected number of results
var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort();
clearTimeout(t);
if (results_unique.length === len) {
test.done();
} else {
console.log('RESULTS Query browser - Exact match(=): ', results_unique.length, results_unique.join(', '));
test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len);
}
}, env.config.wait_map);
}, {
i: i += 1,
config: config
}));
});
return Promise.all(tests);
});
it('Query browser - Prefix match(*) ', function() {
var tests = [], i = 0;
browsers.each(function(client, id) {
tests.push(client.run(function(test) {
var env = test.props;
var t = setTimeout(function() { test.fail('Error: No response.');}, 5000);
var results = [];
var query = { '%': 100000, '.': { '*': '2020-10-' } };
test.async();
ref.get(query).map().once(function(v, k) {
if (k && v) { results.push(k); }
});
var t2 = setTimeout(function() {
var len = 31; /// expected number of results
var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort();
clearTimeout(t);
if (results_unique.length === len) {
test.done();
} else {
console.log('RESULTS Query browser - Prefix match(*): ', results_unique.length, results_unique);
test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len);
}
}, env.config.wait_map);
}, {
i: i += 1,
config: config
}));
});
return Promise.all(tests);
});
////////////////////////////////
it("Wait...", function(done){
setTimeout(done, 3000);
});
it("All finished!", function(done){
console.log("Done! Cleaning things up...");
setTimeout(function(){
done();
},1000);
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
return servers.run(function(){
process.exit();
});
});
});

View File

@ -82,7 +82,7 @@ describe("Broadcast Video", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -180,11 +180,7 @@ describe("Broadcast Video", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -105,8 +105,8 @@ describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +"
it(config.browsers +" browser(s) have joined!", function(){
// Okay! Cool. Now we can move on to the next step...
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
// Which is to manually open up a bunch of browser tabs
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
// Which is to automatically or manually open up a bunch of browser tabs
// and connect to the PANIC server in the same way
// the NodeJS servers did.
@ -249,7 +249,7 @@ describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +"
after("Everything shut down.", function(){
// which is to shut down all the browsers.
browsers.run(function(){
require('./util/open').cleanup() || browsers.run(function(){
setTimeout(function(){
location.reload();
}, 15 * 1000);

View File

@ -75,7 +75,7 @@ describe("No Empty Object on .Once", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -118,11 +118,7 @@ describe("No Empty Object on .Once", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -72,7 +72,7 @@ describe("gun.on should receive updates after crashed relay peer comes back onli
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -217,7 +217,7 @@ describe("gun.on should receive updates after crashed relay peer comes back onli
});
after("Everything shut down.", function(){
return bob.run(function(){
return require('./util/open').cleanup() || bob.run(function(){
process.exit();
});
});

View File

@ -74,7 +74,7 @@ describe("Make sure the Radix Storage Engine (RAD) works.", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -275,11 +275,7 @@ describe("Make sure the Radix Storage Engine (RAD) works.", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -223,11 +223,7 @@ describe("Stress test GUN with SEA users causing PANIC!", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -65,7 +65,7 @@ describe("Make sure SEA syncs correctly", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -188,11 +188,7 @@ describe("Make sure SEA syncs correctly", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -310,11 +310,7 @@ describe("Stress test GUN with SEA users causing PANIC!", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -64,7 +64,7 @@ describe("Private Message Threading", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -218,11 +218,7 @@ describe("Private Message Threading", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

View File

@ -65,7 +65,7 @@ describe("End-to-End Encryption on User Accounts", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -253,11 +253,7 @@ describe("End-to-End Encryption on User Accounts", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});

45
test/panic/util/open.js Normal file
View File

@ -0,0 +1,45 @@
let _browsers = [];
module.exports = {
web: (count, url, options) => {
if (typeof count === 'string') {
options = url;
url = count;
count = 1;
}
if (!url || typeof url !== 'string') {
throw new Error('Invalid URL');
}
try {
require('puppeteer').launch(options).then(browser => {
_browsers.push(browser);
Array(count).fill(0).forEach((x, i) => {
console.log('Opening browser page ' + i + ' with puppeteer...');
browser.newPage().then(page => {
page.on('console', msg => {
if (msg.text() === 'JSHandle@object') {
// FIXME: Avoid text comparison
return;
}
console.log(`${i} [${msg.type()}]: ${msg.text()}`);
});
return page.goto(url);
}).then(() => {
console.log('Browser page ' + i + ' open');
});
});
});
} catch (err) {
console.log("PLEASE OPEN "+ url +" IN "+ count +" BROWSER(S)!");
console.warn('Consider installing puppeteer to automate browser management (npm i -g puppeteer && npm link puppeteer)');
}
},
cleanup: () => {
if (_browsers.length === 0) {
return undefined;
}
console.log('Closing all puppeteer browsers...');
return Promise.all(_browsers.map(b => b.close()))
.then(() => console.log('Closed all puppeteer browsers'));
}
}

View File

@ -65,7 +65,7 @@ describe("Make sure SEA syncs correctly", function(){
});
it(config.browsers +" browser(s) have joined!", function(){
console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!");
require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port);
return browsers.atLeast(config.browsers);
});
@ -233,11 +233,7 @@ describe("Make sure SEA syncs correctly", function(){
});
after("Everything shut down.", function(){
browsers.run(function(){
//location.reload();
//setTimeout(function(){
//}, 15 * 1000);
});
require('./util/open').cleanup();
return servers.run(function(){
process.exit();
});