Skip to content

Commit 526cfff

Browse files
committed
Add tests for AffinityLock behavior and exception handling
1 parent 04268d4 commit 526cfff

1 file changed

Lines changed: 44 additions & 5 deletions

File tree

affinity/src/test/java/net/openhft/affinity/AffinityLockTest.java

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.nio.file.Files;
3131
import java.nio.file.Paths;
3232
import java.util.ArrayList;
33+
import java.util.BitSet;
3334
import java.util.List;
3435

3536
import static net.openhft.affinity.AffinityLock.PROCESSORS;
@@ -300,16 +301,54 @@ public void testAffinityLockDescriptions() {
300301
}
301302

302303
@Test
304+
public void acquireLockWithoutBindingDoesNotChangeAffinity() {
305+
BitSet before = (BitSet) Affinity.getAffinity().clone();
306+
try (AffinityLock lock = AffinityLock.acquireLock(false)) {
307+
assertFalse(lock.isBound());
308+
assertEquals(before, Affinity.getAffinity());
309+
}
310+
assertEquals(before, Affinity.getAffinity());
311+
}
312+
313+
@Test(expected = IllegalArgumentException.class)
303314
public void testTooHighCpuId() {
304-
try (AffinityLock ignored = AffinityLock.acquireLock(123456)) {
305-
assertNotNull(ignored);
315+
AffinityLock.acquireLock(123456);
306316
}
317+
318+
@Test(expected = IllegalArgumentException.class)
319+
public void testNegativeCpuId() {
320+
AffinityLock.acquireLock(-1);
307321
}
308322

309-
@Test
323+
@Test(expected = IllegalArgumentException.class)
310324
public void testTooHighCpuId2() {
311-
try (AffinityLock ignored = AffinityLock.acquireLock(new int[] {123456})) {
312-
assertNotNull(ignored);
325+
AffinityLock.acquireLock(new int[]{123456});
326+
}
327+
328+
@Test(expected = IllegalStateException.class)
329+
public void bindingTwoThreadsToSameCpuThrows() throws InterruptedException {
330+
assumeTrue(Runtime.getRuntime().availableProcessors() > 1);
331+
332+
final AffinityLock lock = AffinityLock.acquireLock(false);
333+
Thread t = new Thread(() -> {
334+
lock.bind();
335+
try {
336+
Thread.sleep(100);
337+
} catch (InterruptedException ignored) {
338+
// ignored
339+
}
340+
});
341+
t.start();
342+
343+
while (!lock.isBound()) {
344+
Thread.sleep(10);
345+
}
346+
347+
try {
348+
lock.bind();
349+
} finally {
350+
t.join();
351+
lock.release();
313352
}
314353
}
315354

0 commit comments

Comments
 (0)