From da3b382f00f36f9a69425972f7ddb49653f99b9c Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Wed, 25 Sep 2024 23:26:01 -0500 Subject: [PATCH] Test that sys.exit works in a Python script --- .../java/org/apposed/appose/ApposeTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/test/java/org/apposed/appose/ApposeTest.java b/src/test/java/org/apposed/appose/ApposeTest.java index eccccc6..2f5c572 100644 --- a/src/test/java/org/apposed/appose/ApposeTest.java +++ b/src/test/java/org/apposed/appose/ApposeTest.java @@ -192,6 +192,28 @@ public void testStartupCrash() throws InterruptedException, IOException { assertEquals(expectedLines, service.errorLines()); } + @Test + public void testPythonSysExit() throws InterruptedException, IOException { + Environment env = Appose.system(); + try (Service service = env.python()) { + maybeDebug(service); + + // Create a task that calls sys.exit. This is a nasty thing to do + // because Python does not exit the worker process when sys.exit is + // called within a dedicated threading.Thread; the thread just dies. + // So in addition to testing the Java code here, we are also testing + // that Appose's python_worker handles this situation well. + Task task = service.task("import sys\nsys.exit(123)"); + + // Launch the task and wait for it to finish. + task.waitFor(); + + // Is the tag flagged as failed due to thread death? + assertSame(TaskStatus.FAILED, task.status); + assertEquals("thread death", task.error); + } + } + @Test public void testCrashWithActiveTask() throws InterruptedException, IOException { Environment env = Appose.system();