Skip to content

Commit d82a0e5

Browse files
committed
Fix test failures on Linux CI for Maven Central publish
- Make 4 resolvePythonExecutable tests OS-aware (Windows/Unix layouts) - Fix ensureMinPool() race: add instance before incrementing currentSize
1 parent ee3de14 commit d82a0e5

2 files changed

Lines changed: 53 additions & 25 deletions

File tree

python-embed-runtime/src/main/java/io/github/howtis/pythonembed/PythonEmbedPool.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,18 +1264,15 @@ public int healthCheck() {
12641264
private void ensureMinPool() {
12651265
if (closed) return;
12661266

1267-
int current;
1268-
while ((current = currentSize.get()) < minPool) {
1269-
if (currentSize.compareAndSet(current, current + 1)) {
1270-
try {
1271-
PythonEmbed embed = createEmbed();
1272-
instances.add(new PooledInstance(embed));
1273-
} catch (Exception e) {
1274-
currentSize.decrementAndGet();
1275-
logger.log(Level.WARNING,
1276-
"Failed to create instance for minPool guarantee", e);
1277-
break;
1278-
}
1267+
while (currentSize.get() < minPool) {
1268+
try {
1269+
PythonEmbed embed = createEmbed();
1270+
instances.add(new PooledInstance(embed));
1271+
currentSize.incrementAndGet();
1272+
} catch (Exception e) {
1273+
logger.log(Level.WARNING,
1274+
"Failed to create instance for minPool guarantee", e);
1275+
break;
12791276
}
12801277
}
12811278
}

python-embed-runtime/src/test/java/io/github/howtis/pythonembed/PythonProcessManagerTest.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,15 @@ void cleanupTempDir() throws Exception {
8585
@Test
8686
void resolvePythonExecutable_venvLayout() throws Exception {
8787
tempDir = Files.createTempDirectory("test-venv-");
88-
Path scriptsDir = Files.createDirectories(tempDir.resolve("Scripts"));
89-
Path pythonExe = scriptsDir.resolve("python.exe");
88+
boolean isWindows = System.getProperty("os.name", "").toLowerCase().contains("win");
89+
Path pythonExe;
90+
if (isWindows) {
91+
Path scriptsDir = Files.createDirectories(tempDir.resolve("Scripts"));
92+
pythonExe = scriptsDir.resolve("python.exe");
93+
} else {
94+
Path binDir = Files.createDirectories(tempDir.resolve("bin"));
95+
pythonExe = binDir.resolve("python3");
96+
}
9097
Files.createFile(pythonExe);
9198

9299
String resolved = PythonProcessManager.resolvePythonExecutable(tempDir);
@@ -96,7 +103,14 @@ void resolvePythonExecutable_venvLayout() throws Exception {
96103
@Test
97104
void resolvePythonExecutable_standaloneLayout() throws Exception {
98105
tempDir = Files.createTempDirectory("test-standalone-");
99-
Path pythonExe = tempDir.resolve("python.exe");
106+
boolean isWindows = System.getProperty("os.name", "").toLowerCase().contains("win");
107+
Path pythonExe;
108+
if (isWindows) {
109+
pythonExe = tempDir.resolve("python.exe");
110+
} else {
111+
Path binDir = Files.createDirectories(tempDir.resolve("bin"));
112+
pythonExe = binDir.resolve("python3");
113+
}
100114
Files.createFile(pythonExe);
101115

102116
String resolved = PythonProcessManager.resolvePythonExecutable(tempDir);
@@ -106,24 +120,41 @@ void resolvePythonExecutable_standaloneLayout() throws Exception {
106120
@Test
107121
void resolvePythonExecutable_venvLayoutPreferredOverStandalone() throws Exception {
108122
tempDir = Files.createTempDirectory("test-both-");
109-
Path scriptsDir = Files.createDirectories(tempDir.resolve("Scripts"));
110-
Path venvPy = scriptsDir.resolve("python.exe");
111-
Files.createFile(venvPy);
112-
Path standalonePy = tempDir.resolve("python.exe");
113-
Files.createFile(standalonePy);
123+
boolean isWindows = System.getProperty("os.name", "").toLowerCase().contains("win");
124+
Path preferredPy;
125+
if (isWindows) {
126+
Path scriptsDir = Files.createDirectories(tempDir.resolve("Scripts"));
127+
preferredPy = scriptsDir.resolve("python.exe");
128+
Files.createFile(preferredPy);
129+
Path standalonePy = tempDir.resolve("python.exe");
130+
Files.createFile(standalonePy);
131+
} else {
132+
Path binDir = Files.createDirectories(tempDir.resolve("bin"));
133+
preferredPy = binDir.resolve("python3");
134+
Files.createFile(preferredPy);
135+
Path fallbackPy = binDir.resolve("python");
136+
Files.createFile(fallbackPy);
137+
}
114138

115139
String resolved = PythonProcessManager.resolvePythonExecutable(tempDir);
116-
assertEquals(venvPy.toAbsolutePath().toString(), resolved,
117-
"venv layout (Scripts/python.exe) should be preferred over standalone (root python.exe)");
140+
assertEquals(preferredPy.toAbsolutePath().toString(), resolved,
141+
"venv layout should be preferred over standalone");
118142
}
119143

120144
@Test
121145
void resolvePythonExecutable_fallbackWhenNeitherExists() {
122146
tempDir = Path.of("nonexistent-" + System.nanoTime());
123147
String resolved = PythonProcessManager.resolvePythonExecutable(tempDir);
124-
assertTrue(resolved.endsWith("Scripts/py" + "thon.exe")
125-
|| resolved.endsWith("Scripts\\py" + "thon.exe"),
126-
"should fall back to Scripts/python.exe path: " + resolved);
148+
boolean isWindows = System.getProperty("os.name", "").toLowerCase().contains("win");
149+
if (isWindows) {
150+
assertTrue(resolved.endsWith("Scripts/py" + "thon.exe")
151+
|| resolved.endsWith("Scripts\\py" + "thon.exe"),
152+
"should fall back to Scripts/python.exe path: " + resolved);
153+
} else {
154+
assertTrue(resolved.endsWith("bin/python3")
155+
|| resolved.endsWith("bin\\python3"),
156+
"should fall back to bin/python3 path: " + resolved);
157+
}
127158
}
128159

129160
@Test

0 commit comments

Comments
 (0)