1 4 package com.tctest; 5 6 import com.tc.exception.TCRuntimeException; 7 import com.tc.object.config.ConfigVisitor; 8 import com.tc.object.config.DSOClientConfigHelper; 9 import com.tc.object.config.TransparencyClassSpec; 10 import com.tc.object.tx.ReadOnlyException; 11 import com.tc.simulator.app.ApplicationConfig; 12 import com.tc.simulator.listener.ListenerProvider; 13 import com.tc.util.Assert; 14 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.Collection ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.concurrent.CyclicBarrier ; 21 import java.util.concurrent.LinkedBlockingQueue ; 22 import java.util.concurrent.TimeUnit ; 23 24 public class GenericQueueTestApp extends GenericTestApp { 25 private final CyclicBarrier localBarrier = new CyclicBarrier (2); 26 27 public GenericQueueTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 28 super(appId, cfg, listenerProvider, LinkedBlockingQueue .class); 29 } 30 31 protected Object getTestObject(String testName) { 32 List lists = (List ) sharedMap.get("queues"); 33 return lists.iterator(); 34 } 35 36 protected void setupTestObject(String testName) { 37 List lists = new ArrayList (); 38 lists.add(new LinkedBlockingQueue ()); 39 lists.add(new LinkedBlockingQueue (5)); 40 41 sharedMap.put("queues", lists); 42 sharedMap.put("arrayForBlockingQueue", new Object [4]); 43 sharedMap.put("arrayForBlockingQueue2", new Object [4]); 44 sharedMap.put("collectionForBlockingQueue", new ArrayList ()); 45 sharedMap.put("collectionForBlockingQueue2", new ArrayList ()); 46 } 47 48 void testBasicAdd(LinkedBlockingQueue queue, boolean validate) { 49 if (validate) { 50 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Second element" }), queue); 51 } else { 52 queue.add("First element"); 53 queue.add("Second element"); 54 } 55 } 56 57 void testAddAll(LinkedBlockingQueue queue, boolean validate) { 58 List toAdd = new ArrayList (); 59 toAdd.add("First element"); 60 toAdd.add("Second element"); 61 if (validate) { 62 assertQueueEqual(toAdd, queue); 63 } else { 64 queue.addAll(toAdd); 65 } 66 } 67 68 void testElement(LinkedBlockingQueue queue, boolean validate) { 69 List toAdd = new ArrayList (); 70 toAdd.add("First element"); 71 toAdd.add("Second element"); 72 if (validate) { 73 Assert.assertEquals(toAdd.get(0), queue.element()); 74 } else { 75 queue.addAll(toAdd); 76 } 77 } 78 79 void testContains(LinkedBlockingQueue queue, boolean validate) { 80 List toAdd = new ArrayList (); 81 toAdd.add("First element"); 82 toAdd.add("Second element"); 83 if (validate) { 84 queue.contains(toAdd.get(1)); 85 } else { 86 queue.addAll(toAdd); 87 } 88 } 89 90 void testContainsAll(LinkedBlockingQueue queue, boolean validate) { 91 List toAdd = new ArrayList (); 92 toAdd.add("First element"); 93 toAdd.add("Second element"); 94 if (validate) { 95 Assert.assertFalse(queue.isEmpty()); 96 queue.containsAll(toAdd); 97 } else { 98 Assert.assertTrue(queue.isEmpty()); 99 queue.addAll(toAdd); 100 Assert.assertFalse(queue.isEmpty()); 101 } 102 } 103 104 void testBasicPut(LinkedBlockingQueue queue, boolean validate) { 105 if (validate) { 106 assertSingleElement("First element", queue); 107 } else { 108 try { 109 queue.put("First element"); 110 } catch (InterruptedException e) { 111 throw new TCRuntimeException(e); 112 } 113 } 114 } 115 116 void testPut(LinkedBlockingQueue queue, boolean validate) { 117 if (validate) { 118 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Second element" }), queue); 119 } else { 120 try { 121 queue.put("First element"); 122 queue.put("Second element"); 123 } catch (InterruptedException e) { 124 throw new TCRuntimeException(e); 125 } 126 } 127 } 128 129 void testClear(LinkedBlockingQueue queue, boolean validate) { 130 if (validate) { 131 assertEmptyQueue(queue); 132 } else { 133 try { 134 queue.put("First element"); 135 queue.put("Second element"); 136 } catch (InterruptedException e) { 137 throw new TCRuntimeException(e); 138 } 139 queue.clear(); 140 } 141 } 142 143 void testDrainTo1(LinkedBlockingQueue queue, boolean validate) { 144 if (validate) { 145 assertEmptyQueue(queue); 146 } else { 147 try { 148 queue.put("First element"); 149 queue.put("Second element"); 150 } catch (InterruptedException e) { 151 throw new TCRuntimeException(e); 152 } 153 List list = new ArrayList (); 154 queue.drainTo(list); 155 assertCollectionEqual(Arrays.asList(new Object [] { "First element", "Second element" }), list); 156 } 157 } 158 159 void testDrainToWithSharedCollection1(LinkedBlockingQueue queue, boolean validate) { 160 Collection collection = getCollection(queue); 161 if (validate) { 162 assertEmptyQueue(queue); 163 assertCollectionEqual(Arrays.asList(new Object [] { "First element", "Second element" }), collection); 164 } else { 165 try { 166 queue.put("First element"); 167 queue.put("Second element"); 168 } catch (InterruptedException e) { 169 throw new TCRuntimeException(e); 170 } 171 synchronized (collection) { 172 queue.drainTo(collection); 173 } 174 } 175 } 176 177 void testDrainTo2(LinkedBlockingQueue queue, boolean validate) { 178 if (validate) { 179 assertQueueEqual(Arrays.asList(new Object [] { "Third element", "Fourth element" }), queue); 180 } else { 181 try { 182 queue.put("First element"); 183 queue.put("Second element"); 184 queue.put("Third element"); 185 queue.put("Fourth element"); 186 } catch (InterruptedException e) { 187 throw new TCRuntimeException(e); 188 } 189 List list = new ArrayList (); 190 queue.drainTo(list, 2); 191 assertCollectionEqual(Arrays.asList(new Object [] { "First element", "Second element" }), list); 192 } 193 } 194 195 void testDrainToWithSharedCollection2(LinkedBlockingQueue queue, boolean validate) { 196 Collection collection = getCollection(queue); 197 if (validate) { 198 assertQueueEqual(Arrays.asList(new Object [] { "Third element", "Fourth element" }), queue); 199 assertCollectionEqual(Arrays.asList(new Object [] { "First element", "Second element" }), collection); 200 } else { 201 try { 202 queue.put("First element"); 203 queue.put("Second element"); 204 queue.put("Third element"); 205 queue.put("Fourth element"); 206 } catch (InterruptedException e) { 207 throw new TCRuntimeException(e); 208 } 209 synchronized (collection) { 210 queue.drainTo(collection, 2); 211 } 212 } 213 } 214 215 void testOffer(LinkedBlockingQueue queue, boolean validate) { 216 if (validate) { 217 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Second element", "Third element", 218 "Fourth element", "Fifth element" }), queue); 219 } else { 220 try { 221 queue.put("First element"); 222 queue.put("Second element"); 223 queue.put("Third element"); 224 queue.put("Fourth element"); 225 } catch (InterruptedException e) { 226 throw new TCRuntimeException(e); 227 } 228 229 queue.offer("Fifth element"); 230 } 231 } 232 233 void testOfferFull(LinkedBlockingQueue queue, boolean validate) { 234 if (validate) { 235 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Second element", "Third element", 236 "Fourth element", "Fifth element" }), queue); 237 } else { 238 try { 239 queue.put("First element"); 240 queue.put("Second element"); 241 queue.put("Third element"); 242 queue.put("Fourth element"); 243 queue.put("Fifth element"); 244 } catch (InterruptedException e) { 245 throw new TCRuntimeException(e); 246 } 247 248 if (queue.remainingCapacity() == 0) { 249 queue.offer("Sixth element"); 250 } 251 } 252 } 253 254 void testOfferTimeout1(LinkedBlockingQueue queue, boolean validate) { 255 if (validate) { 256 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Second element", "Third element", 257 "Fourth element", "Fifth element" }), queue); 258 } else { 259 try { 260 queue.put("First element"); 261 queue.put("Second element"); 262 queue.put("Third element"); 263 queue.put("Fourth element"); 264 queue.put("Fifth element"); 265 } catch (InterruptedException e) { 266 throw new TCRuntimeException(e); 267 } 268 269 if (queue.remainingCapacity() == 0) { 270 try { 271 queue.offer("Sixth element", 10, TimeUnit.MILLISECONDS); 272 } catch (InterruptedException e) { 273 throw new TCRuntimeException(e); 274 } 275 } 276 } 277 } 278 279 void testOfferTimeout2(LinkedBlockingQueue queue, boolean validate) { 280 if (validate) { 281 assertQueueEqual(Arrays.asList(new Object [] { "Second element", "Third element", "Fourth element", 282 "Fifth element", "Sixth element" }), queue); 283 } else { 284 try { 285 queue.put("First element"); 286 queue.put("Second element"); 287 queue.put("Third element"); 288 queue.put("Fourth element"); 289 queue.put("Fifth element"); 290 } catch (InterruptedException e) { 291 throw new TCRuntimeException(e); 292 } 293 294 Thread thread = new Thread (new QueueReader(queue, localBarrier)); 295 thread.start(); 296 try { 297 queue.offer("Sixth element", 100, TimeUnit.SECONDS); 298 localBarrier.await(); 299 } catch (Exception e) { 300 throw new TCRuntimeException(e); 301 } 302 } 303 } 304 305 void testPeek(LinkedBlockingQueue queue, boolean validate) { 306 if (validate) { 307 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Second element", "Third element", 308 "Fourth element", "Fifth element" }), queue); 309 } else { 310 try { 311 queue.put("First element"); 312 queue.put("Second element"); 313 queue.put("Third element"); 314 queue.put("Fourth element"); 315 queue.put("Fifth element"); 316 } catch (InterruptedException e) { 317 throw new TCRuntimeException(e); 318 } 319 320 Assert.assertEquals("First element", queue.peek()); 321 } 322 } 323 324 void testPoll(LinkedBlockingQueue queue, boolean validate) { 325 if (validate) { 326 assertQueueEqual(Arrays.asList(new Object [] { "Second element", "Third element", "Fourth element", 327 "Fifth element" }), queue); 328 } else { 329 try { 330 queue.put("First element"); 331 queue.put("Second element"); 332 queue.put("Third element"); 333 queue.put("Fourth element"); 334 queue.put("Fifth element"); 335 } catch (InterruptedException e) { 336 throw new TCRuntimeException(e); 337 } 338 339 Assert.assertEquals("First element", queue.poll()); 340 } 341 } 342 343 void testPollTimeout1(LinkedBlockingQueue queue, boolean validate) { 344 if (validate) { 345 assertQueueEqual(Arrays.asList(new Object [] { "Second element", "Third element", "Fourth element", 346 "Fifth element" }), queue); 347 } else { 348 try { 349 Assert.assertEquals(null, queue.poll(10, TimeUnit.MILLISECONDS)); 350 } catch (InterruptedException e) { 351 throw new TCRuntimeException(e); 352 } 353 try { 354 queue.put("First element"); 355 queue.put("Second element"); 356 queue.put("Third element"); 357 queue.put("Fourth element"); 358 queue.put("Fifth element"); 359 } catch (InterruptedException e) { 360 throw new TCRuntimeException(e); 361 } 362 363 Assert.assertEquals("First element", queue.poll()); 364 } 365 } 366 367 void testPollTimeout2(LinkedBlockingQueue queue, boolean validate) { 368 if (validate) { 369 assertEmptyQueue(queue); 370 } else { 371 Thread thread = new Thread (new QueuePutter(queue, localBarrier)); 372 thread.start(); 373 try { 374 localBarrier.await(); 375 Assert.assertEquals("New element", queue.poll(100, TimeUnit.SECONDS)); 376 } catch (Exception e) { 377 throw new TCRuntimeException(e); 378 } 379 } 380 } 381 382 void testTake(LinkedBlockingQueue queue, boolean validate) { 383 if (!validate) { 384 Thread thread = new Thread (new QueuePutter(queue, localBarrier)); 385 thread.start(); 386 387 Object queueElement = null; 388 try { 389 localBarrier.await(); 390 queueElement = queue.take(); 391 } catch (Exception e) { 392 throw new TCRuntimeException(e); 393 } 394 395 Assert.assertEquals("New element", queueElement); 396 } 397 } 398 399 void testRemove1(LinkedBlockingQueue queue, boolean validate) { 400 if (validate) { 401 assertQueueEqual(Arrays.asList(new Object [] { "Second element", "Third element", "Fourth element" }), queue); 402 } else { 403 try { 404 queue.put("First element"); 405 queue.put("Second element"); 406 queue.put("Third element"); 407 queue.put("Fourth element"); 408 } catch (InterruptedException e) { 409 throw new TCRuntimeException(e); 410 } 411 412 queue.remove(); 413 } 414 } 415 416 void testRemove2(LinkedBlockingQueue queue, boolean validate) { 417 if (validate) { 418 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Second element", "Fourth element" }), queue); 419 } else { 420 try { 421 queue.put("First element"); 422 queue.put("Second element"); 423 queue.put("Third element"); 424 queue.put("Fourth element"); 425 } catch (InterruptedException e) { 426 throw new TCRuntimeException(e); 427 } 428 429 queue.remove("Third element"); 430 } 431 } 432 433 void testRemove3(LinkedBlockingQueue queue, boolean validate) { 434 if (validate) { 435 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Second element", "Third element", 436 "Fourth element" }), queue); 437 } else { 438 try { 439 queue.put("First element"); 440 queue.put("Second element"); 441 queue.put("Third element"); 442 queue.put("Fourth element"); 443 } catch (InterruptedException e) { 444 throw new TCRuntimeException(e); 445 } 446 447 queue.remove("Fifth element"); 448 } 449 } 450 451 void testRemoveAll(LinkedBlockingQueue queue, boolean validate) { 452 if (validate) { 453 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Fourth element" }), queue); 454 } else { 455 try { 456 queue.put("First element"); 457 queue.put("Second element"); 458 queue.put("Third element"); 459 queue.put("Fourth element"); 460 } catch (InterruptedException e) { 461 throw new TCRuntimeException(e); 462 } 463 464 List toRemove = new ArrayList (); 465 toRemove.add("Second element"); 466 toRemove.add("Third element"); 467 queue.removeAll(toRemove); 468 } 469 } 470 471 void testToArray1(LinkedBlockingQueue queue, boolean validate) { 472 if (validate) { 473 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Second element", "Third element", 474 "Fourth element" }), queue); 475 } else { 476 try { 477 queue.put("First element"); 478 queue.put("Second element"); 479 queue.put("Third element"); 480 queue.put("Fourth element"); 481 } catch (InterruptedException e) { 482 throw new TCRuntimeException(e); 483 } 484 485 Object [] array = queue.toArray(); 486 assertQueueEqual(Arrays.asList(array), queue); 487 } 488 } 489 490 void testToArray2(LinkedBlockingQueue queue, boolean validate) { 491 if (validate) { 492 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Second element", "Third element", 493 "Fourth element" }), queue); 494 } else { 495 try { 496 queue.put("First element"); 497 queue.put("Second element"); 498 queue.put("Third element"); 499 queue.put("Fourth element"); 500 } catch (InterruptedException e) { 501 throw new TCRuntimeException(e); 502 } 503 504 Object [] array = new Object [queue.size()]; 505 Object [] array2 = queue.toArray(array); 506 Assert.assertTrue(array == array2); 507 assertQueueEqual(Arrays.asList(array), queue); 508 } 509 } 510 511 void testToArrayWithSharedArray(LinkedBlockingQueue queue, boolean validate) { 512 Object [] array = getArray(queue); 513 if (validate) { 514 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Second element", "Third element", 515 "Fourth element" }), queue); 516 assertQueueEqual(Arrays.asList(array), queue); 517 } else { 518 try { 519 queue.put("First element"); 520 queue.put("Second element"); 521 queue.put("Third element"); 522 queue.put("Fourth element"); 523 } catch (InterruptedException e) { 524 throw new TCRuntimeException(e); 525 } 526 527 Object [] array2 = null; 530 array2 = queue.toArray(array); 531 Assert.assertTrue(array == array2); 532 } 533 } 534 535 void testReadOnlyDrainTo(LinkedBlockingQueue queue, boolean validate) { 537 Collection collection = getCollection(queue); 538 if (validate) { 539 assertEmptyQueue(queue); 540 Assert.assertEquals(0, collection.size()); 541 } else { 542 try { 543 queue.put("First element"); 544 queue.put("Second element"); 545 } catch (InterruptedException e) { 546 throw new TCRuntimeException(e); 547 } 548 synchronized (collection) { 549 try { 550 queue.drainTo(collection); 551 throw new AssertionError ("Should have thrown a ReadOnlyException"); 552 } catch (ReadOnlyException t) { 553 } 555 } 556 } 557 } 558 559 void testIteratorRemove(LinkedBlockingQueue queue, boolean validate) { 560 if (validate) { 561 assertQueueEqual(Arrays.asList(new Object [] { "First element", "Third element" }), queue); 562 } else { 563 try { 564 queue.put("First element"); 565 queue.put("Second element"); 566 queue.put("Third element"); 567 } catch (InterruptedException e) { 568 throw new TCRuntimeException(e); 569 } 570 Iterator itr = queue.iterator(); 571 itr.next(); 572 itr.next(); 573 synchronized (itr) { 574 itr.remove(); 575 } 576 } 577 } 578 579 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 580 String testClass = GenericQueueTestApp.class.getName(); 581 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 582 spec.addRoot("localBarrier", "localBarrier"); 583 String writeAllowedMethodExpression = "* " + testClass + "*.*(..)"; 584 config.addWriteAutolock(writeAllowedMethodExpression); 585 String readOnlyMethodExpression = "* " + testClass + "*.*ReadOnly*(..)"; 586 config.addReadAutolock(readOnlyMethodExpression); 587 } 588 589 private Collection getCollection(LinkedBlockingQueue queue) { 590 if (queue.remainingCapacity() == 5) { 591 return (Collection ) sharedMap.get("collectionForBlockingQueue2"); 592 } else { 593 return (Collection ) sharedMap.get("collectionForBlockingQueue"); 594 } 595 } 596 597 private Object [] getArray(LinkedBlockingQueue queue) { 598 if (queue.remainingCapacity() == 5) { 599 return (Object []) sharedMap.get("arrayForBlockingQueue2"); 600 } else { 601 return (Object []) sharedMap.get("arrayForBlockingQueue"); 602 } 603 } 604 605 private void assertEmptyQueue(LinkedBlockingQueue queue) { 606 Assert.assertEquals(0, queue.size()); 607 } 608 609 private void assertSingleElement(Object expected, LinkedBlockingQueue queue) { 610 Assert.assertEquals(1, queue.size()); 611 Assert.assertEquals(expected, queue.peek()); 612 } 613 614 private void assertCollectionEqual(Collection expect, Collection actual) { 615 Assert.assertEquals(expect.size(), actual.size()); 616 617 Assert.assertTrue(expect.containsAll(actual)); 618 Assert.assertTrue(actual.containsAll(expect)); 619 } 620 621 private void assertQueueEqual(List expect, LinkedBlockingQueue actual) { 622 Assert.assertEquals(expect.size(), actual.size()); 623 624 Assert.assertTrue(expect.containsAll(actual)); 625 Assert.assertTrue(actual.containsAll(expect)); 626 627 for (Iterator iExpect = expect.iterator(), iActual = actual.iterator(); iExpect.hasNext();) { 628 Assert.assertEquals(iExpect.next(), iActual.next()); 629 } 630 631 if (expect.isEmpty()) { 632 Assert.assertTrue(actual.isEmpty()); 633 } else { 634 Assert.assertFalse(actual.isEmpty()); 635 } 636 } 637 638 private static class QueuePutter implements Runnable { 639 private LinkedBlockingQueue queue; 640 private CyclicBarrier barrier; 641 642 public QueuePutter(LinkedBlockingQueue queue, CyclicBarrier barrier) { 643 this.queue = queue; 644 this.barrier = barrier; 645 } 646 647 public void run() { 648 try { 649 queue.put("New element"); 650 barrier.await(); 651 } catch (Exception e) { 652 throw new TCRuntimeException(e); 653 } 654 } 655 } 656 657 private static class QueueReader implements Runnable { 658 private LinkedBlockingQueue queue; 659 private CyclicBarrier barrier; 660 661 public QueueReader(LinkedBlockingQueue queue, CyclicBarrier barrier) { 662 this.queue = queue; 663 this.barrier = barrier; 664 } 665 666 public void run() { 667 queue.poll(); 668 try { 669 barrier.await(); 670 } catch (Exception e) { 671 throw new TCRuntimeException(e); 672 } 673 } 674 } 675 } 676
| Popular Tags
|