Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Written by Doug Lea with assistance from members of JCP JSR-166 |
| 3 | * Expert Group and released to the public domain, as explained at |
| 4 | * http://creativecommons.org/publicdomain/zero/1.0/ |
| 5 | * Other contributors include Andrew Wright, Jeffrey Hayes, |
| 6 | * Pat Fisher, Mike Judd. |
| 7 | */ |
| 8 | |
| 9 | package jsr166; |
| 10 | |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 11 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 12 | |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 13 | import java.util.ArrayList; |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 14 | import java.util.Arrays; |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 15 | import java.util.Collection; |
| 16 | import java.util.Iterator; |
| 17 | import java.util.NoSuchElementException; |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 18 | import java.util.concurrent.BlockingQueue; |
| 19 | import java.util.concurrent.CountDownLatch; |
| 20 | import java.util.concurrent.Executors; |
| 21 | import java.util.concurrent.ExecutorService; |
| 22 | import java.util.concurrent.SynchronousQueue; |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 23 | |
| 24 | import junit.framework.Test; |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 25 | |
| 26 | public class SynchronousQueueTest extends JSR166TestCase { |
| 27 | |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 28 | // android-note: These tests have been moved into their own separate |
| 29 | // classes to work around CTS issues. |
| 30 | // |
| 31 | // public static class Fair extends BlockingQueueTest { |
| 32 | // protected BlockingQueue emptyCollection() { |
| 33 | // return new SynchronousQueue(true); |
| 34 | // } |
| 35 | // } |
| 36 | // |
| 37 | // public static class NonFair extends BlockingQueueTest { |
| 38 | // protected BlockingQueue emptyCollection() { |
| 39 | // return new SynchronousQueue(false); |
| 40 | // } |
| 41 | // } |
| 42 | // |
| 43 | // public static void main(String[] args) { |
| 44 | // main(suite(), args); |
| 45 | // } |
| 46 | // |
| 47 | // public static Test suite() { |
| 48 | // return newTestSuite(SynchronousQueueTest.class, |
| 49 | // new Fair().testSuite(), |
| 50 | // new NonFair().testSuite()); |
| 51 | // } |
| 52 | |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 53 | /** |
| 54 | * Any SynchronousQueue is both empty and full |
| 55 | */ |
| 56 | public void testEmptyFull() { testEmptyFull(false); } |
| 57 | public void testEmptyFull_fair() { testEmptyFull(true); } |
| 58 | public void testEmptyFull(boolean fair) { |
| 59 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 60 | assertTrue(q.isEmpty()); |
| 61 | assertEquals(0, q.size()); |
| 62 | assertEquals(0, q.remainingCapacity()); |
| 63 | assertFalse(q.offer(zero)); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * offer fails if no active taker |
| 68 | */ |
| 69 | public void testOffer() { testOffer(false); } |
| 70 | public void testOffer_fair() { testOffer(true); } |
| 71 | public void testOffer(boolean fair) { |
| 72 | SynchronousQueue q = new SynchronousQueue(fair); |
| 73 | assertFalse(q.offer(one)); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * add throws IllegalStateException if no active taker |
| 78 | */ |
| 79 | public void testAdd() { testAdd(false); } |
| 80 | public void testAdd_fair() { testAdd(true); } |
| 81 | public void testAdd(boolean fair) { |
| 82 | SynchronousQueue q = new SynchronousQueue(fair); |
| 83 | assertEquals(0, q.remainingCapacity()); |
| 84 | try { |
| 85 | q.add(one); |
| 86 | shouldThrow(); |
| 87 | } catch (IllegalStateException success) {} |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * addAll(this) throws IllegalArgumentException |
| 92 | */ |
| 93 | public void testAddAll_self() { testAddAll_self(false); } |
| 94 | public void testAddAll_self_fair() { testAddAll_self(true); } |
| 95 | public void testAddAll_self(boolean fair) { |
| 96 | SynchronousQueue q = new SynchronousQueue(fair); |
| 97 | try { |
| 98 | q.addAll(q); |
| 99 | shouldThrow(); |
| 100 | } catch (IllegalArgumentException success) {} |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * addAll throws ISE if no active taker |
| 105 | */ |
| 106 | public void testAddAll_ISE() { testAddAll_ISE(false); } |
| 107 | public void testAddAll_ISE_fair() { testAddAll_ISE(true); } |
| 108 | public void testAddAll_ISE(boolean fair) { |
| 109 | SynchronousQueue q = new SynchronousQueue(fair); |
| 110 | Integer[] ints = new Integer[1]; |
| 111 | for (int i = 0; i < ints.length; i++) |
| 112 | ints[i] = i; |
| 113 | Collection<Integer> coll = Arrays.asList(ints); |
| 114 | try { |
| 115 | q.addAll(coll); |
| 116 | shouldThrow(); |
| 117 | } catch (IllegalStateException success) {} |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * put blocks interruptibly if no active taker |
| 122 | */ |
| 123 | public void testBlockingPut() { testBlockingPut(false); } |
| 124 | public void testBlockingPut_fair() { testBlockingPut(true); } |
| 125 | public void testBlockingPut(boolean fair) { |
| 126 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 127 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 128 | Thread t = newStartedThread(new CheckedRunnable() { |
| 129 | public void realRun() throws InterruptedException { |
| 130 | Thread.currentThread().interrupt(); |
| 131 | try { |
| 132 | q.put(99); |
| 133 | shouldThrow(); |
| 134 | } catch (InterruptedException success) {} |
| 135 | assertFalse(Thread.interrupted()); |
| 136 | |
| 137 | pleaseInterrupt.countDown(); |
| 138 | try { |
| 139 | q.put(99); |
| 140 | shouldThrow(); |
| 141 | } catch (InterruptedException success) {} |
| 142 | assertFalse(Thread.interrupted()); |
| 143 | }}); |
| 144 | |
| 145 | await(pleaseInterrupt); |
| 146 | assertThreadStaysAlive(t); |
| 147 | t.interrupt(); |
| 148 | awaitTermination(t); |
| 149 | assertEquals(0, q.remainingCapacity()); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * put blocks interruptibly waiting for take |
| 154 | */ |
| 155 | public void testPutWithTake() { testPutWithTake(false); } |
| 156 | public void testPutWithTake_fair() { testPutWithTake(true); } |
| 157 | public void testPutWithTake(boolean fair) { |
| 158 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 159 | final CountDownLatch pleaseTake = new CountDownLatch(1); |
| 160 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 161 | Thread t = newStartedThread(new CheckedRunnable() { |
| 162 | public void realRun() throws InterruptedException { |
| 163 | pleaseTake.countDown(); |
| 164 | q.put(one); |
| 165 | |
| 166 | pleaseInterrupt.countDown(); |
| 167 | try { |
| 168 | q.put(99); |
| 169 | shouldThrow(); |
| 170 | } catch (InterruptedException success) {} |
| 171 | assertFalse(Thread.interrupted()); |
| 172 | }}); |
| 173 | |
| 174 | await(pleaseTake); |
| 175 | assertEquals(0, q.remainingCapacity()); |
| 176 | try { assertSame(one, q.take()); } |
| 177 | catch (InterruptedException e) { threadUnexpectedException(e); } |
| 178 | |
| 179 | await(pleaseInterrupt); |
| 180 | assertThreadStaysAlive(t); |
| 181 | t.interrupt(); |
| 182 | awaitTermination(t); |
| 183 | assertEquals(0, q.remainingCapacity()); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * timed offer times out if elements not taken |
| 188 | */ |
| 189 | public void testTimedOffer() { testTimedOffer(false); } |
| 190 | public void testTimedOffer_fair() { testTimedOffer(true); } |
| 191 | public void testTimedOffer(boolean fair) { |
| 192 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 193 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 194 | Thread t = newStartedThread(new CheckedRunnable() { |
| 195 | public void realRun() throws InterruptedException { |
| 196 | long startTime = System.nanoTime(); |
| 197 | assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
| 198 | assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
| 199 | pleaseInterrupt.countDown(); |
| 200 | try { |
| 201 | q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
| 202 | shouldThrow(); |
| 203 | } catch (InterruptedException success) {} |
| 204 | }}); |
| 205 | |
| 206 | await(pleaseInterrupt); |
| 207 | assertThreadStaysAlive(t); |
| 208 | t.interrupt(); |
| 209 | awaitTermination(t); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * poll return null if no active putter |
| 214 | */ |
| 215 | public void testPoll() { testPoll(false); } |
| 216 | public void testPoll_fair() { testPoll(true); } |
| 217 | public void testPoll(boolean fair) { |
| 218 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 219 | assertNull(q.poll()); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * timed poll with zero timeout times out if no active putter |
| 224 | */ |
| 225 | public void testTimedPoll0() { testTimedPoll0(false); } |
| 226 | public void testTimedPoll0_fair() { testTimedPoll0(true); } |
| 227 | public void testTimedPoll0(boolean fair) { |
| 228 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 229 | try { assertNull(q.poll(0, MILLISECONDS)); } |
| 230 | catch (InterruptedException e) { threadUnexpectedException(e); } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * timed poll with nonzero timeout times out if no active putter |
| 235 | */ |
| 236 | public void testTimedPoll() { testTimedPoll(false); } |
| 237 | public void testTimedPoll_fair() { testTimedPoll(true); } |
| 238 | public void testTimedPoll(boolean fair) { |
| 239 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 240 | long startTime = System.nanoTime(); |
| 241 | try { assertNull(q.poll(timeoutMillis(), MILLISECONDS)); } |
| 242 | catch (InterruptedException e) { threadUnexpectedException(e); } |
| 243 | assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * timed poll before a delayed offer times out, returning null; |
| 248 | * after offer succeeds; on interruption throws |
| 249 | */ |
| 250 | public void testTimedPollWithOffer() { testTimedPollWithOffer(false); } |
| 251 | public void testTimedPollWithOffer_fair() { testTimedPollWithOffer(true); } |
| 252 | public void testTimedPollWithOffer(boolean fair) { |
| 253 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 254 | final CountDownLatch pleaseOffer = new CountDownLatch(1); |
| 255 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 256 | Thread t = newStartedThread(new CheckedRunnable() { |
| 257 | public void realRun() throws InterruptedException { |
| 258 | long startTime = System.nanoTime(); |
| 259 | assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
| 260 | assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
| 261 | |
| 262 | pleaseOffer.countDown(); |
| 263 | startTime = System.nanoTime(); |
| 264 | assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
| 265 | assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
| 266 | |
| 267 | Thread.currentThread().interrupt(); |
| 268 | try { |
| 269 | q.poll(LONG_DELAY_MS, MILLISECONDS); |
| 270 | shouldThrow(); |
| 271 | } catch (InterruptedException success) {} |
| 272 | assertFalse(Thread.interrupted()); |
| 273 | |
| 274 | pleaseInterrupt.countDown(); |
| 275 | try { |
| 276 | q.poll(LONG_DELAY_MS, MILLISECONDS); |
| 277 | shouldThrow(); |
| 278 | } catch (InterruptedException success) {} |
| 279 | assertFalse(Thread.interrupted()); |
| 280 | }}); |
| 281 | |
| 282 | await(pleaseOffer); |
| 283 | long startTime = System.nanoTime(); |
| 284 | try { assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS)); } |
| 285 | catch (InterruptedException e) { threadUnexpectedException(e); } |
| 286 | assertTrue(millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
| 287 | |
| 288 | await(pleaseInterrupt); |
| 289 | assertThreadStaysAlive(t); |
| 290 | t.interrupt(); |
| 291 | awaitTermination(t); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * peek() returns null if no active putter |
| 296 | */ |
| 297 | public void testPeek() { testPeek(false); } |
| 298 | public void testPeek_fair() { testPeek(true); } |
| 299 | public void testPeek(boolean fair) { |
| 300 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 301 | assertNull(q.peek()); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * element() throws NoSuchElementException if no active putter |
| 306 | */ |
| 307 | public void testElement() { testElement(false); } |
| 308 | public void testElement_fair() { testElement(true); } |
| 309 | public void testElement(boolean fair) { |
| 310 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 311 | try { |
| 312 | q.element(); |
| 313 | shouldThrow(); |
| 314 | } catch (NoSuchElementException success) {} |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * remove() throws NoSuchElementException if no active putter |
| 319 | */ |
| 320 | public void testRemove() { testRemove(false); } |
| 321 | public void testRemove_fair() { testRemove(true); } |
| 322 | public void testRemove(boolean fair) { |
| 323 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 324 | try { |
| 325 | q.remove(); |
| 326 | shouldThrow(); |
| 327 | } catch (NoSuchElementException success) {} |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * contains returns false |
| 332 | */ |
| 333 | public void testContains() { testContains(false); } |
| 334 | public void testContains_fair() { testContains(true); } |
| 335 | public void testContains(boolean fair) { |
| 336 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 337 | assertFalse(q.contains(zero)); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * clear ensures isEmpty |
| 342 | */ |
| 343 | public void testClear() { testClear(false); } |
| 344 | public void testClear_fair() { testClear(true); } |
| 345 | public void testClear(boolean fair) { |
| 346 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 347 | q.clear(); |
| 348 | assertTrue(q.isEmpty()); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * containsAll returns false unless empty |
| 353 | */ |
| 354 | public void testContainsAll() { testContainsAll(false); } |
| 355 | public void testContainsAll_fair() { testContainsAll(true); } |
| 356 | public void testContainsAll(boolean fair) { |
| 357 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 358 | Integer[] empty = new Integer[0]; |
| 359 | assertTrue(q.containsAll(Arrays.asList(empty))); |
| 360 | Integer[] ints = new Integer[1]; ints[0] = zero; |
| 361 | assertFalse(q.containsAll(Arrays.asList(ints))); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * retainAll returns false |
| 366 | */ |
| 367 | public void testRetainAll() { testRetainAll(false); } |
| 368 | public void testRetainAll_fair() { testRetainAll(true); } |
| 369 | public void testRetainAll(boolean fair) { |
| 370 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 371 | Integer[] empty = new Integer[0]; |
| 372 | assertFalse(q.retainAll(Arrays.asList(empty))); |
| 373 | Integer[] ints = new Integer[1]; ints[0] = zero; |
| 374 | assertFalse(q.retainAll(Arrays.asList(ints))); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * removeAll returns false |
| 379 | */ |
| 380 | public void testRemoveAll() { testRemoveAll(false); } |
| 381 | public void testRemoveAll_fair() { testRemoveAll(true); } |
| 382 | public void testRemoveAll(boolean fair) { |
| 383 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 384 | Integer[] empty = new Integer[0]; |
| 385 | assertFalse(q.removeAll(Arrays.asList(empty))); |
| 386 | Integer[] ints = new Integer[1]; ints[0] = zero; |
| 387 | assertFalse(q.containsAll(Arrays.asList(ints))); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * toArray is empty |
| 392 | */ |
| 393 | public void testToArray() { testToArray(false); } |
| 394 | public void testToArray_fair() { testToArray(true); } |
| 395 | public void testToArray(boolean fair) { |
| 396 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 397 | Object[] o = q.toArray(); |
| 398 | assertEquals(0, o.length); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * toArray(Integer array) returns its argument with the first |
| 403 | * element (if present) nulled out |
| 404 | */ |
| 405 | public void testToArray2() { testToArray2(false); } |
| 406 | public void testToArray2_fair() { testToArray2(true); } |
| 407 | public void testToArray2(boolean fair) { |
| 408 | final SynchronousQueue<Integer> q |
| 409 | = new SynchronousQueue<Integer>(fair); |
| 410 | Integer[] a; |
| 411 | |
| 412 | a = new Integer[0]; |
| 413 | assertSame(a, q.toArray(a)); |
| 414 | |
| 415 | a = new Integer[3]; |
| 416 | Arrays.fill(a, 42); |
| 417 | assertSame(a, q.toArray(a)); |
| 418 | assertNull(a[0]); |
| 419 | for (int i = 1; i < a.length; i++) |
| 420 | assertEquals(42, (int) a[i]); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * toArray(null) throws NPE |
| 425 | */ |
| 426 | public void testToArray_null() { testToArray_null(false); } |
| 427 | public void testToArray_null_fair() { testToArray_null(true); } |
| 428 | public void testToArray_null(boolean fair) { |
| 429 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 430 | try { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 431 | Object[] o = q.toArray(null); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 432 | shouldThrow(); |
| 433 | } catch (NullPointerException success) {} |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * iterator does not traverse any elements |
| 438 | */ |
| 439 | public void testIterator() { testIterator(false); } |
| 440 | public void testIterator_fair() { testIterator(true); } |
| 441 | public void testIterator(boolean fair) { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 442 | assertIteratorExhausted(new SynchronousQueue(fair).iterator()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /** |
| 446 | * iterator remove throws ISE |
| 447 | */ |
| 448 | public void testIteratorRemove() { testIteratorRemove(false); } |
| 449 | public void testIteratorRemove_fair() { testIteratorRemove(true); } |
| 450 | public void testIteratorRemove(boolean fair) { |
| 451 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 452 | Iterator it = q.iterator(); |
| 453 | try { |
| 454 | it.remove(); |
| 455 | shouldThrow(); |
| 456 | } catch (IllegalStateException success) {} |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * toString returns a non-null string |
| 461 | */ |
| 462 | public void testToString() { testToString(false); } |
| 463 | public void testToString_fair() { testToString(true); } |
| 464 | public void testToString(boolean fair) { |
| 465 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 466 | String s = q.toString(); |
| 467 | assertNotNull(s); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * offer transfers elements across Executor tasks |
| 472 | */ |
| 473 | public void testOfferInExecutor() { testOfferInExecutor(false); } |
| 474 | public void testOfferInExecutor_fair() { testOfferInExecutor(true); } |
| 475 | public void testOfferInExecutor(boolean fair) { |
| 476 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 477 | ExecutorService executor = Executors.newFixedThreadPool(2); |
| 478 | final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
| 479 | |
| 480 | executor.execute(new CheckedRunnable() { |
| 481 | public void realRun() throws InterruptedException { |
| 482 | assertFalse(q.offer(one)); |
| 483 | threadsStarted.await(); |
| 484 | assertTrue(q.offer(one, LONG_DELAY_MS, MILLISECONDS)); |
| 485 | assertEquals(0, q.remainingCapacity()); |
| 486 | }}); |
| 487 | |
| 488 | executor.execute(new CheckedRunnable() { |
| 489 | public void realRun() throws InterruptedException { |
| 490 | threadsStarted.await(); |
| 491 | assertSame(one, q.take()); |
| 492 | }}); |
| 493 | |
| 494 | joinPool(executor); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * timed poll retrieves elements across Executor threads |
| 499 | */ |
| 500 | public void testPollInExecutor() { testPollInExecutor(false); } |
| 501 | public void testPollInExecutor_fair() { testPollInExecutor(true); } |
| 502 | public void testPollInExecutor(boolean fair) { |
| 503 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 504 | final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
| 505 | ExecutorService executor = Executors.newFixedThreadPool(2); |
| 506 | executor.execute(new CheckedRunnable() { |
| 507 | public void realRun() throws InterruptedException { |
| 508 | assertNull(q.poll()); |
| 509 | threadsStarted.await(); |
| 510 | assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
| 511 | assertTrue(q.isEmpty()); |
| 512 | }}); |
| 513 | |
| 514 | executor.execute(new CheckedRunnable() { |
| 515 | public void realRun() throws InterruptedException { |
| 516 | threadsStarted.await(); |
| 517 | q.put(one); |
| 518 | }}); |
| 519 | |
| 520 | joinPool(executor); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * a deserialized serialized queue is usable |
| 525 | */ |
| 526 | public void testSerialization() { |
| 527 | final SynchronousQueue x = new SynchronousQueue(); |
| 528 | final SynchronousQueue y = new SynchronousQueue(false); |
| 529 | final SynchronousQueue z = new SynchronousQueue(true); |
| 530 | assertSerialEquals(x, y); |
| 531 | assertNotSerialEquals(x, z); |
| 532 | SynchronousQueue[] qs = { x, y, z }; |
| 533 | for (SynchronousQueue q : qs) { |
| 534 | SynchronousQueue clone = serialClone(q); |
| 535 | assertNotSame(q, clone); |
| 536 | assertSerialEquals(q, clone); |
| 537 | assertTrue(clone.isEmpty()); |
| 538 | assertEquals(0, clone.size()); |
| 539 | assertEquals(0, clone.remainingCapacity()); |
| 540 | assertFalse(clone.offer(zero)); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * drainTo(c) of empty queue doesn't transfer elements |
| 546 | */ |
| 547 | public void testDrainTo() { testDrainTo(false); } |
| 548 | public void testDrainTo_fair() { testDrainTo(true); } |
| 549 | public void testDrainTo(boolean fair) { |
| 550 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 551 | ArrayList l = new ArrayList(); |
| 552 | q.drainTo(l); |
| 553 | assertEquals(0, q.size()); |
| 554 | assertEquals(0, l.size()); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * drainTo empties queue, unblocking a waiting put. |
| 559 | */ |
| 560 | public void testDrainToWithActivePut() { testDrainToWithActivePut(false); } |
| 561 | public void testDrainToWithActivePut_fair() { testDrainToWithActivePut(true); } |
| 562 | public void testDrainToWithActivePut(boolean fair) { |
| 563 | final SynchronousQueue q = new SynchronousQueue(fair); |
| 564 | Thread t = newStartedThread(new CheckedRunnable() { |
| 565 | public void realRun() throws InterruptedException { |
| 566 | q.put(one); |
| 567 | }}); |
| 568 | |
| 569 | ArrayList l = new ArrayList(); |
| 570 | long startTime = System.nanoTime(); |
| 571 | while (l.isEmpty()) { |
| 572 | q.drainTo(l); |
| 573 | if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
| 574 | fail("timed out"); |
| 575 | Thread.yield(); |
| 576 | } |
| 577 | assertTrue(l.size() == 1); |
| 578 | assertSame(one, l.get(0)); |
| 579 | awaitTermination(t); |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * drainTo(c, n) empties up to n elements of queue into c |
| 584 | */ |
| 585 | public void testDrainToN() throws InterruptedException { |
| 586 | final SynchronousQueue q = new SynchronousQueue(); |
| 587 | Thread t1 = newStartedThread(new CheckedRunnable() { |
| 588 | public void realRun() throws InterruptedException { |
| 589 | q.put(one); |
| 590 | }}); |
| 591 | |
| 592 | Thread t2 = newStartedThread(new CheckedRunnable() { |
| 593 | public void realRun() throws InterruptedException { |
| 594 | q.put(two); |
| 595 | }}); |
| 596 | |
| 597 | ArrayList l = new ArrayList(); |
| 598 | delay(SHORT_DELAY_MS); |
| 599 | q.drainTo(l, 1); |
| 600 | assertEquals(1, l.size()); |
| 601 | q.drainTo(l, 1); |
| 602 | assertEquals(2, l.size()); |
| 603 | assertTrue(l.contains(one)); |
| 604 | assertTrue(l.contains(two)); |
| 605 | awaitTermination(t1); |
| 606 | awaitTermination(t2); |
| 607 | } |
| 608 | |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 609 | /** |
| 610 | * remove(null), contains(null) always return false |
| 611 | */ |
| 612 | public void testNeverContainsNull() { |
| 613 | Collection<?> q = new SynchronousQueue(); |
| 614 | assertFalse(q.contains(null)); |
| 615 | assertFalse(q.remove(null)); |
| 616 | } |
| 617 | |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 618 | } |