Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions apps/desktop/src/app/DesktopLinuxUrlHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, describe, it } from "@effect/vitest";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Cause from "effect/Cause";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
Expand All @@ -11,6 +14,7 @@ import * as PlatformError from "effect/PlatformError";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";
import * as ChildProcess from "effect/process/ChildProcess";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";

import * as DesktopEnvironment from "./DesktopEnvironment.ts";
Expand Down Expand Up @@ -57,6 +61,7 @@ const layerHandler = (
recorded: RecordedRegistration,
input: {
readonly environment?: Record<string, unknown>;
readonly processSpawner?: ChildProcessSpawner.ChildProcessSpawner["Service"];
readonly updateDesktopDatabaseExitCode?: number;
readonly updateDesktopDatabaseStalled?: boolean;
readonly updateDesktopDatabaseStarted?: Deferred.Deferred<void>;
Expand Down Expand Up @@ -107,6 +112,7 @@ const layerHandler = (
Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
if (input.processSpawner) return input.processSpawner.spawn(command);
const childProcess = command as unknown as {
readonly command: string;
readonly args: ReadonlyArray<string>;
Expand Down Expand Up @@ -370,6 +376,69 @@ describe("DesktopLinuxUrlHandler", () => {
});
});

it.effect.skipIf(HostProcess.Platform.defaultValue() !== "linux")(
"continues registration after a timed-out cache helper ignores SIGTERM",
() =>
Effect.gen(function* () {
const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const nodeExecutable = yield* HostProcess.ExecutablePath;
const recorded = emptyRecording();
const spawned = yield* Deferred.make<ChildProcessSpawner.ChildProcessHandle>();
const ready = yield* Deferred.make<void>();
const processSpawner = ChildProcessSpawner.make((command) =>
Effect.gen(function* () {
if (!ChildProcess.isStandardCommand(command)) {
return yield* Effect.die("expected a single registration command");
}
recorded.commands.push({ command: command.command, args: command.args });
if (command.command !== "update-desktop-database") return mockProcess(0);
const child = yield* realSpawner.spawn(
ChildProcess.make(
nodeExecutable,
[
"-e",
// The watchdog makes the unfixed case exit with a distinguishable code.
"process.on('SIGTERM', () => setTimeout(() => process.exit(99), 5000));" +
"process.stdout.write('ready\\n'); setInterval(() => {}, 2147483647);",
],
{ ...command.options, stdout: "pipe" },
),
);
yield* Deferred.succeed(spawned, child);
yield* Stream.runHead(child.stdout);
yield* Deferred.succeed(ready, undefined);
return child;
}),
);
const registration = yield* runRegister(recorded, { processSpawner }).pipe(
Effect.forkScoped,
);
const child = yield* Deferred.await(spawned);
yield* Effect.addFinalizer(() =>
child.isRunning.pipe(
Effect.flatMap((running) =>
running ? child.kill({ killSignal: "SIGKILL" }) : Effect.void,
),
Effect.ignore,
),
);
yield* Deferred.await(ready);
yield* TestClock.adjust("5 seconds");
yield* Fiber.join(registration);

const exit = yield* Effect.exit(child.exitCode);
if (Exit.isFailure(exit)) {
assert.include(Cause.pretty(exit.cause), "SIGKILL");
} else {
assert.fail(`cache helper exited with ${exit.value} instead of being force-killed`);
}
assert.deepEqual(
recorded.commands.map(({ command }) => command),
["update-desktop-database", "xdg-mime"],
);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);

it.effect("continues to xdg-mime when the desktop MIME cache refresh stalls", () =>
Effect.gen(function* () {
const recorded = emptyRecording();
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/app/DesktopLinuxUrlHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export const make = Effect.gen(function* () {
stdin: "ignore",
stdout: "ignore",
stderr: "ignore",
forceKillAfter: "1 second",
},
);
const handle = yield* spawner.spawn(command);
Expand Down
Loading