Skip to content

Commit bd075f5

Browse files
authored
Fix tests on Linux (#1)
* CI: Fix env var name * Improve exception when unable to read env/file * Don't append architecture for darwin natives * Use a `zstd.lib` property to load the shared library using JNA
1 parent cc50e37 commit bd075f5

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ name: Test discord-zstd-java
22
on:
33
pull_request:
44
push:
5-
branches:
6-
- master
75

86
env:
97
COMMON_CMAKE_FLAGS: "-DZSTD_BUILD_PROGRAMS=OFF -DZSTD_LEGACY_SUPPORT=OFF -DZSTD_BUILD_STATIC=OFF -DBUILD_SHARED_LIBS=ON -DZSTD_BUILD_DICTBUILDER=OFF -DZSTD_BUILD_COMPRESSION=OFF -DCMAKE_BUILD_TYPE=Debug"
@@ -31,7 +29,6 @@ jobs:
3129
cmake --build . -j 4
3230
cp lib/libzstd.so ../../../output/x64/libzstd.so
3331
cd ..
34-
3532
3633
- name: Publish Artifacts
3734
uses: actions/upload-artifact@v5
@@ -74,7 +71,7 @@ jobs:
7471
- name: Run tests
7572
env:
7673
TEST_DATA_ZSTD_KEY: ${{ secrets.TEST_DATA_ZSTD_KEY }}
77-
TEST_DATA_ZSTD_PARAMS: ${{ secrets.TEST_DATA_ZSTD_PARAMS }}
74+
TEST_DATA_ZSTD_PARAMETERS: ${{ secrets.TEST_DATA_ZSTD_PARAMETERS }}
7875
run: ./gradlew test
7976

8077
- name: Upload JUnit XML Test Report
@@ -92,7 +89,7 @@ jobs:
9289
path: '**/build/reports/tests/test'
9390

9491
- name: Annotate Failed Tests
95-
uses: mikepenz/action-junit-report@v6
92+
uses: mikepenz/action-junit-report@v6.0.0
9693
if: ${{ !cancelled() }}
9794
with:
9895
report_paths: '**/build/test-results/test/TEST-*.xml'

api/src/main/java/dev/freya02/discord/zstd/api/ZstdNativesLoader.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public static synchronized boolean load(Path path) {
1717
if (init)
1818
return false;
1919

20-
System.load(path.toAbsolutePath().toString());
20+
final String pathStr = path.toAbsolutePath().toString();
21+
System.setProperty("zstd.lib", pathStr);
22+
System.load(pathStr);
2123
init = true;
2224
return true;
2325
}
@@ -32,19 +34,19 @@ public static synchronized boolean loadFromJar() throws IOException {
3234
String platform;
3335
String extension;
3436
if (osName.startsWith("Linux")) {
35-
platform = "linux";
37+
platform = "linux-" + architecture;
3638
extension = "so";
3739
} else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) {
3840
platform = "darwin";
3941
extension = "dylib";
4042
} else if (osName.startsWith("Windows")) {
41-
platform = "win32";
43+
platform = "win32-" + architecture;
4244
extension = "dll";
4345
} else {
4446
throw new UnsupportedOperationException("Unsupported OS: " + osName);
4547
}
4648

47-
String resourcePath = String.format("/natives/%s-%s/libzstd.%s", platform, architecture, extension);
49+
String resourcePath = String.format("/natives/%s/libzstd.%s", platform, extension);
4850
Path nativePath = NativeUtil.copyNativeFromJar(resourcePath);
4951
load(nativePath);
5052
return true;

jna-impl/src/main/java/dev/freya02/discord/zstd/jna/ZstdJna.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package dev.freya02.discord.zstd.jna;
22

33
import com.sun.jna.Library;
4-
import com.sun.jna.Native;
54
import com.sun.jna.Pointer;
65
import com.sun.jna.Structure;
76

87
import java.nio.ByteBuffer;
98

109
public interface ZstdJna extends Library
1110
{
12-
ZstdJna INSTANCE = Native.load(null, ZstdJna.class);
11+
ZstdJna INSTANCE = ZstdJnaHelper.load();
1312

1413
Pointer ZSTD_createDStream();
1514

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package dev.freya02.discord.zstd.jna;
2+
3+
import com.sun.jna.Native;
4+
import com.sun.jna.Platform;
5+
6+
public class ZstdJnaHelper {
7+
public static ZstdJna load() {
8+
String lib = System.getProperty("zstd.lib");
9+
// On Windows and macOS, if lib is null, it will link functions from the current process,
10+
// on Linux, `System.load` does not allow JNA to find the functions, see https://github.com/java-native-access/jna/issues/1698
11+
if (lib == null && Platform.isLinux()) {
12+
throw new IllegalStateException("Cannot load Zstd from the current process on Linux systems, 'zstd.lib' must be set before ZstdJna is used");
13+
}
14+
15+
return Native.load(lib, ZstdJna.class);
16+
}
17+
}

test-data/src/main/java/dev/freya02/discord/zstd/TestChunks.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ private static String readEnvOrFile(String varName, Path path) throws IOExceptio
9696
final String envVar = System.getenv(varName);
9797
if (envVar != null) return envVar;
9898

99-
return new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
99+
try {
100+
return new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
101+
} catch (IOException e) {
102+
throw new IOException("Env var '" + varName + "' is not present", e);
103+
}
100104
}
101105

102106
private static boolean isCompressedChunk(Path path) {

0 commit comments

Comments
 (0)