1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

feat(std/node): add os.loadavg() (#4075)

This commit is contained in:
ecyrbe 2020-02-23 00:46:52 +01:00 committed by GitHub
parent c34d96d865
commit fb98556d56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 26 deletions

View file

@ -85,7 +85,7 @@ export {
ShutdownMode,
shutdown
} from "./net.ts";
export { dir, env, exit, isTTY, execPath, hostname } from "./os.ts";
export { dir, env, exit, isTTY, execPath, hostname, loadavg } from "./os.ts";
export {
permissions,
PermissionName,

View file

@ -80,6 +80,7 @@ export let OP_TRANSPILE: number;
export let OP_SIGNAL_BIND: number;
export let OP_SIGNAL_UNBIND: number;
export let OP_SIGNAL_POLL: number;
export let OP_LOADAVG: number;
const PLUGIN_ASYNC_HANDLER_MAP: Map<number, AsyncHandler> = new Map();

View file

@ -40,6 +40,12 @@ declare namespace Deno {
stderr: boolean;
};
/** Get the loadavg. Requires the `--allow-env` flag.
*
* console.log(Deno.loadavg());
*/
export function loadavg(): number[];
/** Get the hostname. Requires the `--allow-env` flag.
*
* console.log(Deno.hostname());

View file

@ -11,6 +11,14 @@ import * as util from "./util.ts";
export function isTTY(): { stdin: boolean; stdout: boolean; stderr: boolean } {
return sendSync(dispatch.OP_IS_TTY);
}
/** Get the loadavg.
* Requires the `--allow-env` flag.
*
* console.log(Deno.loadavg());
*/
export function loadavg(): number[] {
return sendSync(dispatch.OP_LOADAVG);
}
/** Get the hostname.
* Requires the `--allow-env` flag.

View file

@ -283,6 +283,23 @@ testPerm({ env: false }, function execPathPerm(): void {
assert(caughtError);
});
testPerm({ env: true }, function loadavgSuccess(): void {
const load = Deno.loadavg();
assertEquals(load.length, 3);
});
testPerm({ env: false }, function loadavgPerm(): void {
let caughtError = false;
try {
Deno.loadavg();
} catch (err) {
caughtError = true;
assert(err instanceof Deno.Err.PermissionDenied);
assertEquals(err.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm({ env: true }, function hostnameDir(): void {
assertNotEquals(Deno.hostname(), "");
});

View file

@ -19,6 +19,7 @@ pub fn init(i: &mut Isolate, s: &State) {
i.register_op("get_env", s.core_op(json_op(s.stateful_op(op_get_env))));
i.register_op("get_dir", s.core_op(json_op(s.stateful_op(op_get_dir))));
i.register_op("hostname", s.core_op(json_op(s.stateful_op(op_hostname))));
i.register_op("loadavg", s.core_op(json_op(s.stateful_op(op_loadavg))));
}
#[derive(Deserialize)]
@ -158,6 +159,22 @@ fn op_is_tty(
})))
}
fn op_loadavg(
state: &State,
_args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, ErrBox> {
state.check_env()?;
match sys_info::loadavg() {
Ok(loadavg) => Ok(JsonOp::Sync(json!([
loadavg.one,
loadavg.five,
loadavg.fifteen
]))),
Err(_) => Ok(JsonOp::Sync(json!([0f64, 0f64, 0f64]))),
}
}
fn op_hostname(
state: &State,
_args: Value,

View file

@ -133,12 +133,12 @@ export function hostname(): string {
return Deno.hostname();
}
/** Not yet implemented */
/** Returns an array containing the 1, 5, and 15 minute load averages */
export function loadavg(): number[] {
if (Deno.build.os == "win") {
return [0, 0, 0];
}
notImplemented(SEE_GITHUB_ISSUE);
return Deno.loadavg();
}
/** Not yet implemented */

View file

@ -1,10 +1,5 @@
const { test } = Deno;
import {
assert,
assertThrows,
assertEquals,
AssertionError
} from "../testing/asserts.ts";
import { assert, assertThrows, assertEquals } from "../testing/asserts.ts";
import * as os from "./os.ts";
test({
@ -168,26 +163,14 @@ test({
}
});
// Method is currently implemented correctly for windows but not for any other os
test({
name: "Load average is an array of 3 numbers",
fn() {
try {
const result = os.loadavg();
assert(result.length == 3);
assertEquals(typeof result[0], "number");
assertEquals(typeof result[1], "number");
assertEquals(typeof result[2], "number");
} catch (error) {
if (!(Object.getPrototypeOf(error) === Error.prototype)) {
const errMsg = `Unexpected error class: ${error.name}`;
throw new AssertionError(errMsg);
} else if (!error.message.includes("Not implemented")) {
throw new AssertionError(
"Expected this error to contain 'Not implemented'"
);
}
}
const result = os.loadavg();
assert(result.length == 3);
assertEquals(typeof result[0], "number");
assertEquals(typeof result[1], "number");
assertEquals(typeof result[2], "number");
}
});