|
30 | 30 | import java.nio.file.Files; |
31 | 31 | import java.nio.file.Paths; |
32 | 32 | import java.util.ArrayList; |
| 33 | +import java.util.BitSet; |
33 | 34 | import java.util.List; |
34 | 35 |
|
35 | 36 | import static net.openhft.affinity.AffinityLock.PROCESSORS; |
@@ -300,16 +301,54 @@ public void testAffinityLockDescriptions() { |
300 | 301 | } |
301 | 302 |
|
302 | 303 | @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) |
303 | 314 | public void testTooHighCpuId() { |
304 | | - try (AffinityLock ignored = AffinityLock.acquireLock(123456)) { |
305 | | - assertNotNull(ignored); |
| 315 | + AffinityLock.acquireLock(123456); |
306 | 316 | } |
| 317 | + |
| 318 | + @Test(expected = IllegalArgumentException.class) |
| 319 | + public void testNegativeCpuId() { |
| 320 | + AffinityLock.acquireLock(-1); |
307 | 321 | } |
308 | 322 |
|
309 | | - @Test |
| 323 | + @Test(expected = IllegalArgumentException.class) |
310 | 324 | 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(); |
313 | 352 | } |
314 | 353 | } |
315 | 354 |
|
|
0 commit comments