Skip to content

Commit

Permalink
feat(std/node): add os.loadavg()
Browse files Browse the repository at this point in the history
  • Loading branch information
ecyrbe committed Feb 22, 2020
1 parent bd640bc commit bacfa69
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cli/js/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions cli/js/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 6 additions & 0 deletions cli/js/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
8 changes: 8 additions & 0 deletions cli/js/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions cli/js/os_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "");
});
Expand Down
17 changes: 17 additions & 0 deletions cli/ops/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions std/node/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
29 changes: 6 additions & 23 deletions std/node/os_test.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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");
}
});

Expand Down

0 comments on commit bacfa69

Please sign in to comment.