Comparing version
@@ -8,3 +8,2 @@ 'use strict'; | ||
const Hoek = require('hoek'); | ||
const Items = require('items'); | ||
@@ -27,5 +26,5 @@ const NetworkMonitor = require('./network'); | ||
psmem: ProcessMonitor.memoryUsage, | ||
pscpu: ProcessMonitor.cpuUsage, | ||
psdelay: ProcessMonitor.delay, | ||
requests: this._networkMonitor.requests, | ||
concurrents: this._networkMonitor.concurrents, | ||
responseTimes: this._networkMonitor.responseTimes, | ||
@@ -40,15 +39,29 @@ sockets: this._networkMonitor.sockets | ||
const host = Os.hostname(); | ||
this._interval = setInterval(() => { | ||
Items.parallel.execute(this._tasks, (error, results) => { | ||
this._interval = setInterval(async () => { | ||
if (error) { | ||
this.emit('error', error); | ||
const tasks = []; | ||
for (const taskName in this._tasks) { | ||
tasks.push(this._tasks[taskName]()); | ||
} | ||
try { | ||
const results = await Promise.all(tasks); | ||
const emit = { | ||
host | ||
}; | ||
for (const taskName in this._tasks) { | ||
emit[taskName] = results.shift(); | ||
} | ||
else { | ||
results.host = host; | ||
this.emit('ops', results); | ||
} | ||
this.emit('ops', emit); | ||
} | ||
catch (err) { | ||
this.emit('error', err); | ||
} | ||
finally { | ||
this._networkMonitor.reset(); | ||
}); | ||
} | ||
}, interval); | ||
@@ -55,0 +68,0 @@ } |
@@ -9,3 +9,2 @@ 'use strict'; | ||
const Hoek = require('hoek'); | ||
const Items = require('items'); | ||
@@ -19,25 +18,33 @@ class Network { | ||
this._httpAgents = [].concat(httpAgents || Http.globalAgent); | ||
this._httpAgents = [].concat(httpsAgents || Https.globalAgent); | ||
this._httpsAgents = [].concat(httpsAgents || Https.globalAgent); | ||
this._server.on('request-internal', (request, event, tags) => { | ||
this._server.ext('onRequest', (request, h) => { | ||
const port = request.connection.info.port; | ||
const port = this._server.info.port; | ||
if (tags.received) { | ||
this._requests[port] = this._requests[port] || { total: 0, disconnects: 0, statusCodes: {} }; | ||
this._requests[port].total++; | ||
this._requests[port] = this._requests[port] || { | ||
total: 0, | ||
disconnects: 0, | ||
statusCodes: {} | ||
}; | ||
this._requests[port].total++; | ||
request.once('disconnect', () => { | ||
request.events.once('disconnect', () => { | ||
this._requests[port].disconnects++; | ||
}); | ||
} | ||
this._requests[port].disconnects++; | ||
}); | ||
return h.continue; | ||
}); | ||
this._server.on('response', (request) => { | ||
this._server.events.on('response', (request) => { | ||
const msec = Date.now() - request.info.received; | ||
const port = request.connection.info.port; | ||
const port = this._server.info.port; | ||
const statusCode = request.response && request.response.statusCode; | ||
const portResponse = this._responseTimes[port] = (this._responseTimes[port] || { count: 0, total: 0, max: 0 }); | ||
const portResponse = this._responseTimes[port] = (this._responseTimes[port] || { | ||
count: 0, | ||
total: 0, | ||
max: 0 | ||
}); | ||
portResponse.count++; | ||
@@ -56,30 +63,9 @@ portResponse.total += msec; | ||
this.requests = (callback) => { | ||
this.requests = () => { | ||
callback(null, this._requests); | ||
return this._requests; | ||
}; | ||
this.concurrents = (callback) => { | ||
this.responseTimes = () => { | ||
const result = {}; | ||
Items.serial(this._server.connections, (connection, next) => { | ||
connection.listener.getConnections((err, count) => { | ||
if (err) { | ||
return next(err); | ||
} | ||
result[connection.info.port] = count; | ||
next(); | ||
}); | ||
}, (err) => { | ||
callback(err, result); | ||
}); | ||
}; | ||
this.responseTimes = (callback) => { | ||
const ports = Object.keys(this._responseTimes); | ||
@@ -89,3 +75,5 @@ const overview = {}; | ||
const port = ports[i]; | ||
const count = Hoek.reach(this, `_responseTimes.${port}.count`, { default: 1 }); | ||
const count = Hoek.reach(this, `_responseTimes.${port}.count`, { | ||
default: 1 | ||
}); | ||
overview[port] = { | ||
@@ -97,12 +85,11 @@ avg: this._responseTimes[port].total / count, | ||
return callback(null, overview); | ||
return overview; | ||
}; | ||
this.sockets = (callback) => { | ||
this.sockets = () => { | ||
const result = { | ||
return { | ||
http: Network.getSocketCount(this._httpAgents), | ||
https: Network.getSocketCount(this._httpAgents) | ||
https: Network.getSocketCount(this._httpsAgents) | ||
}; | ||
callback(null, result); | ||
}; | ||
@@ -114,4 +101,12 @@ | ||
for (let i = 0; i < ports.length; ++i) { | ||
this._requests[ports[i]] = { total: 0, disconnects: 0, statusCodes: {} }; | ||
this._responseTimes[ports[i]] = { count: 0, total: 0, max: 0 }; | ||
this._requests[ports[i]] = { | ||
total: 0, | ||
disconnects: 0, | ||
statusCodes: {} | ||
}; | ||
this._responseTimes[ports[i]] = { | ||
count: 0, | ||
total: 0, | ||
max: 0 | ||
}; | ||
} | ||
@@ -118,0 +113,0 @@ }; |
@@ -16,3 +16,3 @@ 'use strict'; | ||
internals.mem = Utils.makeContinuation(() => { | ||
internals.mem = Utils.resolveNextTick(() => { | ||
@@ -25,4 +25,4 @@ return { | ||
internals.loadavg = Utils.makeContinuation(Os.loadavg); | ||
internals.loadavg = Utils.resolveNextTick(Os.loadavg); | ||
internals.uptime = Utils.makeContinuation(Os.uptime); | ||
internals.uptime = Utils.resolveNextTick(Os.uptime); |
@@ -16,8 +16,12 @@ 'use strict'; | ||
internals.delay = (callback) => { | ||
internals.delay = () => { | ||
const bench = new Hoek.Bench(); | ||
setImmediate(() => { | ||
return callback(null, bench.elapsed()); | ||
return new Promise((resolve) => { | ||
setImmediate(() => { | ||
return resolve(bench.elapsed()); | ||
}); | ||
}); | ||
@@ -27,5 +31,7 @@ }; | ||
internals.uptime = Utils.makeContinuation(process.uptime); | ||
internals.uptime = Utils.resolveNextTick(process.uptime); | ||
internals.memoryUsage = Utils.makeContinuation(process.memoryUsage); | ||
internals.memoryUsage = Utils.resolveNextTick(process.memoryUsage); | ||
internals.cpuUsage = Utils.resolveNextTick(process.cpuUsage); |
'use strict'; | ||
exports.makeContinuation = (predicate) => { | ||
exports.resolveNextTick = (predicate) => { | ||
return (callback) => { | ||
return () => { | ||
process.nextTick(callback, null, predicate()); | ||
return new Promise((resolve) => { | ||
process.nextTick((result) => resolve(result), predicate()); | ||
}); | ||
}; | ||
}; | ||
exports.timeout = (ms) => { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
}; |
{ | ||
"name": "oppsy", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"repository": "git://github.com/hapijs/oppsy", | ||
@@ -16,14 +16,13 @@ "description": "An EventEmitter useful for collecting hapi server ops information", | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=8.0.0" | ||
}, | ||
"license": "BSD-3-Clause", | ||
"devDependencies": { | ||
"code": "2.x.x", | ||
"hapi": "13.x.x", | ||
"lab": "10.x.x" | ||
"code": "5.x.x", | ||
"hapi": "17.x.x", | ||
"lab": "15.x.x" | ||
}, | ||
"dependencies": { | ||
"hoek": "4.x.x", | ||
"items": "2.x.x" | ||
"hoek": "5.x.x" | ||
} | ||
} |
@@ -21,8 +21,8 @@ # oppsy | ||
server.start(() => { | ||
oppsy.start(1000); | ||
}); | ||
await server.start(); | ||
oppsy.start(1000); | ||
``` | ||
This creates a new Oppsy object and starts collecting information ever 1000 miliseconds | ||
This creates a new Oppsy object and starts collecting information every 1000 miliseconds | ||
@@ -42,3 +42,3 @@ ### new Oppsy(server, [config]) | ||
Starts an Oppsy object collecting network and server information. | ||
- `interval` - number of seconds to wait between each data sampling. | ||
- `interval` - number of milliseconds to wait between each data sampling. | ||
@@ -45,0 +45,0 @@ #### oppsy.stop() |
'use strict'; | ||
// Load modules | ||
const Events = require('events'); | ||
const Fs = require('fs'); | ||
@@ -12,3 +11,2 @@ const Http = require('http'); | ||
const Hapi = require('hapi'); | ||
const Items = require('items'); | ||
const Lab = require('lab'); | ||
@@ -19,2 +17,3 @@ | ||
const Network = require('../lib/network'); | ||
const Utils = require('../lib/utils'); | ||
@@ -28,3 +27,2 @@ // Test shortcuts | ||
describe('Oppsy', () => { | ||
@@ -34,14 +32,19 @@ | ||
it('reports on network activity', (done) => { | ||
it('reports on network activity', async () => { | ||
const server = new Hapi.Server(); | ||
server.connection({ host: 'localhost' }); | ||
server.connection({ host: 'localhost' }); | ||
const server = new Hapi.Server({ | ||
host: 'localhost' | ||
}); | ||
server.route({ | ||
options: { | ||
log: { | ||
collect: true | ||
} | ||
}, | ||
method: 'GET', | ||
path: '/', | ||
handler: (request, reply) => { | ||
handler: (request, h) => { | ||
reply(); | ||
return 'ok'; | ||
} | ||
@@ -51,52 +54,38 @@ }); | ||
const network = new Network(server); | ||
const agent = new Http.Agent({ maxSockets: Infinity }); | ||
const usedPorts = []; | ||
const agent = new Http.Agent({ | ||
maxSockets: Infinity | ||
}); | ||
server.start(() => { | ||
await server.start(); | ||
server.connections.forEach((conn) => { | ||
for (let i = 0; i < 20; ++i) { | ||
Http.get({ | ||
path: '/', | ||
host: server.info.host, | ||
port: server.info.port, | ||
agent | ||
}, () => {}); | ||
} | ||
usedPorts.push(conn.info.port); | ||
await Utils.timeout(500); | ||
for (let i = 0; i < 20; ++i) { | ||
Http.get({ | ||
path: '/', | ||
host: conn.info.host, | ||
port: conn.info.port, | ||
agent: agent | ||
}, () => {}); | ||
} | ||
}); | ||
expect(network._requests).to.have.length(1); | ||
expect(network._requests[server.info.port]).to.exist(); | ||
expect(network._requests[server.info.port].total).to.equal(20); | ||
expect(network._requests[server.info.port].statusCodes[200]).to.equal(20); | ||
expect(network._responseTimes[server.info.port]).to.exist(); | ||
}); | ||
setTimeout(() => { | ||
it('resets stored statistics', async () => { | ||
expect(network._requests).to.have.length(2); | ||
let port = usedPorts.shift(); | ||
while (port) { | ||
expect(network._requests[port]).to.exist(); | ||
expect(network._requests[port].total).to.equal(20); | ||
expect(network._requests[port].statusCodes[200]).to.equal(20); | ||
expect(network._responseTimes[port]).to.exist(); | ||
port = usedPorts.shift(); | ||
} | ||
done(); | ||
}, 500); | ||
const server = new Hapi.Server({ | ||
host: 'localhost' | ||
}); | ||
}); | ||
it('resets stored statistics', (done) => { | ||
const server = new Hapi.Server(); | ||
server.connection({ host: 'localhost' }); | ||
server.route({ | ||
method: 'GET', | ||
path: '/', | ||
handler: (request, reply) => { | ||
handler: (request, h) => { | ||
reply(); | ||
return 'ok'; | ||
} | ||
@@ -106,143 +95,161 @@ }); | ||
const network = new Network(server); | ||
const agent = new Http.Agent({ maxSockets: Infinity }); | ||
const agent = new Http.Agent({ | ||
maxSockets: Infinity | ||
}); | ||
server.start(() => { | ||
await server.start(); | ||
for (let i = 0; i < 10; ++i) { | ||
Http.get({ | ||
path: '/', | ||
host: server.info.host, | ||
port: server.info.port, | ||
agent: agent | ||
}, () => {}); | ||
} | ||
for (let i = 0; i < 10; ++i) { | ||
Http.get({ | ||
path: '/', | ||
host: server.info.host, | ||
port: server.info.port, | ||
agent | ||
}, () => {}); | ||
} | ||
await Utils.timeout(300); | ||
setTimeout(() => { | ||
const port = server.info.port; | ||
const port = server.info.port; | ||
expect(network._requests[port]).to.exist(); | ||
expect(network._requests[port].total).to.equal(10); | ||
expect(network._requests[port].statusCodes[200]).to.equal(10); | ||
expect(network._requests[port]).to.exist(); | ||
expect(network._requests[port].total).to.equal(10); | ||
expect(network._requests[port].statusCodes[200]).to.equal(10); | ||
expect(network._responseTimes[port]).to.exist(); | ||
expect(network._responseTimes[port]).to.exist(); | ||
network.reset(); | ||
network.reset(); | ||
expect(network._requests[port]).to.equal({ | ||
total: 0, | ||
disconnects: 0, | ||
statusCodes: {} | ||
}); | ||
expect(network._requests[port]).to.deep.equal({ | ||
total: 0, | ||
disconnects: 0, | ||
statusCodes: {} | ||
}); | ||
expect(network._responseTimes[port]).to.deep.equal({ | ||
count: 0, | ||
total: 0, | ||
max: 0 | ||
}); | ||
done(); | ||
}, 300); | ||
expect(network._responseTimes[port]).to.equal({ | ||
count: 0, | ||
total: 0, | ||
max: 0 | ||
}); | ||
}); | ||
it('reports on socket information', { skip: false }, (done) => { | ||
it('reports on socket information', async () => { | ||
const server = new Hapi.Server(); | ||
server.connection({ host: 'localhost' }); | ||
const server = new Hapi.Server({ | ||
host: 'localhost' | ||
}); | ||
const upstream = new Hapi.Server(); | ||
upstream.connection({ | ||
const upstreamsecure = new Hapi.Server({ | ||
host: 'localhost', | ||
tls: { | ||
key: Fs.readFileSync(process.cwd() + '/test/fixtures/server.key', { encoding: 'utf8' }), | ||
cert: Fs.readFileSync(process.cwd() + '/test/fixtures/server.crt', { encoding: 'utf8' }) | ||
key: Fs.readFileSync(process.cwd() + '/test/fixtures/server.key', { | ||
encoding: 'utf8' | ||
}), | ||
cert: Fs.readFileSync(process.cwd() + '/test/fixtures/server.crt', { | ||
encoding: 'utf8' | ||
}) | ||
} | ||
}); | ||
upstream.route({ | ||
const upstream = new Hapi.Server({ | ||
host: 'localhost' | ||
}); | ||
const upstreamRoute = { | ||
method: 'GET', | ||
path: '/', | ||
handler: (request, reply) => {/* trap the request here */} | ||
}); | ||
handler: async (request, h) => { | ||
upstream.start(() => { | ||
await Utils.timeout(500); | ||
const httpAgent = new Http.Agent({ maxSockets: Infinity }); | ||
const httpsAgent = new Https.Agent({ maxSockets: Infinity }); | ||
const network = new Network(server, httpAgent, httpsAgent); | ||
return 'ok'; | ||
} | ||
}; | ||
server.route({ | ||
method: 'GET', | ||
path: '/', | ||
handler: (request, reply) => { | ||
upstreamsecure.route(upstreamRoute); | ||
Https.get({ | ||
hostname: upstream.info.host, | ||
port: upstream.info.port, | ||
path: '/', | ||
agent: httpsAgent, | ||
rejectUnauthorized: false | ||
}); | ||
} | ||
}); | ||
upstream.route(upstreamRoute); | ||
server.route({ | ||
method: 'GET', | ||
path: '/foo', | ||
handler: (request, reply) => { | ||
await upstreamsecure.start(); | ||
await upstream.start(); | ||
setTimeout(() => { | ||
const httpAgent = new Http.Agent({ | ||
maxSockets: Infinity | ||
}); | ||
const httpsAgent = new Https.Agent({ | ||
maxSockets: Infinity | ||
}); | ||
const network = new Network(server, httpAgent, httpsAgent); | ||
reply(); | ||
}, Math.floor(Math.random() * 10) + 1); | ||
} | ||
}); | ||
server.route({ | ||
method: 'GET', | ||
path: '/', | ||
handler: (request, h) => { | ||
server.start(() => { | ||
Https.get({ | ||
hostname: upstreamsecure.info.host, | ||
port: upstreamsecure.info.port, | ||
path: '/', | ||
agent: httpsAgent, | ||
rejectUnauthorized: false | ||
}); | ||
for (let i = 0; i < 10; ++i) { | ||
Http.get({ | ||
path: '/', | ||
host: server.info.host, | ||
port: server.info.port, | ||
agent: httpAgent | ||
}); | ||
Http.get({ | ||
hostname: upstream.info.host, | ||
port: upstream.info.port, | ||
path: '/', | ||
agent: httpAgent, | ||
rejectUnauthorized: false | ||
}); | ||
Http.get({ | ||
path: '/foo', | ||
host: server.info.host, | ||
port: server.info.port, | ||
agent: httpAgent | ||
}); | ||
} | ||
return 'ok'; | ||
} | ||
}); | ||
setTimeout(() => { | ||
server.route({ | ||
method: 'GET', | ||
path: '/foo', | ||
handler: async (request, h) => { | ||
Items.parallel.execute({ | ||
concurrents: network.concurrents.bind(network), | ||
response: network.responseTimes.bind(network), | ||
sockets: network.sockets.bind(network) | ||
}, (err, results) => { | ||
await Utils.timeout(Math.floor(Math.random() * 10) + 1); | ||
const port = server.info.port; | ||
return 'ok'; | ||
} | ||
}); | ||
expect(err).to.not.exist(); | ||
expect(results.concurrents[port]).to.be.a.number(); | ||
await server.start(); | ||
expect(results.sockets.http.total).to.be.at.least(10); | ||
expect(results.sockets.https.total).to.be.at.least(10); | ||
for (let i = 0; i < 10; ++i) { | ||
Http.get({ | ||
path: '/', | ||
host: server.info.host, | ||
port: server.info.port, | ||
agent: httpAgent | ||
}); | ||
expect(results.response[port].avg).to.be.at.least(1); | ||
expect(results.response[port].max).to.be.at.least(1); | ||
Http.get({ | ||
path: '/foo', | ||
host: server.info.host, | ||
port: server.info.port, | ||
agent: httpAgent | ||
}); | ||
} | ||
done(); | ||
}); | ||
}, 300); | ||
}); | ||
}); | ||
await Utils.timeout(300); | ||
const [response, sockets] = await Promise.all([ | ||
network.responseTimes(), | ||
network.sockets() | ||
]); | ||
const port = server.info.port; | ||
expect(sockets.http.total).to.be.at.least(10); | ||
expect(sockets.https.total).to.be.equal(10); | ||
expect(response[port].avg).to.be.at.least(1); | ||
expect(response[port].max).to.be.at.least(1); | ||
}); | ||
it('tracks server disconnects', (done) => { | ||
it('tracks server disconnects', async () => { | ||
@@ -274,4 +281,5 @@ class TestStream extends Stream.Readable { | ||
const server = new Hapi.Server(); | ||
server.connection({ host: 'localhost' }); | ||
const server = new Hapi.Server({ | ||
host: 'localhost' | ||
}); | ||
@@ -281,5 +289,5 @@ server.route({ | ||
path: '/', | ||
handler: (request, reply) => { | ||
handler: (request, h) => { | ||
reply(new TestStream()); | ||
return new TestStream(); | ||
} | ||
@@ -290,59 +298,41 @@ }); | ||
server.start(() => { | ||
await server.start(); | ||
const options = { | ||
hostname: server.info.host, | ||
port: server.info.port, | ||
path: '/', | ||
method: 'POST' | ||
}; | ||
const options = { | ||
hostname: server.info.host, | ||
port: server.info.port, | ||
path: '/', | ||
method: 'POST' | ||
}; | ||
const req = Http.request(options, (res) => { | ||
const req = Http.request(options, (res) => { | ||
req.destroy(); | ||
}); | ||
req.end('{}'); | ||
req.destroy(); | ||
}); | ||
setTimeout(() => { | ||
req.end('{}'); | ||
network.requests((err, result) => { | ||
await Utils.timeout(700); | ||
expect(err).to.not.exist(); | ||
const requests = {}; | ||
requests[server.info.port] = { total: 1, disconnects: 1, statusCodes: { '200': 1 } }; | ||
const result = await network.requests(); | ||
expect(result).to.deep.equal(requests); | ||
server.stop(done); | ||
}); | ||
}, 400); | ||
}); | ||
it('error checks getConnections', (done) => { | ||
const ee = new Events.EventEmitter(); | ||
ee.connections = [{ | ||
listener: { | ||
getConnections: (callback) => { | ||
callback(new Error('mock error')); | ||
} | ||
const requests = {}; | ||
requests[server.info.port] = { | ||
total: 1, | ||
disconnects: 1, | ||
statusCodes: { | ||
'200': 1 | ||
} | ||
}]; | ||
ee.ext = () => {}; | ||
}; | ||
const network = new Network(ee); | ||
expect(result).to.equal(requests); | ||
network.concurrents((err) => { | ||
expect(err.message).to.equal('mock error'); | ||
done(); | ||
}); | ||
return server.stop(); | ||
}); | ||
it('does not throw if request.response is null', (done) => { | ||
it('does not throw if request.response is null', async () => { | ||
const server = new Hapi.Server(); | ||
server.connection({ host: 'localhost' }); | ||
const server = new Hapi.Server({ | ||
host: 'localhost' | ||
}); | ||
@@ -352,5 +342,5 @@ server.route({ | ||
path: '/', | ||
handler: (request, reply) => { | ||
handler: (request, h) => { | ||
reply(); | ||
return 'ok'; | ||
} | ||
@@ -360,3 +350,3 @@ }); | ||
// force response to be null to mimic client disconnect | ||
server.on('response', (request) => { | ||
server.events.on('response', (request) => { | ||
@@ -368,4 +358,6 @@ request.response = null; | ||
server.start(() => { | ||
await server.start(); | ||
new Promise((resolve) => { | ||
Http.get({ | ||
@@ -379,4 +371,4 @@ path: '/', | ||
expect(network._requests[server.info.port].total).to.equal(1); | ||
expect(network._requests[server.info.port].statusCodes).to.deep.equal({}); | ||
done(); | ||
expect(network._requests[server.info.port].statusCodes).to.equal({}); | ||
resolve(); | ||
}); | ||
@@ -386,2 +378,3 @@ }); | ||
}); | ||
describe('os information', () => { | ||
@@ -391,12 +384,9 @@ | ||
it('returns an object with the current memory usage', (done) => { | ||
it('returns an object with the current memory usage', async () => { | ||
Os.mem((err, mem) => { | ||
const mem = await Os.mem(); | ||
expect(err).to.not.exist(); | ||
expect(mem).to.exist(); | ||
expect(mem.total).to.exist(); | ||
expect(mem.free).to.exist(); | ||
done(); | ||
}); | ||
expect(mem).to.exist(); | ||
expect(mem.total).to.exist(); | ||
expect(mem.free).to.exist(); | ||
}); | ||
@@ -406,10 +396,7 @@ }); | ||
it('returns an object with the current load average', (done) => { | ||
it('returns an object with the current load average', async () => { | ||
Os.loadavg((err, load) => { | ||
const load = await Os.loadavg(); | ||
expect(err).to.not.exist(); | ||
expect(load).to.have.length(3); | ||
done(); | ||
}); | ||
expect(load).to.have.length(3); | ||
}); | ||
@@ -419,11 +406,8 @@ }); | ||
it('returns an object with the current uptime', (done) => { | ||
it('returns an object with the current uptime', async () => { | ||
Os.uptime((err, uptime) => { | ||
const uptime = await Os.uptime(); | ||
expect(err).to.not.exist(); | ||
expect(uptime).to.exist(); | ||
expect(uptime).to.be.a.number(); | ||
done(); | ||
}); | ||
expect(uptime).to.exist(); | ||
expect(uptime).to.be.a.number(); | ||
}); | ||
@@ -436,22 +420,17 @@ }); | ||
it('passes the current memory usage to the callback', (done) => { | ||
it('passes the current memory usage to the callback', async () => { | ||
Process.memoryUsage((err, mem) => { | ||
const mem = await Process.memoryUsage(); | ||
expect(err).not.to.exist(); | ||
expect(mem).to.exist(); | ||
done(); | ||
}); | ||
expect(mem).to.exist(); | ||
}); | ||
}); | ||
describe('delay()', () => { | ||
it('passes the current event queue delay to the callback', (done) => { | ||
it('passes the current event queue delay to the callback', async () => { | ||
Process.delay((err, delay) => { | ||
const delay = await Process.delay(); | ||
expect(err).not.to.exist(); | ||
expect(delay).to.exist(); | ||
done(); | ||
}); | ||
expect(delay).to.exist(); | ||
}); | ||
@@ -458,0 +437,0 @@ }); |
@@ -11,2 +11,3 @@ 'use strict'; | ||
const Network = require('../lib/network'); | ||
const Utils = require('../lib/utils'); | ||
const Oppsy = require('../lib'); | ||
@@ -26,3 +27,3 @@ | ||
it('is an event EventEmitter', (done) => { | ||
it('is an event EventEmitter', () => { | ||
@@ -32,15 +33,14 @@ const opps = new Oppsy(new Hapi.Server(), {}); | ||
expect(opps.on).to.be.a.function(); | ||
done(); | ||
}); | ||
it('creates a network monitor and a map of tasks', (done) => { | ||
it('creates a network monitor and a map of tasks', () => { | ||
const opps = new Oppsy(new Hapi.Server()); | ||
expect(opps._networkMonitor).to.be.an.instanceof(Network); | ||
expect(opps._tasks).to.include(['osload','osmem','osup','psup','psmem','psdelay','requests','concurrents','responseTimes','sockets']); | ||
done(); | ||
expect(opps._tasks).to.include(['osload', 'osmem', 'osup', 'psup', 'psmem', 'psdelay', 'requests', 'responseTimes', 'sockets']); | ||
}); | ||
}); | ||
describe('start()', () => { | ||
it('emits an "ops" event at the specified interval', (done) => { | ||
it('emits an "ops" event at the specified interval', () => { | ||
@@ -51,12 +51,11 @@ let count = 0; | ||
opps._tasks = { | ||
one: (callback) => { | ||
one: () => { | ||
return callback(null, 'foo'); | ||
return 'foo'; | ||
}, | ||
two: (callback) => { | ||
two: async () => { | ||
setTimeout(() => { | ||
await Utils.timeout(40); | ||
return callback(null, 'bar'); | ||
}, 40); | ||
return 'bar'; | ||
} | ||
@@ -68,3 +67,3 @@ }; | ||
count++; | ||
expect(data).to.deep.equal({ | ||
expect(data).to.equal({ | ||
one: 'foo', | ||
@@ -78,8 +77,20 @@ two: 'bar', | ||
}); | ||
opps.on('stop', done); | ||
opps.start(100); | ||
return new Promise((resolve, reject) => { | ||
opps.on('stop', (err) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(); | ||
}); | ||
opps.start(100); | ||
}); | ||
}); | ||
it('emits an error if one occurs during processing', (done) => { | ||
it('emits an error if one occurs during processing', () => { | ||
let count = 0; | ||
@@ -90,12 +101,16 @@ const host = Os.hostname(); | ||
opps._tasks = { | ||
one: (callback) => { | ||
one: () => { | ||
return callback(null, 'foo'); | ||
return 'foo'; | ||
}, | ||
two: (callback) => { | ||
two: () => { | ||
if (count % 2 === 0) { | ||
return callback(new Error('there was an error')); | ||
} | ||
callback(null, 'bar'); | ||
return new Promise((resolve, reject) => { | ||
if (count % 2 === 0) { | ||
reject(new Error('there was an error')); | ||
} | ||
return resolve('bar'); | ||
}); | ||
} | ||
@@ -107,3 +122,3 @@ }; | ||
count++; | ||
expect(data).to.deep.equal({ | ||
expect(data).to.equal({ | ||
one: 'foo', | ||
@@ -114,14 +129,20 @@ two: 'bar', | ||
}); | ||
opps.on('error', (error) => { | ||
expect(error).to.be.an.instanceof(Error); | ||
expect(error.message).to.equal('there was an error'); | ||
opps.stop(); | ||
done(); | ||
return new Promise((resolve) => { | ||
opps.on('error', (error) => { | ||
expect(error).to.be.an.instanceof(Error); | ||
expect(error.message).to.equal('there was an error'); | ||
opps.stop(); | ||
resolve(); | ||
}); | ||
opps.start(100); | ||
}); | ||
opps.start(100); | ||
}); | ||
it('does not emit the event after it is stopped', (done) => { | ||
it('does not emit the event after it is stopped', async () => { | ||
let count = 0; | ||
@@ -131,5 +152,5 @@ const opps = new Oppsy(new Hapi.Server()); | ||
opps._tasks = { | ||
one: (callback) => { | ||
one: () => { | ||
return callback(null, 'foo'); | ||
return 'foo'; | ||
} | ||
@@ -143,15 +164,12 @@ }; | ||
opps.stop(); | ||
setTimeout(() => { | ||
expect(count).to.equal(0); | ||
done(); | ||
}, 500); | ||
await Utils.timeout(500); | ||
expect(count).to.equal(0); | ||
}); | ||
}); | ||
it('emits "ops" events with data', (done) => { | ||
it('emits "ops" events with data', async () => { | ||
let _data = {}; | ||
const server = new Hapi.Server(); | ||
server.connection({ host: 'localhost' }); | ||
@@ -165,18 +183,21 @@ const opps = new Oppsy(new Hapi.Server()); | ||
opps.start(100); | ||
setTimeout(() => { | ||
expect(_data.requests).to.deep.equal({}); | ||
expect(_data.concurrents).to.deep.equal({}); | ||
expect(_data.responseTimes).to.deep.equal({}); | ||
expect(_data.sockets).to.deep.equal({ | ||
http: { total: 0 }, | ||
https: { total: 0 } | ||
}); | ||
expect(_data.osload).to.have.length(3); | ||
expect(_data.osmem).to.contain('total', 'free'); | ||
expect(_data).to.contain('osup', 'psup', 'psdelay', 'host'); | ||
expect(_data.psmem).to.contain('rss', 'heapTotal', 'heapUsed'); | ||
done(); | ||
}, 500); | ||
await Utils.timeout(500); | ||
expect(_data.requests).to.equal({}); | ||
expect(_data.responseTimes).to.equal({}); | ||
expect(_data.sockets).to.equal({ | ||
http: { | ||
total: 0 | ||
}, | ||
https: { | ||
total: 0 | ||
} | ||
}); | ||
expect(_data.osload).to.have.length(3); | ||
expect(_data.osmem).to.contain(['total', 'free']); | ||
expect(_data).to.contain(['osup', 'psup', 'psdelay', 'host']); | ||
expect(_data.psmem).to.contain(['rss', 'heapTotal', 'heapUsed']); | ||
expect(_data.pscpu).to.contain(['user', 'system']); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
1
-50%655
1.55%29826
-4.79%+ Added
- Removed
- Removed
- Removed
Updated