12 |
|
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.HashSet; |
15 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
16 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; |
17 |
|
import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; |
18 |
|
|
18 |
– |
import junit.framework.AssertionFailedError; |
19 |
|
import junit.framework.Test; |
20 |
|
import junit.framework.TestSuite; |
21 |
|
|
140 |
|
long startTime = System.nanoTime(); |
141 |
|
while (!sync.isQueued(t)) { |
142 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
143 |
< |
throw new AssertionFailedError("timed out"); |
143 |
> |
throw new AssertionError("timed out"); |
144 |
|
Thread.yield(); |
145 |
|
} |
146 |
|
assertTrue(t.isAlive()); |
224 |
|
assertTrue(c.await(timeoutMillis, MILLISECONDS)); |
225 |
|
break; |
226 |
|
case awaitNanos: |
227 |
< |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
228 |
< |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
227 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
228 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
229 |
|
assertTrue(nanosRemaining > 0); |
230 |
|
break; |
231 |
|
case awaitUntil: |
252 |
|
break; |
253 |
|
case awaitNanos: |
254 |
|
startTime = System.nanoTime(); |
255 |
< |
long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis); |
256 |
< |
long nanosRemaining = c.awaitNanos(nanosTimeout); |
255 |
> |
long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis); |
256 |
> |
long nanosRemaining = c.awaitNanos(timeoutNanos); |
257 |
|
assertTrue(nanosRemaining <= 0); |
258 |
|
assertTrue(nanosRemaining > -MILLISECONDS.toNanos(LONG_DELAY_MS)); |
259 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
1256 |
|
sync.release(); |
1257 |
|
} |
1258 |
|
|
1259 |
+ |
/** |
1260 |
+ |
* Tests scenario for |
1261 |
+ |
* JDK-8191937: Lost interrupt in AbstractQueuedSynchronizer when tryAcquire methods throw |
1262 |
+ |
*/ |
1263 |
+ |
public void testInterruptedFailingAcquire() throws InterruptedException { |
1264 |
+ |
final RuntimeException ex = new RuntimeException(); |
1265 |
+ |
|
1266 |
+ |
// A synchronizer only offering a choice of failure modes |
1267 |
+ |
class Sync extends AbstractQueuedLongSynchronizer { |
1268 |
+ |
boolean pleaseThrow; |
1269 |
+ |
@Override protected boolean tryAcquire(long ignored) { |
1270 |
+ |
if (pleaseThrow) throw ex; |
1271 |
+ |
return false; |
1272 |
+ |
} |
1273 |
+ |
@Override protected long tryAcquireShared(long ignored) { |
1274 |
+ |
if (pleaseThrow) throw ex; |
1275 |
+ |
return -1; |
1276 |
+ |
} |
1277 |
+ |
@Override protected boolean tryRelease(long ignored) { |
1278 |
+ |
return true; |
1279 |
+ |
} |
1280 |
+ |
@Override protected boolean tryReleaseShared(long ignored) { |
1281 |
+ |
return true; |
1282 |
+ |
} |
1283 |
+ |
} |
1284 |
+ |
|
1285 |
+ |
final Sync s = new Sync(); |
1286 |
+ |
|
1287 |
+ |
final Thread thread = newStartedThread(new CheckedRunnable() { |
1288 |
+ |
public void realRun() { |
1289 |
+ |
try { |
1290 |
+ |
if (ThreadLocalRandom.current().nextBoolean()) |
1291 |
+ |
s.acquire(1); |
1292 |
+ |
else |
1293 |
+ |
s.acquireShared(1); |
1294 |
+ |
shouldThrow(); |
1295 |
+ |
} catch (Throwable t) { |
1296 |
+ |
assertSame(ex, t); |
1297 |
+ |
assertTrue(Thread.interrupted()); |
1298 |
+ |
} |
1299 |
+ |
}}); |
1300 |
+ |
waitForThreadToEnterWaitState(thread); |
1301 |
+ |
assertSame(thread, s.getFirstQueuedThread()); |
1302 |
+ |
assertTrue(s.hasQueuedPredecessors()); |
1303 |
+ |
assertTrue(s.hasQueuedThreads()); |
1304 |
+ |
assertEquals(1, s.getQueueLength()); |
1305 |
+ |
|
1306 |
+ |
s.pleaseThrow = true; |
1307 |
+ |
thread.interrupt(); |
1308 |
+ |
s.release(1); |
1309 |
+ |
awaitTermination(thread); |
1310 |
+ |
} |
1311 |
+ |
|
1312 |
|
} |