< prev index next >

test/jdk/java/util/concurrent/tck/BlockingQueueTest.java

Print this page
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
Reviewed-by: martin, alanb


 236     /**
 237      * timed poll before a delayed offer times out; after offer succeeds;
 238      * on interruption throws
 239      */
 240     public void testTimedPollWithOffer() throws InterruptedException {
 241         final BlockingQueue q = emptyCollection();
 242         final CheckedBarrier barrier = new CheckedBarrier(2);
 243         final Object zero = makeElement(0);
 244         Thread t = newStartedThread(new CheckedRunnable() {
 245             public void realRun() throws InterruptedException {
 246                 long startTime = System.nanoTime();
 247                 assertNull(q.poll(timeoutMillis(), MILLISECONDS));
 248                 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
 249 
 250                 barrier.await();
 251 
 252                 assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
 253 
 254                 Thread.currentThread().interrupt();
 255                 try {
 256                     q.poll(LONG_DELAY_MS, MILLISECONDS);
 257                     shouldThrow();
 258                 } catch (InterruptedException success) {}
 259                 assertFalse(Thread.interrupted());
 260 
 261                 barrier.await();
 262                 try {
 263                     q.poll(LONG_DELAY_MS, MILLISECONDS);
 264                     shouldThrow();
 265                 } catch (InterruptedException success) {}
 266                 assertFalse(Thread.interrupted());
 267 
 268                 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
 269             }});
 270 
 271         barrier.await();
 272         long startTime = System.nanoTime();
 273         assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
 274         assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
 275 
 276         barrier.await();
 277         assertThreadBlocks(t, Thread.State.TIMED_WAITING);
 278         t.interrupt();
 279         awaitTermination(t);
 280     }
 281 
 282     /**
 283      * take() blocks interruptibly when empty
 284      */
 285     public void testTakeFromEmptyBlocksInterruptibly() {
 286         final BlockingQueue q = emptyCollection();
 287         final CountDownLatch threadStarted = new CountDownLatch(1);
 288         Thread t = newStartedThread(new CheckedRunnable() {
 289             public void realRun() {
 290                 threadStarted.countDown();
 291                 try {
 292                     q.take();
 293                     shouldThrow();
 294                 } catch (InterruptedException success) {}
 295                 assertFalse(Thread.interrupted());
 296             }});
 297 
 298         await(threadStarted);
 299         assertThreadBlocks(t, Thread.State.WAITING);
 300         t.interrupt();
 301         awaitTermination(t);
 302     }
 303 
 304     /**
 305      * take() throws InterruptedException immediately if interrupted
 306      * before waiting
 307      */
 308     public void testTakeFromEmptyAfterInterrupt() {
 309         final BlockingQueue q = emptyCollection();
 310         Thread t = newStartedThread(new CheckedRunnable() {
 311             public void realRun() {
 312                 Thread.currentThread().interrupt();
 313                 try {
 314                     q.take();
 315                     shouldThrow();
 316                 } catch (InterruptedException success) {}
 317                 assertFalse(Thread.interrupted());
 318             }});
 319 
 320         awaitTermination(t);
 321     }
 322 
 323     /**
 324      * timed poll() blocks interruptibly when empty
 325      */
 326     public void testTimedPollFromEmptyBlocksInterruptibly() {
 327         final BlockingQueue q = emptyCollection();
 328         final CountDownLatch threadStarted = new CountDownLatch(1);
 329         Thread t = newStartedThread(new CheckedRunnable() {
 330             public void realRun() {
 331                 threadStarted.countDown();
 332                 try {
 333                     q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
 334                     shouldThrow();
 335                 } catch (InterruptedException success) {}
 336                 assertFalse(Thread.interrupted());
 337             }});
 338 
 339         await(threadStarted);
 340         assertThreadBlocks(t, Thread.State.TIMED_WAITING);
 341         t.interrupt();
 342         awaitTermination(t);
 343     }
 344 
 345     /**
 346      * timed poll() throws InterruptedException immediately if
 347      * interrupted before waiting
 348      */
 349     public void testTimedPollFromEmptyAfterInterrupt() {
 350         final BlockingQueue q = emptyCollection();
 351         Thread t = newStartedThread(new CheckedRunnable() {
 352             public void realRun() {
 353                 Thread.currentThread().interrupt();
 354                 try {
 355                     q.poll(2 * LONG_DELAY_MS, MILLISECONDS);
 356                     shouldThrow();
 357                 } catch (InterruptedException success) {}
 358                 assertFalse(Thread.interrupted());
 359             }});
 360 
 361         awaitTermination(t);
 362     }
 363 
 364     /**
 365      * remove(x) removes x and returns true if present
 366      * TODO: move to superclass CollectionTest.java
 367      */
 368     public void testRemoveElement() {
 369         final BlockingQueue q = emptyCollection();
 370         final int size = Math.min(q.remainingCapacity(), SIZE);
 371         final Object[] elts = new Object[size];
 372         assertFalse(q.contains(makeElement(99)));
 373         assertFalse(q.remove(makeElement(99)));
 374         checkEmpty(q);
 375         for (int i = 0; i < size; i++)




 236     /**
 237      * timed poll before a delayed offer times out; after offer succeeds;
 238      * on interruption throws
 239      */
 240     public void testTimedPollWithOffer() throws InterruptedException {
 241         final BlockingQueue q = emptyCollection();
 242         final CheckedBarrier barrier = new CheckedBarrier(2);
 243         final Object zero = makeElement(0);
 244         Thread t = newStartedThread(new CheckedRunnable() {
 245             public void realRun() throws InterruptedException {
 246                 long startTime = System.nanoTime();
 247                 assertNull(q.poll(timeoutMillis(), MILLISECONDS));
 248                 assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
 249 
 250                 barrier.await();
 251 
 252                 assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
 253 
 254                 Thread.currentThread().interrupt();
 255                 try {
 256                     q.poll(randomTimeout(), randomTimeUnit());
 257                     shouldThrow();
 258                 } catch (InterruptedException success) {}
 259                 assertFalse(Thread.interrupted());
 260 
 261                 barrier.await();
 262                 try {
 263                     q.poll(LONG_DELAY_MS, MILLISECONDS);
 264                     shouldThrow();
 265                 } catch (InterruptedException success) {}
 266                 assertFalse(Thread.interrupted());
 267 
 268                 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
 269             }});
 270 
 271         barrier.await();
 272         long startTime = System.nanoTime();
 273         assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
 274         assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
 275 
 276         barrier.await();
 277         if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
 278         t.interrupt();
 279         awaitTermination(t);
 280     }
 281 
 282     /**
 283      * take() blocks interruptibly when empty
 284      */
 285     public void testTakeFromEmptyBlocksInterruptibly() {
 286         final BlockingQueue q = emptyCollection();
 287         final CountDownLatch threadStarted = new CountDownLatch(1);
 288         Thread t = newStartedThread(new CheckedRunnable() {
 289             public void realRun() {
 290                 threadStarted.countDown();
 291                 try {
 292                     q.take();
 293                     shouldThrow();
 294                 } catch (InterruptedException success) {}
 295                 assertFalse(Thread.interrupted());
 296             }});
 297 
 298         await(threadStarted);
 299         if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
 300         t.interrupt();
 301         awaitTermination(t);
 302     }
 303 
 304     /**
 305      * take() throws InterruptedException immediately if interrupted
 306      * before waiting
 307      */
 308     public void testTakeFromEmptyAfterInterrupt() {
 309         final BlockingQueue q = emptyCollection();
 310         Thread t = newStartedThread(new CheckedRunnable() {
 311             public void realRun() {
 312                 Thread.currentThread().interrupt();
 313                 try {
 314                     q.take();
 315                     shouldThrow();
 316                 } catch (InterruptedException success) {}
 317                 assertFalse(Thread.interrupted());
 318             }});
 319 
 320         awaitTermination(t);
 321     }
 322 
 323     /**
 324      * timed poll() blocks interruptibly when empty
 325      */
 326     public void testTimedPollFromEmptyBlocksInterruptibly() {
 327         final BlockingQueue q = emptyCollection();
 328         final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
 329         Thread t = newStartedThread(new CheckedRunnable() {
 330             public void realRun() {
 331                 pleaseInterrupt.countDown();
 332                 try {
 333                     q.poll(LONGER_DELAY_MS, MILLISECONDS);
 334                     shouldThrow();
 335                 } catch (InterruptedException success) {}
 336                 assertFalse(Thread.interrupted());
 337             }});
 338 
 339         await(pleaseInterrupt);
 340         if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
 341         t.interrupt();
 342         awaitTermination(t);
 343     }
 344 
 345     /**
 346      * timed poll() throws InterruptedException immediately if
 347      * interrupted before waiting
 348      */
 349     public void testTimedPollFromEmptyAfterInterrupt() {
 350         final BlockingQueue q = emptyCollection();
 351         Thread t = newStartedThread(new CheckedRunnable() {
 352             public void realRun() {
 353                 Thread.currentThread().interrupt();
 354                 try {
 355                     q.poll(LONGER_DELAY_MS, MILLISECONDS);
 356                     shouldThrow();
 357                 } catch (InterruptedException success) {}
 358                 assertFalse(Thread.interrupted());
 359             }});
 360 
 361         awaitTermination(t);
 362     }
 363 
 364     /**
 365      * remove(x) removes x and returns true if present
 366      * TODO: move to superclass CollectionTest.java
 367      */
 368     public void testRemoveElement() {
 369         final BlockingQueue q = emptyCollection();
 370         final int size = Math.min(q.remainingCapacity(), SIZE);
 371         final Object[] elts = new Object[size];
 372         assertFalse(q.contains(makeElement(99)));
 373         assertFalse(q.remove(makeElement(99)));
 374         checkEmpty(q);
 375         for (int i = 0; i < size; i++)


< prev index next >