Path: blob/main/core/posix-node/src/spawn.test.ts
1396 views
import posix from "./index";12test("posix_spawn /bin/sleep and wait for it to finish using waitpid and confirm the time", () => {3const t0 = new Date().valueOf();4const pid = posix.posix_spawn?.(5"/bin/sleep",6null,7null,8["/bin/sleep", "0.5"],9{}10);11if (pid == null) {12throw Error("pid must be a positive integer");13}14const x = posix.waitpid?.(pid, 0);15if (x == null) {16throw Error("waitpid must return something");17}18const { ret, wstatus } = x;19expect(ret).toBe(pid);20expect(wstatus).toBe(0);21const tm = new Date().valueOf() - t0;22expect(tm > 400 && tm < 1000).toBe(true);23});2425test("posix_spawnp sleep and wait for it to finish using waitpid and confirm the time", () => {26const t0 = new Date().valueOf();27const pid = posix.posix_spawnp?.("sleep", null, null, ["sleep", "0.5"], {});28if (pid == null) {29throw Error("pid must be a positive integer");30}31const x = posix.waitpid?.(pid, 0);32if (x == null) {33throw Error("waitpid must return something");34}35const { ret, wstatus } = x;36expect(ret).toBe(pid);37expect(wstatus).toBe(0);38const tm = new Date().valueOf() - t0;39expect(tm > 400 && tm < 1000).toBe(true);40});4142test("calling posix_spawnp with invalid fd arg to addclose to throw", () => {43expect(() =>44posix.posix_spawnp?.("sleep", [["addclose", -1]], {}, ["sleep", "3"], {})45).toThrow("posix_spawn_file_actions_addclose failed");46});4748test("calling posix_spawnp with invalid new_fd arg to adddup2 to throw", () => {49expect(() =>50posix.posix_spawnp?.(51"sleep",52[["adddup2", 1, -100]],53{},54["sleep", "3"],55{}56)57).toThrow("posix_spawn_file_actions_adddup2 failed");58});5960test("calling posix_spawnp with invalid fd arg to addopen to throw", () => {61expect(() =>62posix.posix_spawnp?.(63"sleep",64[["addopen", -1, "/tmp/a", 0, 0]],65{},66["sleep", "3"],67{}68)69).toThrow("posix_spawn_file_actions_addopen failed");70});717273