| 34 | |
| 35 | //@SuppressWarnings("unused") |
| 36 | public class LastErrorTest extends TestCase { |
| 37 | |
| 38 | private static final Map<String, ?> OPTIONS = |
| 39 | Collections.singletonMap(Library.OPTION_FUNCTION_MAPPER, new FunctionMapper() { |
| 40 | @Override |
| 41 | public String getFunctionName(NativeLibrary library, Method m) { |
| 42 | if (m.getName().equals("noThrowLastError") |
| 43 | || m.getName().equals("throwLastError")) { |
| 44 | return "setLastError"; |
| 45 | } |
| 46 | return m.getName(); |
| 47 | } |
| 48 | }); |
| 49 | |
| 50 | public interface TestLibrary extends Library { |
| 51 | void setLastError(int code); |
| 52 | void noThrowLastError(int code); |
| 53 | void throwLastError(int code) throws LastErrorException; |
| 54 | } |
| 55 | |
| 56 | public static class DirectTestLibrary implements TestLibrary { |
| 57 | @Override |
| 58 | public native void setLastError(int code); |
| 59 | @Override |
| 60 | public native void noThrowLastError(int code); |
| 61 | @Override |
| 62 | public native void throwLastError(int code) throws LastErrorException; |
| 63 | static { |
| 64 | Native.register(NativeLibrary.getInstance("testlib", OPTIONS)); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public void testLastErrorPerThreadStorage() throws Exception { |
| 69 | final TestLibrary lib = Native.load("testlib", TestLibrary.class); |
| 70 | final int NTHREADS = 100; |
| 71 | final int[] errors = new int[NTHREADS]; |
| 72 | List<Thread> threads = new ArrayList<>(NTHREADS); |
| 73 | for (int i=0;i < NTHREADS;i++) { |
| 74 | final int idx = i; |
| 75 | Thread t = new Thread("tLastErrorSetter-" + i) { |
| 76 | @Override |
| 77 | public void run() { |
| 78 | lib.setLastError(-idx-1); |
| 79 | errors[idx] = Native.getLastError(); |
| 80 | } |
| 81 | }; |
| 82 | t.setDaemon(true); // so we can stop the main thread if necessary |
| 83 | threads.add(t); |
| 84 | } |
| 85 | int EXPECTED = 42; |
| 86 | lib.setLastError(EXPECTED); |
| 87 | assertEquals("Wrong error on main thread (immediate)", EXPECTED, Native.getLastError()); |
| 88 | for (Thread t : threads) { |
| 89 | t.start(); |
| 90 | } |
| 91 | for (Thread t : threads) { |
| 92 | t.join(TimeUnit.SECONDS.toMillis(7L)); |
| 93 | assertFalse("Thread " + t.getName() + " still alive", t.isAlive()); |
nothing calls this directly
no test coverage detected
searching dependent graphs…