|
55 | 55 | import static org.junit.Assert.assertFalse; |
56 | 56 | import static org.junit.Assert.assertNull; |
57 | 57 | import static org.junit.Assert.assertSame; |
| 58 | +import static org.junit.Assert.assertThrows; |
58 | 59 | import static org.junit.Assert.assertTrue; |
59 | 60 | import static org.junit.Assert.fail; |
60 | 61 |
|
@@ -425,6 +426,119 @@ public void testDefaultMappingCollectRetainsSequentialConsumption() throws Excep |
425 | 426 | collect.close(); |
426 | 427 | } |
427 | 428 |
|
| 429 | + @Test |
| 430 | + public void testLegacyInitializesOtherInputsOnceAndFinishesAtLastChild() throws Exception { |
| 431 | + Child a = new Child(block(1)); |
| 432 | + Child b = new Child(block(2)); |
| 433 | + b.ready(); |
| 434 | + CollectOperator collect = |
| 435 | + new CollectOperator(new TestContext(), new ArrayList<>(Arrays.asList(a, b))); |
| 436 | + for (int i = 0; i < 10; i++) { |
| 437 | + assertFalse(collect.isBlocked().isDone()); |
| 438 | + } |
| 439 | + assertEquals(11, a.waitCalls); |
| 440 | + assertEquals(1, b.waitCalls); |
| 441 | + assertEquals(0, b.advances); |
| 442 | + a.ready(); |
| 443 | + assertSame(a.output, collect.next()); |
| 444 | + a.hasNext = false; |
| 445 | + assertNull(collect.next()); |
| 446 | + assertTrue(collect.hasNext()); |
| 447 | + assertFalse(collect.isFinished()); |
| 448 | + for (int i = 0; i < 10; i++) { |
| 449 | + assertTrue(collect.isBlocked().isDone()); |
| 450 | + } |
| 451 | + assertEquals(11, a.waitCalls); |
| 452 | + assertEquals(11, b.waitCalls); |
| 453 | + assertSame(b.output, collect.next()); |
| 454 | + b.hasNext = false; |
| 455 | + assertNull(collect.next()); |
| 456 | + assertFalse(collect.hasNext()); |
| 457 | + assertTrue(collect.isFinished()); |
| 458 | + assertTrue(collect.isBlocked().isDone()); |
| 459 | + collect.close(); |
| 460 | + assertEquals(1, a.closes); |
| 461 | + assertEquals(1, b.closes); |
| 462 | + } |
| 463 | + |
| 464 | + @Test |
| 465 | + public void testLegacyMemoryUsesLargestChildPeak() throws Exception { |
| 466 | + Child a = new Child(block(1)); |
| 467 | + Child b = new Child(block(2)); |
| 468 | + a.peak = 100; |
| 469 | + a.retained = 30; |
| 470 | + a.returnSize = 10; |
| 471 | + b.peak = 80; |
| 472 | + b.retained = 50; |
| 473 | + b.returnSize = 20; |
| 474 | + CollectOperator collect = |
| 475 | + new CollectOperator(new TestContext(), new ArrayList<>(Arrays.asList(a, b))); |
| 476 | + assertEquals(100, collect.calculateMaxPeekMemory()); |
| 477 | + assertEquals(0, collect.calculateRetainedSizeAfterCallingNext()); |
| 478 | + assertEquals(20, collect.calculateMaxReturnSize()); |
| 479 | + collect.close(); |
| 480 | + } |
| 481 | + |
| 482 | + @Test |
| 483 | + public void testCannotEnableReadyConsumptionAfterLegacyInputIsRetired() throws Exception { |
| 484 | + Child a = new Child(block(1)); |
| 485 | + Child b = new Child(block(2)); |
| 486 | + a.hasNext = false; |
| 487 | + b.ready(); |
| 488 | + CollectOperator collect = |
| 489 | + new CollectOperator(new TestContext(), new ArrayList<>(Arrays.asList(a, b))); |
| 490 | + assertNull(collect.next()); |
| 491 | + assertThrows(IllegalStateException.class, collect::enableReadyInputConsumption); |
| 492 | + assertSame(b.output, collect.next()); |
| 493 | + b.hasNext = false; |
| 494 | + assertNull(collect.next()); |
| 495 | + assertTrue(collect.isFinished()); |
| 496 | + } |
| 497 | + |
| 498 | + @Test |
| 499 | + public void testCannotEnableReadyConsumptionAfterLegacyReadinessObservation() throws Exception { |
| 500 | + Child child = new Child(block(1)); |
| 501 | + CollectOperator collect = |
| 502 | + new CollectOperator(new TestContext(), new ArrayList<>(Collections.singletonList(child))); |
| 503 | + assertFalse(collect.isBlocked().isDone()); |
| 504 | + assertThrows(IllegalStateException.class, collect::enableReadyInputConsumption); |
| 505 | + collect.close(); |
| 506 | + } |
| 507 | + |
| 508 | + @Test |
| 509 | + public void testCannotEnableReadyConsumptionAfterLegacyNextWithoutIsBlocked() throws Exception { |
| 510 | + Child child = new Child(block(1)); |
| 511 | + child.ready(); |
| 512 | + CollectOperator collect = |
| 513 | + new CollectOperator(new TestContext(), new ArrayList<>(Collections.singletonList(child))); |
| 514 | + assertSame(child.output, collect.next()); |
| 515 | + assertThrows(IllegalStateException.class, collect::enableReadyInputConsumption); |
| 516 | + collect.close(); |
| 517 | + } |
| 518 | + |
| 519 | + @Test |
| 520 | + public void testCannotEnableReadyConsumptionAfterLegacyClose() throws Exception { |
| 521 | + Child child = new Child(block(1)); |
| 522 | + CollectOperator collect = |
| 523 | + new CollectOperator(new TestContext(), new ArrayList<>(Collections.singletonList(child))); |
| 524 | + collect.close(); |
| 525 | + assertThrows(IllegalStateException.class, collect::enableReadyInputConsumption); |
| 526 | + assertEquals(1, child.closes); |
| 527 | + } |
| 528 | + |
| 529 | + @Test |
| 530 | + public void testEnablingReadyConsumptionIsIdempotent() throws Exception { |
| 531 | + Child child = new Child(block(1)); |
| 532 | + child.ready(); |
| 533 | + CollectOperator collect = collect(child); |
| 534 | + collect.enableReadyInputConsumption(); |
| 535 | + ListenableFuture<?> selection = collect.isBlocked(); |
| 536 | + collect.enableReadyInputConsumption(); |
| 537 | + assertSame(selection, collect.isBlocked()); |
| 538 | + assertSame(child.output, collect.next()); |
| 539 | + collect.close(); |
| 540 | + } |
| 541 | + |
428 | 542 | @Test |
429 | 543 | public void testMappingUsesActualChildIdentity() throws Exception { |
430 | 544 | Child a = new Child(block(10, 11)); |
|
0 commit comments