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