Files
panic-server/patch.js
Jesse Gibson c5e8625acc Full configuration, automatic browser tab manipulation, statistics, progress events, completion event, de-duplication of acknowledgments, accurate recursive test runners, prep for selenium instantiation, included polyfills and extensions.
New barrage of options now accepted (providing defaults in absence), such as a progress callback, a done callback and timeout configuration, a data generation function, etc...

Browser constructor meant to open new tabs, run the test (using the given options), and close the tab when finished. There is no loss of options by doing this (although it has had a notable effect on performance when the tab is not in focus).

Each progress event and the done event recieve the options object with statistics embeded in the "stats" property. It has details like the average latency, the total elapsed time, the acknowledged packets, the fastest/slowest times, etc...

Acknowledgments can fire more than once, and for now it dedupes those and only fires once. In the future I may change this to include every acknowledgment. For data!

The recursive test runner wasn't running the correct number of times (due to confusion and off-by-1 errors)... that has been fixed.

The test file checks to see if there were options globally declared. If so, run the test using those. In the future we may use selenium to boot up the browsers, and we'll need some way to kick off the test and provide options. That was a nice compromise.

I've included some useful polyfills (like interfacing with console.log so it can be passed naked to other functions, Object.keys polyfill for counting confirmed objects) and some extensions (like valMapDone).

Next steps include adding the gun instance to the options object, statistics for errors, node compatibility and the such.
2015-12-22 13:36:50 -07:00

110 lines
2.1 KiB
JavaScript

/*globals Gun, type, console*/
/*
Return an object like this:
return {
expect: Object,
done: Function,
progress: Function,
interval: Number,
peers: Array,
packets: Number,
id: String,
key: String,
path: String,
packet: Function
};
*/
var patch;
(function () {
'use strict';
var random = Gun.text.random;
function is(data, expected) {
var string, error, actual = type(data);
if (actual !== expected) {
string = JSON.stringify(data);
error = 'Expected ' + expected + ', was ' + actual + ': ' + string;
throw new TypeError(error);
}
return true;
}
function or(obj, prop, val) {
return (obj[prop] = obj[prop] || val);
}
function done(opt) {
or(opt, 'done', {});
or(opt.done, 'cb', function () {
console.log('No finishing callback');
});
or(opt.done, 'timeout', 2000);
is(opt.done, 'object');
is(opt.done.timeout, 'number');
is(opt.done.cb, 'function');
}
function packet(opt) {
// wrap the packet function
// set time and id for
// every returned object
opt.packet = (function () {
var makePacket = opt.packet || function () {
return random();
};
return function () {
return {
data: makePacket(),
time: Gun.time.is(),
id: opt.id
};
};
}());
is(opt.packet(), 'object');
}
patch = function (opt) {
var url = location.protocol + '//';
url += location.host;
url += '/gun';
opt = opt || {};
opt.interval = opt.interval || 0;
opt.peers = opt.peers || [url];
opt.packets = opt.packets || 50;
opt.id = opt.id || random(10);
opt.key = opt.key || random(15);
opt.path = opt.path || random(5);
opt.requests = [];
opt.requests.start = Gun.time.is();
or(opt, 'progress', function (opt, num) {
console.log('Saved', num);
});
done(opt);
packet(opt);
is(opt.key, 'string');
is(opt.interval, 'number');
is(opt.peers, 'array');
is(opt.packets, 'number');
is(opt.id, 'string');
is(opt.path, 'string');
is(opt.progress, 'function');
if (opt.packets < 0) {
throw new RangeError("Negative packets? Are you crazy?");
}
return opt;
};
}());