Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const server = new Hapi.Server()
await server.register({
plugin: require('../lib'),
options: {
signals: 'HAPIPULSE'
}
})
await server.start()
// a stopped hapi server has a "started" timestamp of 0
expect(server.info.started).to.not.equal(0)
process.emit('HAPIPULSE')
// wait for the server to stop
await Hoek.wait(100)
Sinon.assert.called(process.exit)
// a stopped hapi server has a "started" timestamp of 0
expect(server.info.started).to.equal(0)
})
})
}
};
let d = 0;
client.onDisconnect = (willReconnect, log) => ++d;
await client.connect();
expect(client._heartbeatTimeout).to.equal(380);
await client.request('/');
await Hoek.wait(520);
expect(d).to.equal(0);
client._onMessage = Hoek.ignore; // Stop processing messages
await Hoek.wait(480);
expect(d).to.equal(1);
client.disconnect();
await server.stop();
});
});
const orig = client.connection.get;
client.connection.get = async function (key) { // Delayed get
await Hoek.wait(100);
return orig.call(client.connection, key);
};
const policy = new Catbox.Policy(rule, client, 'test-segment');
await client.start();
const { value: value1, report: report1 } = await policy.get('test'); // Cache lookup takes 100 + generate 50
expect(value1.gen).to.equal(1); // Fresh
expect(report1.error).to.not.exist();
await Hoek.wait(210); // Wait for stale
const { value: value2, report: report2 } = await policy.get('test'); // Cache lookup takes 100, generate comes back after 50
expect(value2.gen).to.equal(2); // Fresh
expect(report2.error).to.not.exist();
const { value: value3, report: report3 } = await policy.get('test'); // Cache lookup takes 100
expect(value3.gen).to.equal(2); // Cached (100 left to stale)
expect(report3.error).to.not.exist();
client.connection.get = orig;
});
const server = new Hapi.Server()
await server.register({
plugin: require('../lib'),
options: {
logger: { error: () => {} },
signals: 'ERRORPULSE',
postServerStop: stub
}
})
await server.start()
process.emit('ERRORPULSE')
// wait for the server to stop
await Hoek.wait(100)
expect(process.listenerCount('ERRORPULSE')).to.equal(0)
Sinon.assert.called(process.exit)
Sinon.assert.called(stub)
// a stopped hapi server has a "started" timestamp of 0
expect(server.info.started).to.equal(0)
})
it('stops the server on SIGTERM', async () => {
await server.start()
// a stopped hapi server has a "started" timestamp of 0
expect(server.info.started).to.not.equal(0)
process.emit('SIGTERM')
// wait for the server to stop
await Hoek.wait(100)
Sinon.assert.called(process.exit)
// a stopped hapi server has a "started" timestamp of 0
expect(server.info.started).to.equal(0)
})
handler: async () => {
await Hoek.wait(440);
return 'hello';
}
});
const method = async function (id) {
await Hoek.wait(50);
return { id, gen: ++gen };
};
let buffer;
const handler = (request) => {
buffer = buffer || Buffer.alloc(2048);
return 'ok';
};
const log = server.events.once('log');
server.route({ method: 'GET', path: '/', handler });
await server.start();
const res1 = await server.inject('/');
expect(res1.statusCode).to.equal(200);
await Hoek.wait(10);
const res2 = await server.inject('/');
expect(res2.statusCode).to.equal(503);
const [event, tags] = await log;
expect(event.channel).to.equal('internal');
expect(event.data.rss > 10000).to.equal(true);
expect(tags.load).to.be.true();
await server.stop();
});
});
const aHandler = async (data) => {
await Hoek.wait(50);
updates.push({ a: data, id: 1 });
};
it('fails check due to high rss load', async () => {
const heavy = new Heavy({ sampleInterval: 5, maxRssBytes: 1 });
heavy.start();
expect(() => heavy.check()).to.not.throw();
await Hoek.wait(10);
expect(() => heavy.check()).to.throw('Server under heavy load (rss)');
expect(heavy.load.rss).to.be.above(10000);
heavy.stop();
});