Skip to content

Commit

Permalink
fix getLastErrorText
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHegarty committed Feb 4, 2025
1 parent 7ca879b commit e22d482
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public static void checkError(int value, String caller) {
static String getLastErrorText() {
try {
MemorySegment seg = (MemorySegment) getLastErrorTextMethodHandle.invokeExact();
if (seg.equals(MemorySegment.NULL)) {
return "no last error text";
}
return seg.reinterpret(MAX_ERROR_TEXT).getString(0L);
} catch (Throwable t) {
throw new RuntimeException(t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected void compareResults(SearchResults results, List<List<Integer>> expecte
}
}

static boolean isLinuxAmd64() {
protected static boolean isLinuxAmd64() {
String name = System.getProperty("os.name");
return (name.startsWith("Linux")) && System.getProperty("os.arch").equals("amd64");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.nvidia.cuvs.internal.common;

import com.nvidia.cuvs.CuVSTestCase;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

import static com.carrotsearch.randomizedtesting.RandomizedTest.assumeTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

public class UtilIT extends CuVSTestCase {

private static final Logger log = LoggerFactory.getLogger(UtilIT.class);

@Before
public void setup() {
assumeTrue("not supported on " + System.getProperty("os.name"), isLinuxAmd64());
}

@Test
public void testGetLastErrorText() throws Throwable {
var cls = Class.forName("com.nvidia.cuvs.internal.common.Util");
var lookup = MethodHandles.lookup();
var mt = MethodType.methodType(String.class);
var mh = lookup.findStatic(cls, "getLastErrorText", mt);

// first, ensures that accessing the error text when there is none does not crash!
String errorText = (String) mh.invoke();
// second, ensures that the default test is returned
assertThat(errorText, equalTo("no last error text"));
}
}

0 comments on commit e22d482

Please sign in to comment.