even-better
Version:
Server and process monitoring plugin
195 lines (145 loc) • 7.75 kB
JavaScript
// Load modules
var Code = require('code');
var Hapi = require('hapi');
var Hoek = require('hoek');
var Http = require('http');
var Https = require('https');
var Lab = require('lab');
var Wreck = require('wreck');
// Done for testing because Wreck is a singleton and every test run ads one event to it
Wreck.setMaxListeners(0);
var GoodReporter = require('./helper');
// Declare internals
var internals = {
agent: new Https.Agent({ maxSockets: 6 })
};
// Test shortcuts
var lab = exports.lab = Lab.script();
var expect = Code.expect;
var describe = lab.describe;
var it = lab.it;
describe('Plugin', function () {
it('emits ops data', function (done) {
var server = new Hapi.Server();
var options = {
opsInterval: 100,
httpAgents: new Http.Agent(),
httpsAgents: new Https.Agent()
};
var one = new GoodReporter({ ops: '*' });
options.reporters = [one];
var plugin = {
register: require('..'),
options: options
};
server.register(plugin, function (err) {
expect(err).to.not.exist();
server.plugins.good.monitor.once('ops', function (event) {
expect(event.osload).to.exist();
expect(one.messages).to.have.length(1);
var message = one.messages[0];
expect(message.event).to.equal('ops');
expect(message.host).to.exist();
done();
});
});
});
it('tracks used sockets', function (done) {
var server = new Hapi.Server();
server.connection({ host: 'localhost' });
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
Https.get({
hostname: 'www.google.com',
port: 433,
path: '/',
agent: internals.agent
});
}
});
var options = {
opsInterval: 1000,
httpsAgents: internals.agent
};
var one = new GoodReporter({ ops: '*' });
options.reporters = [one];
var plugin = {
register: require('..'),
options: options
};
server.register(plugin, function (err) {
expect(err).to.not.exist();
server.plugins.good.monitor.once('ops', function (event) {
expect(event.host).to.exist();
expect(event.sockets).to.exist();
expect(event.sockets.https.total).to.equal(6);
done();
});
for (var i = 0; i < 10; ++i) {
server.inject({ url: '/' });
}
});
});
it('wreck data emits', function (done) {
var server = new Hapi.Server();
var tlsOptions = {
labels: ['https'],
port: 0,
key: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafx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0RAu9weyWxYXOWRhnISsPghZg\nIjcrFNA5gYEHGnNHoNqVqE/mBpk3kI+rEVzuu59scv4QNQ7jegOFgSt8DNzuAZ0x\ntHTW1lBG3u8gG1eYBMquexoSSHmMUb73lQ2l/dC6AKjOHFB9Ouq3IjjdFGwx1diz\n/yWh+Y8wY1Mgpyiy6ObJ5W8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAoSc6Skb4\ng1e0ZqPKXBV2qbx7hlqIyYpubCl1rDiEdVzqYYZEwmst36fJRRrVaFuAM/1DYAmT\nWMhU+yTfA+vCS4tql9b9zUhPw/IDHpBDWyR01spoZFBF/hE1MGNpCSXXsAbmCiVf\naxrIgR2DNketbDxkQx671KwF1+1JOMo9ffXp+OhuRo5NaGIxhTsZ+f/MA4y084Aj\nDI39av50sTRTWWShlN+J7PtdQVA5SZD97oYbeUeL7gI18kAJww9eUdmT0nEjcwKs\nxsQT1fyKbo7AlZBY4KSlUMuGnn0VnAsB9b+LxtXlDfnjyM8bVQx1uAfRo0DO8p/5\n3J5DTjAU55deBQ==\n-----END CERTIFICATE-----\n'
};
server.connection({ tls: tlsOptions });
server.connection({ port: 0, labels: ['http'] });
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply('ok');
}
});
server.select('http').route({
method: 'GET',
path: '/http',
handler: function (request, reply) {
reply('ok');
}
});
var emitted;
var options = {
httpAgents: new Http.Agent(),
httpsAgents: new Https.Agent()
};
options.reporters = [new GoodReporter({
wreck: '*'
}, null, function (event) {
if (!emitted) {
expect(event.event).to.equal('wreck');
expect(event.request.protocol).to.equal('https:');
expect(event.request.host).to.exist();
expect(event.request.path).to.equal('/');
emitted = true;
}
else {
expect(event.event).to.equal('wreck');
expect(event.request.protocol).to.equal('http:');
expect(event.request.host).to.exist();
expect(event.request.path).to.equal('/http');
done();
}
})];
var plugin = {
register: require('..'),
options: options
};
server.register(plugin, function (err) {
expect(err).to.not.exist();
server.start(function () {
Wreck.get('https://127.0.0.1:' + server.connections[0].info.port + '/', { rejectUnauthorized: false }, function () {
Wreck.get('http://127.0.0.1:' + server.connections[1].info.port + '/http', Hoek.ignore);
});
});
});
});
});