Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('works', () => {
expect(utils.isAtHome()).to.be.eql(false);
process.env[ENV_VARS.IS_AT_HOME] = 1;
expect(utils.isAtHome()).to.be.eql(true);
delete process.env[ENV_VARS.IS_AT_HOME];
expect(utils.isAtHome()).to.be.eql(false);
});
});
it('works', () => {
expect(utils.isAtHome()).to.be.eql(false);
process.env[ENV_VARS.IS_AT_HOME] = 1;
expect(utils.isAtHome()).to.be.eql(true);
delete process.env[ENV_VARS.IS_AT_HOME];
expect(utils.isAtHome()).to.be.eql(false);
});
});
await snapshotter.start();
await emitAndWait(10);
await emitAndWait(10);
await emitAndWait(10);
await emitAndWait(0);
await snapshotter.stop();
const cpuSnapshots = snapshotter.getCpuSample();
expect(cpuSnapshots).to.have.lengthOf(4);
cpuSnapshots.forEach((ss, i) => {
expect(ss.createdAt).to.be.a('date');
expect(ss.isOverloaded).to.be.a('boolean');
expect(ss.isOverloaded).to.be.eql(i % 2 === 0);
});
} finally {
delete process.env[ENV_VARS.IS_AT_HOME];
}
});
it('correctly marks CPU overloaded using Platform event', async () => {
process.env[ENV_VARS.IS_AT_HOME] = '1';
let count = 0;
const emitAndWait = async (delay) => {
events.emit(ACTOR_EVENT_NAMES.SYSTEM_INFO, {
isCpuOverloaded: count % 2 === 0,
createdAt: (new Date()).toISOString(),
cpuCurrentUsage: 66.6,
});
count++;
await Apify.utils.sleep(delay);
};
try {
const snapshotter = new Snapshotter();
await snapshotter.start();
await emitAndWait(10);
await emitAndWait(10);
after(() => {
delete process.env[ENV_VARS.IS_AT_HOME];
});
it('should work', async () => {
it('works', async () => {
const runId = 'my-run-id';
const expectedEventTypes = ['ACTOR.RUN.SUCCEEDED'];
const expectedRequestUrl = 'http://example.com/api';
const expectedPayloadTemplate = '{"hello":{{world}}';
const expectedIdempotencyKey = 'some-key';
process.env[ENV_VARS.ACTOR_RUN_ID] = runId;
process.env[ENV_VARS.IS_AT_HOME] = '1';
const webhooksMock = sinon.mock(Apify.client.webhooks);
const webhook = {
isAdHoc: true,
eventTypes: expectedEventTypes,
condition: {
actorRunId: runId,
},
requestUrl: expectedRequestUrl,
payloadTemplate: expectedPayloadTemplate,
idempotencyKey: expectedIdempotencyKey,
};
webhooksMock.expects('createWebhook')
.withExactArgs({ webhook })
before(() => {
logLevel = log.getLevel();
log.setLevel(log.LEVELS.ERROR);
process.env[ENV_VARS.IS_AT_HOME] = 1;
});
webhooksMock.expects('createWebhook')
.withExactArgs({ webhook })
.once()
.returns(Promise.resolve());
await Apify.addWebhook({
eventTypes: expectedEventTypes,
requestUrl: expectedRequestUrl,
payloadTemplate: expectedPayloadTemplate,
idempotencyKey: expectedIdempotencyKey,
});
delete process.env[ENV_VARS.ACTOR_RUN_ID];
delete process.env[ENV_VARS.IS_AT_HOME];
webhooksMock.verify();
});
after(() => {
log.setLevel(logLevel);
delete process.env[ENV_VARS.IS_AT_HOME];
});
export const isAtHome = () => !!process.env[ENV_VARS.IS_AT_HOME];