Skip to content

Commit 0edb26e

Browse files
committed
added unit tests for affinity implementations
1 parent 6bd7004 commit 0edb26e

5 files changed

Lines changed: 58 additions & 56 deletions

File tree

src/main/java/vanilla/java/affinity/impl/IAffinityImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
* @since 29.12.11, 20:14
88
*/
99
public interface IAffinityImpl {
10+
/**
11+
* @return returns affinity mask for current thread
12+
*/
1013
public long getAffinity();
1114

15+
/**
16+
* @param affinity sets affinity mask of current thread to specified value
17+
*/
1218
public void setAffinity( final long affinity );
1319
}

src/main/java/vanilla/java/affinity/impl/JNAAffinity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public long getAffinity() {
6666
}
6767

6868
@Override
69-
public void setAffinity(long affinity) {
69+
public void setAffinity(final long affinity) {
7070
final CLibrary lib = CLibrary.INSTANCE;
7171
final int ret = lib.sched_setaffinity(0, Long.SIZE / 8, new LongByReference(affinity));
7272
if (ret < 0) {
Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,59 @@
11
package vanilla.java.affinity.impl;
22

3-
import org.apache.commons.logging.Log;
4-
import org.apache.commons.logging.LogFactory;
5-
import org.junit.Assert;
6-
import org.junit.Test;
3+
import org.junit.*;
4+
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertTrue;
77

8-
import static com.google.common.base.Preconditions.*;
98

109
/**
11-
* fixme: Class AbstractAffinityImplTest is for porn
12-
*
1310
* @author cheremin
1411
* @since 29.12.11, 20:25
1512
*/
16-
public class AbstractAffinityImplTest {
17-
private static final Log log = LogFactory.getLog( AbstractAffinityImplTest.class );
13+
public abstract class AbstractAffinityImplTest {
14+
15+
public abstract IAffinityImpl getImpl();
16+
1817

1918
@Test
2019
public void getAffinityCompletesGracefully() throws Exception {
21-
JNAAffinity.INSTANCE.getAffinity();
20+
getImpl().getAffinity();
21+
}
22+
23+
@Test
24+
public void getAffinityReturnsValidValue() throws Exception {
25+
final long affinity = getImpl().getAffinity();
26+
assertTrue(
27+
"Affinity mask " + affinity + " must be >0",
28+
affinity > 0
29+
);
30+
final int cores = Runtime.getRuntime().availableProcessors();
31+
assertTrue(
32+
"Affinity mask " + affinity + " must be <=(2^cores-1)",
33+
affinity <= ( 1 >> cores )
34+
);
2235
}
2336

2437
@Test
2538
public void setAffinityCompletesGracefully() throws Exception {
26-
JNAAffinity.INSTANCE.setAffinity( 1 );
39+
getImpl().setAffinity( 1 );
2740
}
2841

2942
@Test
3043
public void getAffinityReturnsValuePreviouslySet() throws Exception {
31-
JNAAffinity.INSTANCE.setAffinity( 1 );
32-
final long affinity = JNAAffinity.INSTANCE.getAffinity();
33-
Assert.assertEquals( 1, affinity );
44+
final IAffinityImpl impl = getImpl();
45+
final int cores = Runtime.getRuntime().availableProcessors();
46+
for ( int core = 0; core < cores; core++ ) {
47+
final long mask = ( 1 >> core );
48+
getAffinityReturnsValuePreviouslySet( impl, mask );
49+
}
50+
}
51+
52+
private void getAffinityReturnsValuePreviouslySet( final IAffinityImpl impl,
53+
final long mask ) throws Exception {
54+
55+
impl.setAffinity( mask );
56+
final long _mask = impl.getAffinity();
57+
assertEquals( mask, _mask );
3458
}
3559
}

src/test/java/vanilla/java/affinity/impl/JNAAffinityTest.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,15 @@
1818

1919
import org.junit.*;
2020

21-
/**
22-
* @author peter.lawrey
23-
*/
24-
@Ignore("No methods for now")
25-
public class JNAAffinityTest {
21+
/** @author peter.lawrey */
22+
public class JNAAffinityTest extends AbstractAffinityImplTest {
2623
@BeforeClass
2724
public static void checkJniLibraryPresent() {
28-
Assume.assumeTrue( JNAAffinity.LOADED);
25+
Assume.assumeTrue( JNAAffinity.LOADED );
2926
}
3027

31-
/*@Test
32-
public void testNanoTimePerf() {
33-
final int runs = 10 * 1000 * 1000;
34-
JNAAffinity.INSTANCE.nanoTime();
35-
long start = System.nanoTime();
36-
long start0 = JNAAffinity.INSTANCE.nanoTime();
37-
for (int i = 0; i < runs; i++)
38-
JNAAffinity.INSTANCE.nanoTime();
39-
long time = System.nanoTime() - start;
40-
final long time0 = JNAAffinity.INSTANCE.nanoTime() - start0;
41-
long time2 = NativeAffinity.tscToNano(time0);
42-
System.out.printf("Each call took %.1f ns and the ratio was %.5f%n", (double) time / runs, (double) time2 / time);
43-
}*/
28+
@Override
29+
public IAffinityImpl getImpl() {
30+
return JNAAffinity.INSTANCE;
31+
}
4432
}

src/test/java/vanilla/java/affinity/impl/NativeAffinityTest.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,16 @@
1818

1919
import org.junit.Assume;
2020
import org.junit.BeforeClass;
21-
import org.junit.Test;
2221

23-
import static org.junit.Assert.*;
24-
25-
/**
26-
* @author peter.lawrey
27-
*/
28-
public class NativeAffinityTest {
22+
/** @author peter.lawrey */
23+
public class NativeAffinityTest extends AbstractAffinityImplTest {
2924
@BeforeClass
3025
public static void checkJniLibraryPresent() {
31-
Assume.assumeTrue(NativeAffinity.LOADED);
26+
Assume.assumeTrue( NativeAffinity.LOADED );
3227
}
3328

34-
@Test
35-
public void testGetAffinity() throws Exception {
36-
long a = NativeAffinity.INSTANCE.getAffinity();
37-
assertFalse(a == 0);
38-
assertFalse(a == -1);
39-
}
40-
41-
@Test
42-
public void testSetAffinity() throws Exception {
43-
NativeAffinity.INSTANCE.setAffinity(0x1);
44-
assertEquals(0x1, NativeAffinity.INSTANCE.getAffinity());
45-
46-
NativeAffinity.INSTANCE.setAffinity(0x2);
47-
assertEquals(0x2, NativeAffinity.INSTANCE.getAffinity());
29+
@Override
30+
public IAffinityImpl getImpl() {
31+
return NativeAffinity.INSTANCE;
4832
}
4933
}

0 commit comments

Comments
 (0)