Skip to content

Commit 5e01693

Browse files
author
Rob Austin
committed
added JnaTestCases
1 parent 98bd330 commit 5e01693

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
*
3+
* * Copyright (C) 2016 higherfrequencytrading.com
4+
* *
5+
* * This program is free software: you can redistribute it and/or modify
6+
* * it under the terms of the GNU Lesser General Public License as published by
7+
* * the Free Software Foundation, either version 3 of the License.
8+
* *
9+
* * This program is distributed in the hope that it will be useful,
10+
* * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* * GNU Lesser General Public License for more details.
13+
* *
14+
* * You should have received a copy of the GNU Lesser General Public License
15+
* * along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
19+
package software.chronicle.enterprise.internals;
20+
21+
import net.openhft.affinity.IAffinity;
22+
import net.openhft.affinity.impl.LinuxJNAAffinity;
23+
import net.openhft.affinity.impl.Utilities;
24+
import org.junit.After;
25+
import org.junit.Assume;
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
29+
import java.util.BitSet;
30+
31+
import static org.junit.Assert.assertEquals;
32+
import static org.junit.Assert.assertTrue;
33+
34+
/**
35+
* @author peter.lawrey
36+
*/
37+
public class JnaAffinityTest {
38+
protected static final int CORES = Runtime.getRuntime().availableProcessors();
39+
protected static final BitSet CORES_MASK = new BitSet(CORES);
40+
41+
static {
42+
CORES_MASK.set(0, CORES, true);
43+
}
44+
45+
@BeforeClass
46+
public static void checkJniLibraryPresent() {
47+
Assume.assumeTrue(LinuxJNAAffinity.LOADED);
48+
}
49+
50+
@Test
51+
public void getAffinityCompletesGracefully() {
52+
System.out.println("affinity: " + Utilities.toBinaryString(getImpl().getAffinity()));
53+
}
54+
55+
@Test
56+
public void getAffinityReturnsValidValue() {
57+
final BitSet affinity = getImpl().getAffinity();
58+
assertTrue(
59+
"Affinity mask " + Utilities.toBinaryString(affinity) + " must be non-empty",
60+
!affinity.isEmpty()
61+
);
62+
final int allCoresMask = (1 << CORES) - 1;
63+
assertTrue(
64+
"Affinity mask " + Utilities.toBinaryString(affinity) + " must be <=(2^" + CORES + "-1 = " + allCoresMask + ")",
65+
affinity.length() <= CORES_MASK.length()
66+
);
67+
}
68+
69+
@Test
70+
public void setAffinityCompletesGracefully() {
71+
BitSet affinity = new BitSet(1);
72+
affinity.set(0, true);
73+
getImpl().setAffinity(affinity);
74+
}
75+
76+
@Test
77+
public void getAffinityReturnsValuePreviouslySet() {
78+
String osName = System.getProperty("os.name");
79+
if (!osName.startsWith("Linux")) {
80+
System.out.println("Skipping Linux tests");
81+
return;
82+
}
83+
final IAffinity impl = LinuxJNAAffinity.INSTANCE;
84+
final int cores = CORES;
85+
for (int core = 0; core < cores; core++) {
86+
final BitSet mask = new BitSet();
87+
mask.set(core, true);
88+
getAffinityReturnsValuePreviouslySet(impl, mask);
89+
}
90+
}
91+
92+
93+
@Test
94+
public void showOtherIds() {
95+
System.out.println("processId: " + LinuxJNAAffinity.INSTANCE.getProcessId());
96+
System.out.println("threadId: " + LinuxJNAAffinity.INSTANCE.getThreadId());
97+
System.out.println("cpu: " + LinuxJNAAffinity.INSTANCE.getCpu());
98+
}
99+
100+
private void getAffinityReturnsValuePreviouslySet(final IAffinity impl,
101+
final BitSet mask) {
102+
103+
impl.setAffinity(mask);
104+
final BitSet _mask = impl.getAffinity();
105+
assertEquals(mask, _mask);
106+
}
107+
108+
@After
109+
public void tearDown() {
110+
getImpl().setAffinity(CORES_MASK);
111+
}
112+
113+
public IAffinity getImpl() {
114+
return LinuxJNAAffinity.INSTANCE;
115+
}
116+
}

0 commit comments

Comments
 (0)