Repository: denoland/deno
File: tests/unit_node/os_test.ts (TypeScript)

Code before:
  name: "os.setPriority() & os.getPriority()",
  // disabled because os.getPriority() doesn't work without sudo
  ignore: true,
  fn() {
    const child = new Deno.Command(Deno.execPath(), {
      args: ["eval", "while (true) { console.log('foo') }"],
    }).spawn();
    const originalPriority = os.getPriority(child.pid);
    assertNotEquals(originalPriority, os.constants.priority.PRIORITY_HIGH);
    os.setPriority(child.pid, os.constants.priority.PRIORITY_HIGH);
    assertEquals(
      os.getPriority(child.pid),
      os.constants.priority.PRIORITY_HIGH,
    );
    os.setPriority(child.pid, originalPriority);
    assertEquals(os.getPriority(child.pid), originalPriority);
    child.kill();
  },
});

Deno.test({
  name:
    "os.setPriority() throw os permission denied error & os.getPriority() doesn't",
  async fn() {
    const child = new Deno.Command(Deno.execPath(), {
      args: ["eval", "while (true) { console.log('foo') }"],
    }).spawn();
    assertThrows(
      () => {
        try {
          os.setPriority(child.pid, os.constants.priority.PRIORITY_HIGH);
        } catch (err) {
          console.error(err);
          throw err;
        }
      },
      Deno.errors.PermissionDenied,
    );
    os.getPriority(child.pid);
    child.kill();
    await child.status;
  },
});

// Gets the diff in log_10 scale
function diffLog10(a: number, b: number): number {
  return Math.abs(Math.log10(a) - Math.log10(b));
}

Deno.test({
  name:

Review comment (suggestion):
Though documentation on Windows is quite scarce, I got this from here: https://github.com/nodejs/node/blob/591ba692bfe30408e6a67397e7d18bfa1b9c3561/test/parallel/test-os-process-priority.js#L131-L132

The reason why previously this test passes on Windows is because `os.setPriority()` always throw no matter what. See #30405 

Code after:
  name: "os.setPriority() & os.getPriority()",
  // disabled because os.getPriority() doesn't work without sudo
  ignore: true,
  fn() {
    const child = new Deno.Command(Deno.execPath(), {
      args: ["eval", "while (true) { console.log('foo') }"],
    }).spawn();
    const originalPriority = os.getPriority(child.pid);
    assertNotEquals(originalPriority, os.constants.priority.PRIORITY_HIGH);
    os.setPriority(child.pid, os.constants.priority.PRIORITY_HIGH);
    assertEquals(
      os.getPriority(child.pid),
      os.constants.priority.PRIORITY_HIGH,
    );
    os.setPriority(child.pid, originalPriority);
    assertEquals(os.getPriority(child.pid), originalPriority);
    child.kill();
  },
});

Deno.test({
  name:
    "os.setPriority() throw os permission denied error & os.getPriority() doesn't",
  // Windows allows setting process priority without privilege escalation up to `PRIORITY_HIGH`.
  // Setting to `PRIORITY_HIGHEST` will be silently reduced to `PRIORITY_HIGH`.
  ignore: Deno.build.os === "windows",
  async fn() {
    const child = new Deno.Command(Deno.execPath(), {
      args: ["eval", "while (true) { console.log('foo') }"],
    }).spawn();
    assertThrows(
      () => {
        try {
          os.setPriority(child.pid, os.constants.priority.PRIORITY_HIGH);
        } catch (err) {
          console.error(err);
          throw err;
        }
      },
      Error,
      "uv_os_setpriority returned EACCES (permission denied)",
    );
    os.getPriority(child.pid);
    child.kill();
    await child.status;
  },
});

Deno.test({
  name: "os.setPriority() works on Windows",
  ignore: Deno.build.os !== "windows",