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 | */ |
| 6 | |
| 7 | package jsr166; |
| 8 | |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 9 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 10 | |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 11 | import java.util.ArrayList; |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 12 | import java.util.Arrays; |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 13 | import java.util.Collection; |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 14 | import java.util.Deque; |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 15 | import java.util.Iterator; |
| 16 | import java.util.NoSuchElementException; |
| 17 | import java.util.Queue; |
| 18 | import java.util.concurrent.BlockingDeque; |
| 19 | import java.util.concurrent.BlockingQueue; |
| 20 | import java.util.concurrent.CountDownLatch; |
| 21 | import java.util.concurrent.Executors; |
| 22 | import java.util.concurrent.ExecutorService; |
| 23 | import java.util.concurrent.LinkedBlockingDeque; |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 24 | |
| 25 | import junit.framework.Test; |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 26 | |
| 27 | public class LinkedBlockingDequeTest extends JSR166TestCase { |
| 28 | |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 29 | // android-note: These tests have been moved into their own separate |
| 30 | // classes to work around CTS issues. |
| 31 | // |
| 32 | // public static class Unbounded extends BlockingQueueTest { |
| 33 | // protected BlockingQueue emptyCollection() { |
| 34 | // return new LinkedBlockingDeque(); |
| 35 | // } |
| 36 | // } |
| 37 | // |
| 38 | // public static class Bounded extends BlockingQueueTest { |
| 39 | // protected BlockingQueue emptyCollection() { |
| 40 | // return new LinkedBlockingDeque(SIZE); |
| 41 | // } |
| 42 | // } |
| 43 | // |
| 44 | // public static void main(String[] args) { |
| 45 | // main(suite(), args); |
| 46 | // } |
| 47 | // |
| 48 | // public static Test suite() { |
| 49 | // return newTestSuite(LinkedBlockingDequeTest.class, |
| 50 | // new Unbounded().testSuite(), |
| 51 | // new Bounded().testSuite()); |
| 52 | // } |
| 53 | |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 54 | /** |
| 55 | * Returns a new deque of given size containing consecutive |
| 56 | * Integers 0 ... n. |
| 57 | */ |
| 58 | private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
| 59 | LinkedBlockingDeque<Integer> q = |
| 60 | new LinkedBlockingDeque<Integer>(n); |
| 61 | assertTrue(q.isEmpty()); |
| 62 | for (int i = 0; i < n; i++) |
| 63 | assertTrue(q.offer(new Integer(i))); |
| 64 | assertFalse(q.isEmpty()); |
| 65 | assertEquals(0, q.remainingCapacity()); |
| 66 | assertEquals(n, q.size()); |
| 67 | return q; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * isEmpty is true before add, false after |
| 72 | */ |
| 73 | public void testEmpty() { |
| 74 | LinkedBlockingDeque q = new LinkedBlockingDeque(); |
| 75 | assertTrue(q.isEmpty()); |
| 76 | q.add(new Integer(1)); |
| 77 | assertFalse(q.isEmpty()); |
| 78 | q.add(new Integer(2)); |
| 79 | q.removeFirst(); |
| 80 | q.removeFirst(); |
| 81 | assertTrue(q.isEmpty()); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * size changes when elements added and removed |
| 86 | */ |
| 87 | public void testSize() { |
| 88 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 89 | for (int i = 0; i < SIZE; ++i) { |
| 90 | assertEquals(SIZE-i, q.size()); |
| 91 | q.removeFirst(); |
| 92 | } |
| 93 | for (int i = 0; i < SIZE; ++i) { |
| 94 | assertEquals(i, q.size()); |
| 95 | q.add(new Integer(i)); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * offerFirst(null) throws NullPointerException |
| 101 | */ |
| 102 | public void testOfferFirstNull() { |
| 103 | LinkedBlockingDeque q = new LinkedBlockingDeque(); |
| 104 | try { |
| 105 | q.offerFirst(null); |
| 106 | shouldThrow(); |
| 107 | } catch (NullPointerException success) {} |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * offerLast(null) throws NullPointerException |
| 112 | */ |
| 113 | public void testOfferLastNull() { |
| 114 | LinkedBlockingDeque q = new LinkedBlockingDeque(); |
| 115 | try { |
| 116 | q.offerLast(null); |
| 117 | shouldThrow(); |
| 118 | } catch (NullPointerException success) {} |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * OfferFirst succeeds |
| 123 | */ |
| 124 | public void testOfferFirst() { |
| 125 | LinkedBlockingDeque q = new LinkedBlockingDeque(); |
| 126 | assertTrue(q.offerFirst(new Integer(0))); |
| 127 | assertTrue(q.offerFirst(new Integer(1))); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * OfferLast succeeds |
| 132 | */ |
| 133 | public void testOfferLast() { |
| 134 | LinkedBlockingDeque q = new LinkedBlockingDeque(); |
| 135 | assertTrue(q.offerLast(new Integer(0))); |
| 136 | assertTrue(q.offerLast(new Integer(1))); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * pollFirst succeeds unless empty |
| 141 | */ |
| 142 | public void testPollFirst() { |
| 143 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 144 | for (int i = 0; i < SIZE; ++i) { |
| 145 | assertEquals(i, q.pollFirst()); |
| 146 | } |
| 147 | assertNull(q.pollFirst()); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * pollLast succeeds unless empty |
| 152 | */ |
| 153 | public void testPollLast() { |
| 154 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 155 | for (int i = SIZE-1; i >= 0; --i) { |
| 156 | assertEquals(i, q.pollLast()); |
| 157 | } |
| 158 | assertNull(q.pollLast()); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * peekFirst returns next element, or null if empty |
| 163 | */ |
| 164 | public void testPeekFirst() { |
| 165 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 166 | for (int i = 0; i < SIZE; ++i) { |
| 167 | assertEquals(i, q.peekFirst()); |
| 168 | assertEquals(i, q.pollFirst()); |
| 169 | assertTrue(q.peekFirst() == null || |
| 170 | !q.peekFirst().equals(i)); |
| 171 | } |
| 172 | assertNull(q.peekFirst()); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * peek returns next element, or null if empty |
| 177 | */ |
| 178 | public void testPeek() { |
| 179 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 180 | for (int i = 0; i < SIZE; ++i) { |
| 181 | assertEquals(i, q.peek()); |
| 182 | assertEquals(i, q.pollFirst()); |
| 183 | assertTrue(q.peek() == null || |
| 184 | !q.peek().equals(i)); |
| 185 | } |
| 186 | assertNull(q.peek()); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * peekLast returns next element, or null if empty |
| 191 | */ |
| 192 | public void testPeekLast() { |
| 193 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 194 | for (int i = SIZE-1; i >= 0; --i) { |
| 195 | assertEquals(i, q.peekLast()); |
| 196 | assertEquals(i, q.pollLast()); |
| 197 | assertTrue(q.peekLast() == null || |
| 198 | !q.peekLast().equals(i)); |
| 199 | } |
| 200 | assertNull(q.peekLast()); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * getFirst() returns first element, or throws NSEE if empty |
| 205 | */ |
| 206 | public void testFirstElement() { |
| 207 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 208 | for (int i = 0; i < SIZE; ++i) { |
| 209 | assertEquals(i, q.getFirst()); |
| 210 | assertEquals(i, q.pollFirst()); |
| 211 | } |
| 212 | try { |
| 213 | q.getFirst(); |
| 214 | shouldThrow(); |
| 215 | } catch (NoSuchElementException success) {} |
| 216 | assertNull(q.peekFirst()); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * getLast() returns last element, or throws NSEE if empty |
| 221 | */ |
| 222 | public void testLastElement() { |
| 223 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 224 | for (int i = SIZE-1; i >= 0; --i) { |
| 225 | assertEquals(i, q.getLast()); |
| 226 | assertEquals(i, q.pollLast()); |
| 227 | } |
| 228 | try { |
| 229 | q.getLast(); |
| 230 | shouldThrow(); |
| 231 | } catch (NoSuchElementException success) {} |
| 232 | assertNull(q.peekLast()); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * removeFirst() removes first element, or throws NSEE if empty |
| 237 | */ |
| 238 | public void testRemoveFirst() { |
| 239 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 240 | for (int i = 0; i < SIZE; ++i) { |
| 241 | assertEquals(i, q.removeFirst()); |
| 242 | } |
| 243 | try { |
| 244 | q.removeFirst(); |
| 245 | shouldThrow(); |
| 246 | } catch (NoSuchElementException success) {} |
| 247 | assertNull(q.peekFirst()); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * removeLast() removes last element, or throws NSEE if empty |
| 252 | */ |
| 253 | public void testRemoveLast() { |
| 254 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 255 | for (int i = SIZE - 1; i >= 0; --i) { |
| 256 | assertEquals(i, q.removeLast()); |
| 257 | } |
| 258 | try { |
| 259 | q.removeLast(); |
| 260 | shouldThrow(); |
| 261 | } catch (NoSuchElementException success) {} |
| 262 | assertNull(q.peekLast()); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * remove removes next element, or throws NSEE if empty |
| 267 | */ |
| 268 | public void testRemove() { |
| 269 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 270 | for (int i = 0; i < SIZE; ++i) { |
| 271 | assertEquals(i, q.remove()); |
| 272 | } |
| 273 | try { |
| 274 | q.remove(); |
| 275 | shouldThrow(); |
| 276 | } catch (NoSuchElementException success) {} |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * removeFirstOccurrence(x) removes x and returns true if present |
| 281 | */ |
| 282 | public void testRemoveFirstOccurrence() { |
| 283 | LinkedBlockingDeque q = populatedDeque(SIZE); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 284 | for (int i = 1; i < SIZE; i += 2) { |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 285 | assertTrue(q.removeFirstOccurrence(new Integer(i))); |
| 286 | } |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 287 | for (int i = 0; i < SIZE; i += 2) { |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 288 | assertTrue(q.removeFirstOccurrence(new Integer(i))); |
| 289 | assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
| 290 | } |
| 291 | assertTrue(q.isEmpty()); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * removeLastOccurrence(x) removes x and returns true if present |
| 296 | */ |
| 297 | public void testRemoveLastOccurrence() { |
| 298 | LinkedBlockingDeque q = populatedDeque(SIZE); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 299 | for (int i = 1; i < SIZE; i += 2) { |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 300 | assertTrue(q.removeLastOccurrence(new Integer(i))); |
| 301 | } |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 302 | for (int i = 0; i < SIZE; i += 2) { |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 303 | assertTrue(q.removeLastOccurrence(new Integer(i))); |
| 304 | assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
| 305 | } |
| 306 | assertTrue(q.isEmpty()); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * peekFirst returns element inserted with addFirst |
| 311 | */ |
| 312 | public void testAddFirst() { |
| 313 | LinkedBlockingDeque q = populatedDeque(3); |
| 314 | q.pollLast(); |
| 315 | q.addFirst(four); |
| 316 | assertSame(four, q.peekFirst()); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * peekLast returns element inserted with addLast |
| 321 | */ |
| 322 | public void testAddLast() { |
| 323 | LinkedBlockingDeque q = populatedDeque(3); |
| 324 | q.pollLast(); |
| 325 | q.addLast(four); |
| 326 | assertSame(four, q.peekLast()); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * A new deque has the indicated capacity, or Integer.MAX_VALUE if |
| 331 | * none given |
| 332 | */ |
| 333 | public void testConstructor1() { |
| 334 | assertEquals(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity()); |
| 335 | assertEquals(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity()); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Constructor throws IllegalArgumentException if capacity argument nonpositive |
| 340 | */ |
| 341 | public void testConstructor2() { |
| 342 | try { |
| 343 | new LinkedBlockingDeque(0); |
| 344 | shouldThrow(); |
| 345 | } catch (IllegalArgumentException success) {} |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Initializing from null Collection throws NullPointerException |
| 350 | */ |
| 351 | public void testConstructor3() { |
| 352 | try { |
| 353 | new LinkedBlockingDeque(null); |
| 354 | shouldThrow(); |
| 355 | } catch (NullPointerException success) {} |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Initializing from Collection of null elements throws NullPointerException |
| 360 | */ |
| 361 | public void testConstructor4() { |
| 362 | Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
| 363 | try { |
| 364 | new LinkedBlockingDeque(elements); |
| 365 | shouldThrow(); |
| 366 | } catch (NullPointerException success) {} |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Initializing from Collection with some null elements throws |
| 371 | * NullPointerException |
| 372 | */ |
| 373 | public void testConstructor5() { |
| 374 | Integer[] ints = new Integer[SIZE]; |
| 375 | for (int i = 0; i < SIZE-1; ++i) |
| 376 | ints[i] = i; |
| 377 | Collection<Integer> elements = Arrays.asList(ints); |
| 378 | try { |
| 379 | new LinkedBlockingDeque(elements); |
| 380 | shouldThrow(); |
| 381 | } catch (NullPointerException success) {} |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Deque contains all elements of collection used to initialize |
| 386 | */ |
| 387 | public void testConstructor6() { |
| 388 | Integer[] ints = new Integer[SIZE]; |
| 389 | for (int i = 0; i < SIZE; ++i) |
| 390 | ints[i] = i; |
| 391 | LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
| 392 | for (int i = 0; i < SIZE; ++i) |
| 393 | assertEquals(ints[i], q.poll()); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Deque transitions from empty to full when elements added |
| 398 | */ |
| 399 | public void testEmptyFull() { |
| 400 | LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
| 401 | assertTrue(q.isEmpty()); |
| 402 | assertEquals("should have room for 2", 2, q.remainingCapacity()); |
| 403 | q.add(one); |
| 404 | assertFalse(q.isEmpty()); |
| 405 | q.add(two); |
| 406 | assertFalse(q.isEmpty()); |
| 407 | assertEquals(0, q.remainingCapacity()); |
| 408 | assertFalse(q.offer(three)); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * remainingCapacity decreases on add, increases on remove |
| 413 | */ |
| 414 | public void testRemainingCapacity() { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 415 | BlockingQueue q = populatedDeque(SIZE); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 416 | for (int i = 0; i < SIZE; ++i) { |
| 417 | assertEquals(i, q.remainingCapacity()); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 418 | assertEquals(SIZE, q.size() + q.remainingCapacity()); |
| 419 | assertEquals(i, q.remove()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 420 | } |
| 421 | for (int i = 0; i < SIZE; ++i) { |
| 422 | assertEquals(SIZE-i, q.remainingCapacity()); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 423 | assertEquals(SIZE, q.size() + q.remainingCapacity()); |
| 424 | assertTrue(q.add(i)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * push(null) throws NPE |
| 430 | */ |
| 431 | public void testPushNull() { |
| 432 | try { |
| 433 | LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
| 434 | q.push(null); |
| 435 | shouldThrow(); |
| 436 | } catch (NullPointerException success) {} |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * push succeeds if not full; throws ISE if full |
| 441 | */ |
| 442 | public void testPush() { |
| 443 | try { |
| 444 | LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
| 445 | for (int i = 0; i < SIZE; ++i) { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 446 | Integer x = new Integer(i); |
| 447 | q.push(x); |
| 448 | assertEquals(x, q.peek()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 449 | } |
| 450 | assertEquals(0, q.remainingCapacity()); |
| 451 | q.push(new Integer(SIZE)); |
| 452 | shouldThrow(); |
| 453 | } catch (IllegalStateException success) {} |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * peekFirst returns element inserted with push |
| 458 | */ |
| 459 | public void testPushWithPeek() { |
| 460 | LinkedBlockingDeque q = populatedDeque(3); |
| 461 | q.pollLast(); |
| 462 | q.push(four); |
| 463 | assertSame(four, q.peekFirst()); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * pop removes next element, or throws NSEE if empty |
| 468 | */ |
| 469 | public void testPop() { |
| 470 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 471 | for (int i = 0; i < SIZE; ++i) { |
| 472 | assertEquals(i, q.pop()); |
| 473 | } |
| 474 | try { |
| 475 | q.pop(); |
| 476 | shouldThrow(); |
| 477 | } catch (NoSuchElementException success) {} |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Offer succeeds if not full; fails if full |
| 482 | */ |
| 483 | public void testOffer() { |
| 484 | LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
| 485 | assertTrue(q.offer(zero)); |
| 486 | assertFalse(q.offer(one)); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * add succeeds if not full; throws ISE if full |
| 491 | */ |
| 492 | public void testAdd() { |
| 493 | LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
| 494 | for (int i = 0; i < SIZE; ++i) |
| 495 | assertTrue(q.add(new Integer(i))); |
| 496 | assertEquals(0, q.remainingCapacity()); |
| 497 | try { |
| 498 | q.add(new Integer(SIZE)); |
| 499 | shouldThrow(); |
| 500 | } catch (IllegalStateException success) {} |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * addAll(this) throws IAE |
| 505 | */ |
| 506 | public void testAddAllSelf() { |
| 507 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 508 | try { |
| 509 | q.addAll(q); |
| 510 | shouldThrow(); |
| 511 | } catch (IllegalArgumentException success) {} |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * addAll of a collection with any null elements throws NPE after |
| 516 | * possibly adding some elements |
| 517 | */ |
| 518 | public void testAddAll3() { |
| 519 | LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
| 520 | Integer[] ints = new Integer[SIZE]; |
| 521 | for (int i = 0; i < SIZE-1; ++i) |
| 522 | ints[i] = new Integer(i); |
| 523 | Collection<Integer> elements = Arrays.asList(ints); |
| 524 | try { |
| 525 | q.addAll(elements); |
| 526 | shouldThrow(); |
| 527 | } catch (NullPointerException success) {} |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * addAll throws IllegalStateException if not enough room |
| 532 | */ |
| 533 | public void testAddAll4() { |
| 534 | LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1); |
| 535 | Integer[] ints = new Integer[SIZE]; |
| 536 | for (int i = 0; i < SIZE; ++i) |
| 537 | ints[i] = new Integer(i); |
| 538 | Collection<Integer> elements = Arrays.asList(ints); |
| 539 | try { |
| 540 | q.addAll(elements); |
| 541 | shouldThrow(); |
| 542 | } catch (IllegalStateException success) {} |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Deque contains all elements, in traversal order, of successful addAll |
| 547 | */ |
| 548 | public void testAddAll5() { |
| 549 | Integer[] empty = new Integer[0]; |
| 550 | Integer[] ints = new Integer[SIZE]; |
| 551 | for (int i = 0; i < SIZE; ++i) |
| 552 | ints[i] = new Integer(i); |
| 553 | LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
| 554 | assertFalse(q.addAll(Arrays.asList(empty))); |
| 555 | assertTrue(q.addAll(Arrays.asList(ints))); |
| 556 | for (int i = 0; i < SIZE; ++i) |
| 557 | assertEquals(ints[i], q.poll()); |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * all elements successfully put are contained |
| 562 | */ |
| 563 | public void testPut() throws InterruptedException { |
| 564 | LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
| 565 | for (int i = 0; i < SIZE; ++i) { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 566 | Integer x = new Integer(i); |
| 567 | q.put(x); |
| 568 | assertTrue(q.contains(x)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 569 | } |
| 570 | assertEquals(0, q.remainingCapacity()); |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * put blocks interruptibly if full |
| 575 | */ |
| 576 | public void testBlockingPut() throws InterruptedException { |
| 577 | final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
| 578 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 579 | Thread t = newStartedThread(new CheckedRunnable() { |
| 580 | public void realRun() throws InterruptedException { |
| 581 | for (int i = 0; i < SIZE; ++i) |
| 582 | q.put(i); |
| 583 | assertEquals(SIZE, q.size()); |
| 584 | assertEquals(0, q.remainingCapacity()); |
| 585 | |
| 586 | Thread.currentThread().interrupt(); |
| 587 | try { |
| 588 | q.put(99); |
| 589 | shouldThrow(); |
| 590 | } catch (InterruptedException success) {} |
| 591 | assertFalse(Thread.interrupted()); |
| 592 | |
| 593 | pleaseInterrupt.countDown(); |
| 594 | try { |
| 595 | q.put(99); |
| 596 | shouldThrow(); |
| 597 | } catch (InterruptedException success) {} |
| 598 | assertFalse(Thread.interrupted()); |
| 599 | }}); |
| 600 | |
| 601 | await(pleaseInterrupt); |
| 602 | assertThreadStaysAlive(t); |
| 603 | t.interrupt(); |
| 604 | awaitTermination(t); |
| 605 | assertEquals(SIZE, q.size()); |
| 606 | assertEquals(0, q.remainingCapacity()); |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * put blocks interruptibly waiting for take when full |
| 611 | */ |
| 612 | public void testPutWithTake() throws InterruptedException { |
| 613 | final int capacity = 2; |
| 614 | final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
| 615 | final CountDownLatch pleaseTake = new CountDownLatch(1); |
| 616 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 617 | Thread t = newStartedThread(new CheckedRunnable() { |
| 618 | public void realRun() throws InterruptedException { |
| 619 | for (int i = 0; i < capacity; i++) |
| 620 | q.put(i); |
| 621 | pleaseTake.countDown(); |
| 622 | q.put(86); |
| 623 | |
| 624 | pleaseInterrupt.countDown(); |
| 625 | try { |
| 626 | q.put(99); |
| 627 | shouldThrow(); |
| 628 | } catch (InterruptedException success) {} |
| 629 | assertFalse(Thread.interrupted()); |
| 630 | }}); |
| 631 | |
| 632 | await(pleaseTake); |
| 633 | assertEquals(0, q.remainingCapacity()); |
| 634 | assertEquals(0, q.take()); |
| 635 | |
| 636 | await(pleaseInterrupt); |
| 637 | assertThreadStaysAlive(t); |
| 638 | t.interrupt(); |
| 639 | awaitTermination(t); |
| 640 | assertEquals(0, q.remainingCapacity()); |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * timed offer times out if full and elements not taken |
| 645 | */ |
| 646 | public void testTimedOffer() throws InterruptedException { |
| 647 | final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
| 648 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 649 | Thread t = newStartedThread(new CheckedRunnable() { |
| 650 | public void realRun() throws InterruptedException { |
| 651 | q.put(new Object()); |
| 652 | q.put(new Object()); |
| 653 | long startTime = System.nanoTime(); |
| 654 | assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS)); |
| 655 | assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
| 656 | pleaseInterrupt.countDown(); |
| 657 | try { |
| 658 | q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
| 659 | shouldThrow(); |
| 660 | } catch (InterruptedException success) {} |
| 661 | }}); |
| 662 | |
| 663 | await(pleaseInterrupt); |
| 664 | assertThreadStaysAlive(t); |
| 665 | t.interrupt(); |
| 666 | awaitTermination(t); |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * take retrieves elements in FIFO order |
| 671 | */ |
| 672 | public void testTake() throws InterruptedException { |
| 673 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 674 | for (int i = 0; i < SIZE; ++i) { |
| 675 | assertEquals(i, q.take()); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * take removes existing elements until empty, then blocks interruptibly |
| 681 | */ |
| 682 | public void testBlockingTake() throws InterruptedException { |
| 683 | final LinkedBlockingDeque q = populatedDeque(SIZE); |
| 684 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 685 | Thread t = newStartedThread(new CheckedRunnable() { |
| 686 | public void realRun() throws InterruptedException { |
| 687 | for (int i = 0; i < SIZE; ++i) { |
| 688 | assertEquals(i, q.take()); |
| 689 | } |
| 690 | |
| 691 | Thread.currentThread().interrupt(); |
| 692 | try { |
| 693 | q.take(); |
| 694 | shouldThrow(); |
| 695 | } catch (InterruptedException success) {} |
| 696 | assertFalse(Thread.interrupted()); |
| 697 | |
| 698 | pleaseInterrupt.countDown(); |
| 699 | try { |
| 700 | q.take(); |
| 701 | shouldThrow(); |
| 702 | } catch (InterruptedException success) {} |
| 703 | assertFalse(Thread.interrupted()); |
| 704 | }}); |
| 705 | |
| 706 | await(pleaseInterrupt); |
| 707 | assertThreadStaysAlive(t); |
| 708 | t.interrupt(); |
| 709 | awaitTermination(t); |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * poll succeeds unless empty |
| 714 | */ |
| 715 | public void testPoll() { |
| 716 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 717 | for (int i = 0; i < SIZE; ++i) { |
| 718 | assertEquals(i, q.poll()); |
| 719 | } |
| 720 | assertNull(q.poll()); |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * timed poll with zero timeout succeeds when non-empty, else times out |
| 725 | */ |
| 726 | public void testTimedPoll0() throws InterruptedException { |
| 727 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 728 | for (int i = 0; i < SIZE; ++i) { |
| 729 | assertEquals(i, q.poll(0, MILLISECONDS)); |
| 730 | } |
| 731 | assertNull(q.poll(0, MILLISECONDS)); |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * timed poll with nonzero timeout succeeds when non-empty, else times out |
| 736 | */ |
| 737 | public void testTimedPoll() throws InterruptedException { |
| 738 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 739 | for (int i = 0; i < SIZE; ++i) { |
| 740 | long startTime = System.nanoTime(); |
| 741 | assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
| 742 | assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
| 743 | } |
| 744 | long startTime = System.nanoTime(); |
| 745 | assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
| 746 | assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
| 747 | checkEmpty(q); |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Interrupted timed poll throws InterruptedException instead of |
| 752 | * returning timeout status |
| 753 | */ |
| 754 | public void testInterruptedTimedPoll() throws InterruptedException { |
| 755 | final BlockingQueue<Integer> q = populatedDeque(SIZE); |
| 756 | final CountDownLatch aboutToWait = new CountDownLatch(1); |
| 757 | Thread t = newStartedThread(new CheckedRunnable() { |
| 758 | public void realRun() throws InterruptedException { |
| 759 | for (int i = 0; i < SIZE; ++i) { |
| 760 | long t0 = System.nanoTime(); |
| 761 | assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
| 762 | assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
| 763 | } |
| 764 | long t0 = System.nanoTime(); |
| 765 | aboutToWait.countDown(); |
| 766 | try { |
| 767 | q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
| 768 | shouldThrow(); |
| 769 | } catch (InterruptedException success) { |
| 770 | assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
| 771 | } |
| 772 | }}); |
| 773 | |
| 774 | aboutToWait.await(); |
| 775 | waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
| 776 | t.interrupt(); |
| 777 | awaitTermination(t, MEDIUM_DELAY_MS); |
| 778 | checkEmpty(q); |
| 779 | } |
| 780 | |
| 781 | /** |
| 782 | * putFirst(null) throws NPE |
| 783 | */ |
| 784 | public void testPutFirstNull() throws InterruptedException { |
| 785 | LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
| 786 | try { |
| 787 | q.putFirst(null); |
| 788 | shouldThrow(); |
| 789 | } catch (NullPointerException success) {} |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * all elements successfully putFirst are contained |
| 794 | */ |
| 795 | public void testPutFirst() throws InterruptedException { |
| 796 | LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
| 797 | for (int i = 0; i < SIZE; ++i) { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 798 | Integer x = new Integer(i); |
| 799 | q.putFirst(x); |
| 800 | assertTrue(q.contains(x)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 801 | } |
| 802 | assertEquals(0, q.remainingCapacity()); |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * putFirst blocks interruptibly if full |
| 807 | */ |
| 808 | public void testBlockingPutFirst() throws InterruptedException { |
| 809 | final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
| 810 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 811 | Thread t = newStartedThread(new CheckedRunnable() { |
| 812 | public void realRun() throws InterruptedException { |
| 813 | for (int i = 0; i < SIZE; ++i) |
| 814 | q.putFirst(i); |
| 815 | assertEquals(SIZE, q.size()); |
| 816 | assertEquals(0, q.remainingCapacity()); |
| 817 | |
| 818 | Thread.currentThread().interrupt(); |
| 819 | try { |
| 820 | q.putFirst(99); |
| 821 | shouldThrow(); |
| 822 | } catch (InterruptedException success) {} |
| 823 | assertFalse(Thread.interrupted()); |
| 824 | |
| 825 | pleaseInterrupt.countDown(); |
| 826 | try { |
| 827 | q.putFirst(99); |
| 828 | shouldThrow(); |
| 829 | } catch (InterruptedException success) {} |
| 830 | assertFalse(Thread.interrupted()); |
| 831 | }}); |
| 832 | |
| 833 | await(pleaseInterrupt); |
| 834 | assertThreadStaysAlive(t); |
| 835 | t.interrupt(); |
| 836 | awaitTermination(t); |
| 837 | assertEquals(SIZE, q.size()); |
| 838 | assertEquals(0, q.remainingCapacity()); |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * putFirst blocks interruptibly waiting for take when full |
| 843 | */ |
| 844 | public void testPutFirstWithTake() throws InterruptedException { |
| 845 | final int capacity = 2; |
| 846 | final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
| 847 | final CountDownLatch pleaseTake = new CountDownLatch(1); |
| 848 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 849 | Thread t = newStartedThread(new CheckedRunnable() { |
| 850 | public void realRun() throws InterruptedException { |
| 851 | for (int i = 0; i < capacity; i++) |
| 852 | q.putFirst(i); |
| 853 | pleaseTake.countDown(); |
| 854 | q.putFirst(86); |
| 855 | |
| 856 | pleaseInterrupt.countDown(); |
| 857 | try { |
| 858 | q.putFirst(99); |
| 859 | shouldThrow(); |
| 860 | } catch (InterruptedException success) {} |
| 861 | assertFalse(Thread.interrupted()); |
| 862 | }}); |
| 863 | |
| 864 | await(pleaseTake); |
| 865 | assertEquals(0, q.remainingCapacity()); |
| 866 | assertEquals(capacity - 1, q.take()); |
| 867 | |
| 868 | await(pleaseInterrupt); |
| 869 | assertThreadStaysAlive(t); |
| 870 | t.interrupt(); |
| 871 | awaitTermination(t); |
| 872 | assertEquals(0, q.remainingCapacity()); |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * timed offerFirst times out if full and elements not taken |
| 877 | */ |
| 878 | public void testTimedOfferFirst() throws InterruptedException { |
| 879 | final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
| 880 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 881 | Thread t = newStartedThread(new CheckedRunnable() { |
| 882 | public void realRun() throws InterruptedException { |
| 883 | q.putFirst(new Object()); |
| 884 | q.putFirst(new Object()); |
| 885 | long startTime = System.nanoTime(); |
| 886 | assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS)); |
| 887 | assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
| 888 | pleaseInterrupt.countDown(); |
| 889 | try { |
| 890 | q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
| 891 | shouldThrow(); |
| 892 | } catch (InterruptedException success) {} |
| 893 | }}); |
| 894 | |
| 895 | await(pleaseInterrupt); |
| 896 | assertThreadStaysAlive(t); |
| 897 | t.interrupt(); |
| 898 | awaitTermination(t); |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * take retrieves elements in FIFO order |
| 903 | */ |
| 904 | public void testTakeFirst() throws InterruptedException { |
| 905 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 906 | for (int i = 0; i < SIZE; ++i) { |
| 907 | assertEquals(i, q.takeFirst()); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | /** |
| 912 | * takeFirst() blocks interruptibly when empty |
| 913 | */ |
| 914 | public void testTakeFirstFromEmptyBlocksInterruptibly() { |
| 915 | final BlockingDeque q = new LinkedBlockingDeque(); |
| 916 | final CountDownLatch threadStarted = new CountDownLatch(1); |
| 917 | Thread t = newStartedThread(new CheckedRunnable() { |
| 918 | public void realRun() { |
| 919 | threadStarted.countDown(); |
| 920 | try { |
| 921 | q.takeFirst(); |
| 922 | shouldThrow(); |
| 923 | } catch (InterruptedException success) {} |
| 924 | assertFalse(Thread.interrupted()); |
| 925 | }}); |
| 926 | |
| 927 | await(threadStarted); |
| 928 | assertThreadStaysAlive(t); |
| 929 | t.interrupt(); |
| 930 | awaitTermination(t); |
| 931 | } |
| 932 | |
| 933 | /** |
| 934 | * takeFirst() throws InterruptedException immediately if interrupted |
| 935 | * before waiting |
| 936 | */ |
| 937 | public void testTakeFirstFromEmptyAfterInterrupt() { |
| 938 | final BlockingDeque q = new LinkedBlockingDeque(); |
| 939 | Thread t = newStartedThread(new CheckedRunnable() { |
| 940 | public void realRun() { |
| 941 | Thread.currentThread().interrupt(); |
| 942 | try { |
| 943 | q.takeFirst(); |
| 944 | shouldThrow(); |
| 945 | } catch (InterruptedException success) {} |
| 946 | assertFalse(Thread.interrupted()); |
| 947 | }}); |
| 948 | |
| 949 | awaitTermination(t); |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * takeLast() blocks interruptibly when empty |
| 954 | */ |
| 955 | public void testTakeLastFromEmptyBlocksInterruptibly() { |
| 956 | final BlockingDeque q = new LinkedBlockingDeque(); |
| 957 | final CountDownLatch threadStarted = new CountDownLatch(1); |
| 958 | Thread t = newStartedThread(new CheckedRunnable() { |
| 959 | public void realRun() { |
| 960 | threadStarted.countDown(); |
| 961 | try { |
| 962 | q.takeLast(); |
| 963 | shouldThrow(); |
| 964 | } catch (InterruptedException success) {} |
| 965 | assertFalse(Thread.interrupted()); |
| 966 | }}); |
| 967 | |
| 968 | await(threadStarted); |
| 969 | assertThreadStaysAlive(t); |
| 970 | t.interrupt(); |
| 971 | awaitTermination(t); |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * takeLast() throws InterruptedException immediately if interrupted |
| 976 | * before waiting |
| 977 | */ |
| 978 | public void testTakeLastFromEmptyAfterInterrupt() { |
| 979 | final BlockingDeque q = new LinkedBlockingDeque(); |
| 980 | Thread t = newStartedThread(new CheckedRunnable() { |
| 981 | public void realRun() { |
| 982 | Thread.currentThread().interrupt(); |
| 983 | try { |
| 984 | q.takeLast(); |
| 985 | shouldThrow(); |
| 986 | } catch (InterruptedException success) {} |
| 987 | assertFalse(Thread.interrupted()); |
| 988 | }}); |
| 989 | |
| 990 | awaitTermination(t); |
| 991 | } |
| 992 | |
| 993 | /** |
| 994 | * takeFirst removes existing elements until empty, then blocks interruptibly |
| 995 | */ |
| 996 | public void testBlockingTakeFirst() throws InterruptedException { |
| 997 | final LinkedBlockingDeque q = populatedDeque(SIZE); |
| 998 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 999 | Thread t = newStartedThread(new CheckedRunnable() { |
| 1000 | public void realRun() throws InterruptedException { |
| 1001 | for (int i = 0; i < SIZE; ++i) { |
| 1002 | assertEquals(i, q.takeFirst()); |
| 1003 | } |
| 1004 | |
| 1005 | Thread.currentThread().interrupt(); |
| 1006 | try { |
| 1007 | q.takeFirst(); |
| 1008 | shouldThrow(); |
| 1009 | } catch (InterruptedException success) {} |
| 1010 | assertFalse(Thread.interrupted()); |
| 1011 | |
| 1012 | pleaseInterrupt.countDown(); |
| 1013 | try { |
| 1014 | q.takeFirst(); |
| 1015 | shouldThrow(); |
| 1016 | } catch (InterruptedException success) {} |
| 1017 | assertFalse(Thread.interrupted()); |
| 1018 | }}); |
| 1019 | |
| 1020 | await(pleaseInterrupt); |
| 1021 | assertThreadStaysAlive(t); |
| 1022 | t.interrupt(); |
| 1023 | awaitTermination(t); |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * timed pollFirst with zero timeout succeeds when non-empty, else times out |
| 1028 | */ |
| 1029 | public void testTimedPollFirst0() throws InterruptedException { |
| 1030 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1031 | for (int i = 0; i < SIZE; ++i) { |
| 1032 | assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
| 1033 | } |
| 1034 | assertNull(q.pollFirst(0, MILLISECONDS)); |
| 1035 | } |
| 1036 | |
| 1037 | /** |
| 1038 | * timed pollFirst with nonzero timeout succeeds when non-empty, else times out |
| 1039 | */ |
| 1040 | public void testTimedPollFirst() throws InterruptedException { |
| 1041 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1042 | for (int i = 0; i < SIZE; ++i) { |
| 1043 | long startTime = System.nanoTime(); |
| 1044 | assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
| 1045 | assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
| 1046 | } |
| 1047 | long startTime = System.nanoTime(); |
| 1048 | assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS)); |
| 1049 | assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
| 1050 | checkEmpty(q); |
| 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * Interrupted timed pollFirst throws InterruptedException instead of |
| 1055 | * returning timeout status |
| 1056 | */ |
| 1057 | public void testInterruptedTimedPollFirst() throws InterruptedException { |
| 1058 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 1059 | Thread t = newStartedThread(new CheckedRunnable() { |
| 1060 | public void realRun() throws InterruptedException { |
| 1061 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1062 | for (int i = 0; i < SIZE; ++i) { |
| 1063 | assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
| 1064 | } |
| 1065 | |
| 1066 | Thread.currentThread().interrupt(); |
| 1067 | try { |
| 1068 | q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
| 1069 | shouldThrow(); |
| 1070 | } catch (InterruptedException success) {} |
| 1071 | assertFalse(Thread.interrupted()); |
| 1072 | |
| 1073 | pleaseInterrupt.countDown(); |
| 1074 | try { |
| 1075 | q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
| 1076 | shouldThrow(); |
| 1077 | } catch (InterruptedException success) {} |
| 1078 | assertFalse(Thread.interrupted()); |
| 1079 | }}); |
| 1080 | |
| 1081 | await(pleaseInterrupt); |
| 1082 | assertThreadStaysAlive(t); |
| 1083 | t.interrupt(); |
| 1084 | awaitTermination(t); |
| 1085 | } |
| 1086 | |
| 1087 | /** |
| 1088 | * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
| 1089 | * on interruption throws |
| 1090 | */ |
| 1091 | public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
| 1092 | final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
| 1093 | final CheckedBarrier barrier = new CheckedBarrier(2); |
| 1094 | Thread t = newStartedThread(new CheckedRunnable() { |
| 1095 | public void realRun() throws InterruptedException { |
| 1096 | long startTime = System.nanoTime(); |
| 1097 | assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS)); |
| 1098 | assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
| 1099 | |
| 1100 | barrier.await(); |
| 1101 | |
| 1102 | assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
| 1103 | |
| 1104 | Thread.currentThread().interrupt(); |
| 1105 | try { |
| 1106 | q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
| 1107 | shouldThrow(); |
| 1108 | } catch (InterruptedException success) {} |
| 1109 | |
| 1110 | barrier.await(); |
| 1111 | try { |
| 1112 | q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
| 1113 | shouldThrow(); |
| 1114 | } catch (InterruptedException success) {} |
| 1115 | assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
| 1116 | }}); |
| 1117 | |
| 1118 | barrier.await(); |
| 1119 | long startTime = System.nanoTime(); |
| 1120 | assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS)); |
| 1121 | assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
| 1122 | barrier.await(); |
| 1123 | assertThreadStaysAlive(t); |
| 1124 | t.interrupt(); |
| 1125 | awaitTermination(t); |
| 1126 | } |
| 1127 | |
| 1128 | /** |
| 1129 | * putLast(null) throws NPE |
| 1130 | */ |
| 1131 | public void testPutLastNull() throws InterruptedException { |
| 1132 | LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
| 1133 | try { |
| 1134 | q.putLast(null); |
| 1135 | shouldThrow(); |
| 1136 | } catch (NullPointerException success) {} |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * all elements successfully putLast are contained |
| 1141 | */ |
| 1142 | public void testPutLast() throws InterruptedException { |
| 1143 | LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
| 1144 | for (int i = 0; i < SIZE; ++i) { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 1145 | Integer x = new Integer(i); |
| 1146 | q.putLast(x); |
| 1147 | assertTrue(q.contains(x)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 1148 | } |
| 1149 | assertEquals(0, q.remainingCapacity()); |
| 1150 | } |
| 1151 | |
| 1152 | /** |
| 1153 | * putLast blocks interruptibly if full |
| 1154 | */ |
| 1155 | public void testBlockingPutLast() throws InterruptedException { |
| 1156 | final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
| 1157 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 1158 | Thread t = newStartedThread(new CheckedRunnable() { |
| 1159 | public void realRun() throws InterruptedException { |
| 1160 | for (int i = 0; i < SIZE; ++i) |
| 1161 | q.putLast(i); |
| 1162 | assertEquals(SIZE, q.size()); |
| 1163 | assertEquals(0, q.remainingCapacity()); |
| 1164 | |
| 1165 | Thread.currentThread().interrupt(); |
| 1166 | try { |
| 1167 | q.putLast(99); |
| 1168 | shouldThrow(); |
| 1169 | } catch (InterruptedException success) {} |
| 1170 | assertFalse(Thread.interrupted()); |
| 1171 | |
| 1172 | pleaseInterrupt.countDown(); |
| 1173 | try { |
| 1174 | q.putLast(99); |
| 1175 | shouldThrow(); |
| 1176 | } catch (InterruptedException success) {} |
| 1177 | assertFalse(Thread.interrupted()); |
| 1178 | }}); |
| 1179 | |
| 1180 | await(pleaseInterrupt); |
| 1181 | assertThreadStaysAlive(t); |
| 1182 | t.interrupt(); |
| 1183 | awaitTermination(t); |
| 1184 | assertEquals(SIZE, q.size()); |
| 1185 | assertEquals(0, q.remainingCapacity()); |
| 1186 | } |
| 1187 | |
| 1188 | /** |
| 1189 | * putLast blocks interruptibly waiting for take when full |
| 1190 | */ |
| 1191 | public void testPutLastWithTake() throws InterruptedException { |
| 1192 | final int capacity = 2; |
| 1193 | final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
| 1194 | final CountDownLatch pleaseTake = new CountDownLatch(1); |
| 1195 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 1196 | Thread t = newStartedThread(new CheckedRunnable() { |
| 1197 | public void realRun() throws InterruptedException { |
| 1198 | for (int i = 0; i < capacity; i++) |
| 1199 | q.putLast(i); |
| 1200 | pleaseTake.countDown(); |
| 1201 | q.putLast(86); |
| 1202 | |
| 1203 | pleaseInterrupt.countDown(); |
| 1204 | try { |
| 1205 | q.putLast(99); |
| 1206 | shouldThrow(); |
| 1207 | } catch (InterruptedException success) {} |
| 1208 | assertFalse(Thread.interrupted()); |
| 1209 | }}); |
| 1210 | |
| 1211 | await(pleaseTake); |
| 1212 | assertEquals(0, q.remainingCapacity()); |
| 1213 | assertEquals(0, q.take()); |
| 1214 | |
| 1215 | await(pleaseInterrupt); |
| 1216 | assertThreadStaysAlive(t); |
| 1217 | t.interrupt(); |
| 1218 | awaitTermination(t); |
| 1219 | assertEquals(0, q.remainingCapacity()); |
| 1220 | } |
| 1221 | |
| 1222 | /** |
| 1223 | * timed offerLast times out if full and elements not taken |
| 1224 | */ |
| 1225 | public void testTimedOfferLast() throws InterruptedException { |
| 1226 | final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
| 1227 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 1228 | Thread t = newStartedThread(new CheckedRunnable() { |
| 1229 | public void realRun() throws InterruptedException { |
| 1230 | q.putLast(new Object()); |
| 1231 | q.putLast(new Object()); |
| 1232 | long startTime = System.nanoTime(); |
| 1233 | assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS)); |
| 1234 | assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
| 1235 | pleaseInterrupt.countDown(); |
| 1236 | try { |
| 1237 | q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS); |
| 1238 | shouldThrow(); |
| 1239 | } catch (InterruptedException success) {} |
| 1240 | }}); |
| 1241 | |
| 1242 | await(pleaseInterrupt); |
| 1243 | assertThreadStaysAlive(t); |
| 1244 | t.interrupt(); |
| 1245 | awaitTermination(t); |
| 1246 | } |
| 1247 | |
| 1248 | /** |
| 1249 | * takeLast retrieves elements in FIFO order |
| 1250 | */ |
| 1251 | public void testTakeLast() throws InterruptedException { |
| 1252 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1253 | for (int i = 0; i < SIZE; ++i) { |
| 1254 | assertEquals(SIZE-i-1, q.takeLast()); |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | /** |
| 1259 | * takeLast removes existing elements until empty, then blocks interruptibly |
| 1260 | */ |
| 1261 | public void testBlockingTakeLast() throws InterruptedException { |
| 1262 | final LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1263 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 1264 | Thread t = newStartedThread(new CheckedRunnable() { |
| 1265 | public void realRun() throws InterruptedException { |
| 1266 | for (int i = 0; i < SIZE; ++i) { |
| 1267 | assertEquals(SIZE-i-1, q.takeLast()); |
| 1268 | } |
| 1269 | |
| 1270 | Thread.currentThread().interrupt(); |
| 1271 | try { |
| 1272 | q.takeLast(); |
| 1273 | shouldThrow(); |
| 1274 | } catch (InterruptedException success) {} |
| 1275 | assertFalse(Thread.interrupted()); |
| 1276 | |
| 1277 | pleaseInterrupt.countDown(); |
| 1278 | try { |
| 1279 | q.takeLast(); |
| 1280 | shouldThrow(); |
| 1281 | } catch (InterruptedException success) {} |
| 1282 | assertFalse(Thread.interrupted()); |
| 1283 | }}); |
| 1284 | |
| 1285 | await(pleaseInterrupt); |
| 1286 | assertThreadStaysAlive(t); |
| 1287 | t.interrupt(); |
| 1288 | awaitTermination(t); |
| 1289 | } |
| 1290 | |
| 1291 | /** |
| 1292 | * timed pollLast with zero timeout succeeds when non-empty, else times out |
| 1293 | */ |
| 1294 | public void testTimedPollLast0() throws InterruptedException { |
| 1295 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1296 | for (int i = 0; i < SIZE; ++i) { |
| 1297 | assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
| 1298 | } |
| 1299 | assertNull(q.pollLast(0, MILLISECONDS)); |
| 1300 | } |
| 1301 | |
| 1302 | /** |
| 1303 | * timed pollLast with nonzero timeout succeeds when non-empty, else times out |
| 1304 | */ |
| 1305 | public void testTimedPollLast() throws InterruptedException { |
| 1306 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1307 | for (int i = 0; i < SIZE; ++i) { |
| 1308 | long startTime = System.nanoTime(); |
| 1309 | assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
| 1310 | assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
| 1311 | } |
| 1312 | long startTime = System.nanoTime(); |
| 1313 | assertNull(q.pollLast(timeoutMillis(), MILLISECONDS)); |
| 1314 | assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
| 1315 | checkEmpty(q); |
| 1316 | } |
| 1317 | |
| 1318 | /** |
| 1319 | * Interrupted timed pollLast throws InterruptedException instead of |
| 1320 | * returning timeout status |
| 1321 | */ |
| 1322 | public void testInterruptedTimedPollLast() throws InterruptedException { |
| 1323 | final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
| 1324 | Thread t = newStartedThread(new CheckedRunnable() { |
| 1325 | public void realRun() throws InterruptedException { |
| 1326 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1327 | for (int i = 0; i < SIZE; ++i) { |
| 1328 | assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
| 1329 | } |
| 1330 | |
| 1331 | Thread.currentThread().interrupt(); |
| 1332 | try { |
| 1333 | q.pollLast(LONG_DELAY_MS, MILLISECONDS); |
| 1334 | shouldThrow(); |
| 1335 | } catch (InterruptedException success) {} |
| 1336 | assertFalse(Thread.interrupted()); |
| 1337 | |
| 1338 | pleaseInterrupt.countDown(); |
| 1339 | try { |
| 1340 | q.pollLast(LONG_DELAY_MS, MILLISECONDS); |
| 1341 | shouldThrow(); |
| 1342 | } catch (InterruptedException success) {} |
| 1343 | assertFalse(Thread.interrupted()); |
| 1344 | }}); |
| 1345 | |
| 1346 | await(pleaseInterrupt); |
| 1347 | assertThreadStaysAlive(t); |
| 1348 | t.interrupt(); |
| 1349 | awaitTermination(t); |
| 1350 | } |
| 1351 | |
| 1352 | /** |
| 1353 | * timed poll before a delayed offerLast fails; after offerLast succeeds; |
| 1354 | * on interruption throws |
| 1355 | */ |
| 1356 | public void testTimedPollWithOfferLast() throws InterruptedException { |
| 1357 | final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
| 1358 | final CheckedBarrier barrier = new CheckedBarrier(2); |
| 1359 | Thread t = newStartedThread(new CheckedRunnable() { |
| 1360 | public void realRun() throws InterruptedException { |
| 1361 | long startTime = System.nanoTime(); |
| 1362 | assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
| 1363 | assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
| 1364 | |
| 1365 | barrier.await(); |
| 1366 | |
| 1367 | assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
| 1368 | |
| 1369 | Thread.currentThread().interrupt(); |
| 1370 | try { |
| 1371 | q.poll(LONG_DELAY_MS, MILLISECONDS); |
| 1372 | shouldThrow(); |
| 1373 | } catch (InterruptedException success) {} |
| 1374 | assertFalse(Thread.interrupted()); |
| 1375 | |
| 1376 | barrier.await(); |
| 1377 | try { |
| 1378 | q.poll(LONG_DELAY_MS, MILLISECONDS); |
| 1379 | shouldThrow(); |
| 1380 | } catch (InterruptedException success) {} |
| 1381 | assertFalse(Thread.interrupted()); |
| 1382 | }}); |
| 1383 | |
| 1384 | barrier.await(); |
| 1385 | long startTime = System.nanoTime(); |
| 1386 | assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS)); |
| 1387 | assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
| 1388 | |
| 1389 | barrier.await(); |
| 1390 | assertThreadStaysAlive(t); |
| 1391 | t.interrupt(); |
| 1392 | awaitTermination(t); |
| 1393 | } |
| 1394 | |
| 1395 | /** |
| 1396 | * element returns next element, or throws NSEE if empty |
| 1397 | */ |
| 1398 | public void testElement() { |
| 1399 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1400 | for (int i = 0; i < SIZE; ++i) { |
| 1401 | assertEquals(i, q.element()); |
| 1402 | q.poll(); |
| 1403 | } |
| 1404 | try { |
| 1405 | q.element(); |
| 1406 | shouldThrow(); |
| 1407 | } catch (NoSuchElementException success) {} |
| 1408 | } |
| 1409 | |
| 1410 | /** |
| 1411 | * contains(x) reports true when elements added but not yet removed |
| 1412 | */ |
| 1413 | public void testContains() { |
| 1414 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1415 | for (int i = 0; i < SIZE; ++i) { |
| 1416 | assertTrue(q.contains(new Integer(i))); |
| 1417 | q.poll(); |
| 1418 | assertFalse(q.contains(new Integer(i))); |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | /** |
| 1423 | * clear removes all elements |
| 1424 | */ |
| 1425 | public void testClear() { |
| 1426 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1427 | q.clear(); |
| 1428 | assertTrue(q.isEmpty()); |
| 1429 | assertEquals(0, q.size()); |
| 1430 | assertEquals(SIZE, q.remainingCapacity()); |
| 1431 | q.add(one); |
| 1432 | assertFalse(q.isEmpty()); |
| 1433 | assertTrue(q.contains(one)); |
| 1434 | q.clear(); |
| 1435 | assertTrue(q.isEmpty()); |
| 1436 | } |
| 1437 | |
| 1438 | /** |
| 1439 | * containsAll(c) is true when c contains a subset of elements |
| 1440 | */ |
| 1441 | public void testContainsAll() { |
| 1442 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1443 | LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE); |
| 1444 | for (int i = 0; i < SIZE; ++i) { |
| 1445 | assertTrue(q.containsAll(p)); |
| 1446 | assertFalse(p.containsAll(q)); |
| 1447 | p.add(new Integer(i)); |
| 1448 | } |
| 1449 | assertTrue(p.containsAll(q)); |
| 1450 | } |
| 1451 | |
| 1452 | /** |
| 1453 | * retainAll(c) retains only those elements of c and reports true if changed |
| 1454 | */ |
| 1455 | public void testRetainAll() { |
| 1456 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1457 | LinkedBlockingDeque p = populatedDeque(SIZE); |
| 1458 | for (int i = 0; i < SIZE; ++i) { |
| 1459 | boolean changed = q.retainAll(p); |
| 1460 | if (i == 0) |
| 1461 | assertFalse(changed); |
| 1462 | else |
| 1463 | assertTrue(changed); |
| 1464 | |
| 1465 | assertTrue(q.containsAll(p)); |
| 1466 | assertEquals(SIZE-i, q.size()); |
| 1467 | p.remove(); |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | /** |
| 1472 | * removeAll(c) removes only those elements of c and reports true if changed |
| 1473 | */ |
| 1474 | public void testRemoveAll() { |
| 1475 | for (int i = 1; i < SIZE; ++i) { |
| 1476 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1477 | LinkedBlockingDeque p = populatedDeque(i); |
| 1478 | assertTrue(q.removeAll(p)); |
| 1479 | assertEquals(SIZE-i, q.size()); |
| 1480 | for (int j = 0; j < i; ++j) { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 1481 | Integer x = (Integer)(p.remove()); |
| 1482 | assertFalse(q.contains(x)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | /** |
| 1488 | * toArray contains all elements in FIFO order |
| 1489 | */ |
| 1490 | public void testToArray() throws InterruptedException { |
| 1491 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1492 | Object[] o = q.toArray(); |
| 1493 | for (int i = 0; i < o.length; i++) |
| 1494 | assertSame(o[i], q.poll()); |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * toArray(a) contains all elements in FIFO order |
| 1499 | */ |
| 1500 | public void testToArray2() { |
| 1501 | LinkedBlockingDeque<Integer> q = populatedDeque(SIZE); |
| 1502 | Integer[] ints = new Integer[SIZE]; |
| 1503 | Integer[] array = q.toArray(ints); |
| 1504 | assertSame(ints, array); |
| 1505 | for (int i = 0; i < ints.length; i++) |
| 1506 | assertSame(ints[i], q.remove()); |
| 1507 | } |
| 1508 | |
| 1509 | /** |
| 1510 | * toArray(incompatible array type) throws ArrayStoreException |
| 1511 | */ |
| 1512 | public void testToArray1_BadArg() { |
| 1513 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1514 | try { |
| 1515 | q.toArray(new String[10]); |
| 1516 | shouldThrow(); |
| 1517 | } catch (ArrayStoreException success) {} |
| 1518 | } |
| 1519 | |
| 1520 | /** |
| 1521 | * iterator iterates through all elements |
| 1522 | */ |
| 1523 | public void testIterator() throws InterruptedException { |
| 1524 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1525 | Iterator it = q.iterator(); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 1526 | int i; |
| 1527 | for (i = 0; it.hasNext(); i++) |
| 1528 | assertTrue(q.contains(it.next())); |
| 1529 | assertEquals(i, SIZE); |
| 1530 | assertIteratorExhausted(it); |
| 1531 | |
| 1532 | it = q.iterator(); |
| 1533 | for (i = 0; it.hasNext(); i++) |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 1534 | assertEquals(it.next(), q.take()); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 1535 | assertEquals(i, SIZE); |
| 1536 | assertIteratorExhausted(it); |
| 1537 | } |
| 1538 | |
| 1539 | /** |
| 1540 | * iterator of empty collection has no elements |
| 1541 | */ |
| 1542 | public void testEmptyIterator() { |
| 1543 | Deque c = new LinkedBlockingDeque(); |
| 1544 | assertIteratorExhausted(c.iterator()); |
| 1545 | assertIteratorExhausted(c.descendingIterator()); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | /** |
| 1549 | * iterator.remove removes current element |
| 1550 | */ |
| 1551 | public void testIteratorRemove() { |
| 1552 | final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
| 1553 | q.add(two); |
| 1554 | q.add(one); |
| 1555 | q.add(three); |
| 1556 | |
| 1557 | Iterator it = q.iterator(); |
| 1558 | it.next(); |
| 1559 | it.remove(); |
| 1560 | |
| 1561 | it = q.iterator(); |
| 1562 | assertSame(it.next(), one); |
| 1563 | assertSame(it.next(), three); |
| 1564 | assertFalse(it.hasNext()); |
| 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * iterator ordering is FIFO |
| 1569 | */ |
| 1570 | public void testIteratorOrdering() { |
| 1571 | final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
| 1572 | q.add(one); |
| 1573 | q.add(two); |
| 1574 | q.add(three); |
| 1575 | assertEquals(0, q.remainingCapacity()); |
| 1576 | int k = 0; |
| 1577 | for (Iterator it = q.iterator(); it.hasNext();) { |
| 1578 | assertEquals(++k, it.next()); |
| 1579 | } |
| 1580 | assertEquals(3, k); |
| 1581 | } |
| 1582 | |
| 1583 | /** |
| 1584 | * Modifications do not cause iterators to fail |
| 1585 | */ |
| 1586 | public void testWeaklyConsistentIteration() { |
| 1587 | final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
| 1588 | q.add(one); |
| 1589 | q.add(two); |
| 1590 | q.add(three); |
| 1591 | for (Iterator it = q.iterator(); it.hasNext();) { |
| 1592 | q.remove(); |
| 1593 | it.next(); |
| 1594 | } |
| 1595 | assertEquals(0, q.size()); |
| 1596 | } |
| 1597 | |
| 1598 | /** |
| 1599 | * Descending iterator iterates through all elements |
| 1600 | */ |
| 1601 | public void testDescendingIterator() { |
| 1602 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1603 | int i = 0; |
| 1604 | Iterator it = q.descendingIterator(); |
| 1605 | while (it.hasNext()) { |
| 1606 | assertTrue(q.contains(it.next())); |
| 1607 | ++i; |
| 1608 | } |
| 1609 | assertEquals(i, SIZE); |
| 1610 | assertFalse(it.hasNext()); |
| 1611 | try { |
| 1612 | it.next(); |
| 1613 | shouldThrow(); |
| 1614 | } catch (NoSuchElementException success) {} |
| 1615 | } |
| 1616 | |
| 1617 | /** |
| 1618 | * Descending iterator ordering is reverse FIFO |
| 1619 | */ |
| 1620 | public void testDescendingIteratorOrdering() { |
| 1621 | final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
| 1622 | for (int iters = 0; iters < 100; ++iters) { |
| 1623 | q.add(new Integer(3)); |
| 1624 | q.add(new Integer(2)); |
| 1625 | q.add(new Integer(1)); |
| 1626 | int k = 0; |
| 1627 | for (Iterator it = q.descendingIterator(); it.hasNext();) { |
| 1628 | assertEquals(++k, it.next()); |
| 1629 | } |
| 1630 | |
| 1631 | assertEquals(3, k); |
| 1632 | q.remove(); |
| 1633 | q.remove(); |
| 1634 | q.remove(); |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | /** |
| 1639 | * descendingIterator.remove removes current element |
| 1640 | */ |
| 1641 | public void testDescendingIteratorRemove() { |
| 1642 | final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
| 1643 | for (int iters = 0; iters < 100; ++iters) { |
| 1644 | q.add(new Integer(3)); |
| 1645 | q.add(new Integer(2)); |
| 1646 | q.add(new Integer(1)); |
| 1647 | Iterator it = q.descendingIterator(); |
| 1648 | assertEquals(it.next(), new Integer(1)); |
| 1649 | it.remove(); |
| 1650 | assertEquals(it.next(), new Integer(2)); |
| 1651 | it = q.descendingIterator(); |
| 1652 | assertEquals(it.next(), new Integer(2)); |
| 1653 | assertEquals(it.next(), new Integer(3)); |
| 1654 | it.remove(); |
| 1655 | assertFalse(it.hasNext()); |
| 1656 | q.remove(); |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | /** |
| 1661 | * toString contains toStrings of elements |
| 1662 | */ |
| 1663 | public void testToString() { |
| 1664 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1665 | String s = q.toString(); |
| 1666 | for (int i = 0; i < SIZE; ++i) { |
| 1667 | assertTrue(s.contains(String.valueOf(i))); |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | /** |
| 1672 | * offer transfers elements across Executor tasks |
| 1673 | */ |
| 1674 | public void testOfferInExecutor() { |
| 1675 | final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
| 1676 | q.add(one); |
| 1677 | q.add(two); |
| 1678 | ExecutorService executor = Executors.newFixedThreadPool(2); |
| 1679 | final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
| 1680 | executor.execute(new CheckedRunnable() { |
| 1681 | public void realRun() throws InterruptedException { |
| 1682 | assertFalse(q.offer(three)); |
| 1683 | threadsStarted.await(); |
| 1684 | assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
| 1685 | assertEquals(0, q.remainingCapacity()); |
| 1686 | }}); |
| 1687 | |
| 1688 | executor.execute(new CheckedRunnable() { |
| 1689 | public void realRun() throws InterruptedException { |
| 1690 | threadsStarted.await(); |
| 1691 | assertSame(one, q.take()); |
| 1692 | }}); |
| 1693 | |
| 1694 | joinPool(executor); |
| 1695 | } |
| 1696 | |
| 1697 | /** |
| 1698 | * timed poll retrieves elements across Executor threads |
| 1699 | */ |
| 1700 | public void testPollInExecutor() { |
| 1701 | final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
| 1702 | final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
| 1703 | ExecutorService executor = Executors.newFixedThreadPool(2); |
| 1704 | executor.execute(new CheckedRunnable() { |
| 1705 | public void realRun() throws InterruptedException { |
| 1706 | assertNull(q.poll()); |
| 1707 | threadsStarted.await(); |
| 1708 | assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
| 1709 | checkEmpty(q); |
| 1710 | }}); |
| 1711 | |
| 1712 | executor.execute(new CheckedRunnable() { |
| 1713 | public void realRun() throws InterruptedException { |
| 1714 | threadsStarted.await(); |
| 1715 | q.put(one); |
| 1716 | }}); |
| 1717 | |
| 1718 | joinPool(executor); |
| 1719 | } |
| 1720 | |
| 1721 | /** |
| 1722 | * A deserialized serialized deque has same elements in same order |
| 1723 | */ |
| 1724 | public void testSerialization() throws Exception { |
| 1725 | Queue x = populatedDeque(SIZE); |
| 1726 | Queue y = serialClone(x); |
| 1727 | |
| 1728 | assertNotSame(y, x); |
| 1729 | assertEquals(x.size(), y.size()); |
| 1730 | assertEquals(x.toString(), y.toString()); |
| 1731 | assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
| 1732 | while (!x.isEmpty()) { |
| 1733 | assertFalse(y.isEmpty()); |
| 1734 | assertEquals(x.remove(), y.remove()); |
| 1735 | } |
| 1736 | assertTrue(y.isEmpty()); |
| 1737 | } |
| 1738 | |
| 1739 | /** |
| 1740 | * drainTo(c) empties deque into another collection c |
| 1741 | */ |
| 1742 | public void testDrainTo() { |
| 1743 | LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1744 | ArrayList l = new ArrayList(); |
| 1745 | q.drainTo(l); |
| 1746 | assertEquals(0, q.size()); |
| 1747 | assertEquals(SIZE, l.size()); |
| 1748 | for (int i = 0; i < SIZE; ++i) |
| 1749 | assertEquals(l.get(i), new Integer(i)); |
| 1750 | q.add(zero); |
| 1751 | q.add(one); |
| 1752 | assertFalse(q.isEmpty()); |
| 1753 | assertTrue(q.contains(zero)); |
| 1754 | assertTrue(q.contains(one)); |
| 1755 | l.clear(); |
| 1756 | q.drainTo(l); |
| 1757 | assertEquals(0, q.size()); |
| 1758 | assertEquals(2, l.size()); |
| 1759 | for (int i = 0; i < 2; ++i) |
| 1760 | assertEquals(l.get(i), new Integer(i)); |
| 1761 | } |
| 1762 | |
| 1763 | /** |
| 1764 | * drainTo empties full deque, unblocking a waiting put. |
| 1765 | */ |
| 1766 | public void testDrainToWithActivePut() throws InterruptedException { |
| 1767 | final LinkedBlockingDeque q = populatedDeque(SIZE); |
| 1768 | Thread t = new Thread(new CheckedRunnable() { |
| 1769 | public void realRun() throws InterruptedException { |
| 1770 | q.put(new Integer(SIZE+1)); |
| 1771 | }}); |
| 1772 | |
| 1773 | t.start(); |
| 1774 | ArrayList l = new ArrayList(); |
| 1775 | q.drainTo(l); |
| 1776 | assertTrue(l.size() >= SIZE); |
| 1777 | for (int i = 0; i < SIZE; ++i) |
| 1778 | assertEquals(l.get(i), new Integer(i)); |
| 1779 | t.join(); |
| 1780 | assertTrue(q.size() + l.size() >= SIZE); |
| 1781 | } |
| 1782 | |
| 1783 | /** |
| 1784 | * drainTo(c, n) empties first min(n, size) elements of queue into c |
| 1785 | */ |
| 1786 | public void testDrainToN() { |
| 1787 | LinkedBlockingDeque q = new LinkedBlockingDeque(); |
| 1788 | for (int i = 0; i < SIZE + 2; ++i) { |
| 1789 | for (int j = 0; j < SIZE; j++) |
| 1790 | assertTrue(q.offer(new Integer(j))); |
| 1791 | ArrayList l = new ArrayList(); |
| 1792 | q.drainTo(l, i); |
| 1793 | int k = (i < SIZE) ? i : SIZE; |
| 1794 | assertEquals(k, l.size()); |
| 1795 | assertEquals(SIZE-k, q.size()); |
| 1796 | for (int j = 0; j < k; ++j) |
| 1797 | assertEquals(l.get(j), new Integer(j)); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 1798 | do {} while (q.poll() != null); |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | /** |
| 1803 | * remove(null), contains(null) always return false |
| 1804 | */ |
| 1805 | public void testNeverContainsNull() { |
| 1806 | Deque<?>[] qs = { |
| 1807 | new LinkedBlockingDeque<Object>(), |
| 1808 | populatedDeque(2), |
| 1809 | }; |
| 1810 | |
| 1811 | for (Deque<?> q : qs) { |
| 1812 | assertFalse(q.contains(null)); |
| 1813 | assertFalse(q.remove(null)); |
| 1814 | assertFalse(q.removeFirstOccurrence(null)); |
| 1815 | assertFalse(q.removeLastOccurrence(null)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | } |