12 |
|
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.HashSet; |
15 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
16 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
17 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
18 |
|
|
1264 |
|
public void testInterruptedFailingAcquire() throws Throwable { |
1265 |
|
class PleaseThrow extends RuntimeException {} |
1266 |
|
final PleaseThrow ex = new PleaseThrow(); |
1267 |
+ |
final AtomicBoolean thrown = new AtomicBoolean(); |
1268 |
|
|
1269 |
|
// A synchronizer only offering a choice of failure modes |
1270 |
|
class Sync extends AbstractQueuedLongSynchronizer { |
1271 |
|
volatile boolean pleaseThrow; |
1272 |
+ |
void maybeThrow() { |
1273 |
+ |
if (pleaseThrow) { |
1274 |
+ |
// assert: tryAcquire methods can throw at most once |
1275 |
+ |
if (! thrown.compareAndSet(false, true)) |
1276 |
+ |
throw new AssertionError(); |
1277 |
+ |
throw ex; |
1278 |
+ |
} |
1279 |
+ |
} |
1280 |
+ |
|
1281 |
|
@Override protected boolean tryAcquire(long ignored) { |
1282 |
< |
if (pleaseThrow) throw ex; |
1282 |
> |
maybeThrow(); |
1283 |
|
return false; |
1284 |
|
} |
1285 |
|
@Override protected long tryAcquireShared(long ignored) { |
1286 |
< |
if (pleaseThrow) throw ex; |
1286 |
> |
maybeThrow(); |
1287 |
|
return -1; |
1288 |
|
} |
1289 |
|
@Override protected boolean tryRelease(long ignored) { |
1367 |
|
} |
1368 |
|
awaitTermination(thread); |
1369 |
|
|
1370 |
+ |
if (! acquireInterruptibly) |
1371 |
+ |
assertTrue(thrown.get()); |
1372 |
+ |
|
1373 |
|
assertNull(s.getFirstQueuedThread()); |
1374 |
|
assertFalse(s.hasQueuedPredecessors()); |
1375 |
|
assertFalse(s.hasQueuedThreads()); |