summaryrefslogtreecommitdiffstats
path: root/toolkit/components/processtools/tests/xpcshell/test_process_kill.js
blob: 9781efaffc3d68301ed3106f7a3a121298b9fae0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* eslint-disable mozilla/no-arbitrary-setTimeout */
"use strict";

const { setTimeout } = ChromeUtils.importESModule(
  "resource://gre/modules/Timer.sys.mjs"
);

const { Subprocess } = ChromeUtils.importESModule(
  "resource://gre/modules/Subprocess.sys.mjs"
);

const ProcessTools = Cc["@mozilla.org/processtools-service;1"].getService(
  Ci.nsIProcessToolsService
);

let PYTHON;

// Find Python.
add_task(async function setup() {
  PYTHON = await Subprocess.pathSearch(Services.env.get("PYTHON"));
});

// Ensure that killing a process... kills the process.
add_task(async function test_subprocess_kill() {
  // We launch Python, as it's a long-running process and it exists
  // on all desktop platforms on which we run tests.
  let proc = await Subprocess.call({
    command: PYTHON,
    arguments: [],
  });

  let isTerminated = false;

  proc.wait().then(() => {
    isTerminated = true;
  });

  await new Promise(resolve => setTimeout(resolve, 100));
  Assert.ok(
    !isTerminated,
    "We haven't killed the process yet, it should still be running."
  );

  // Time to kill the process.
  ProcessTools.kill(proc.pid);

  await new Promise(resolve => setTimeout(resolve, 100));
  Assert.ok(
    isTerminated,
    "We have killed the process already, it shouldn't be running anymore."
  );
});