|
55 | 55 | import static org.hamcrest.Matchers.greaterThan;
|
56 | 56 | import static org.junit.Assert.assertEquals;
|
57 | 57 | import static org.junit.Assert.assertFalse;
|
| 58 | +import static org.junit.Assert.assertNotNull; |
| 59 | +import static org.junit.Assert.assertNull; |
58 | 60 | import static org.junit.Assert.assertThat;
|
59 | 61 | import static org.junit.Assert.assertTrue;
|
60 | 62 | import static org.junit.Assert.fail;
|
@@ -544,28 +546,297 @@ public void writeTxRetriedUntilFailure()
|
544 | 546 | }
|
545 | 547 |
|
546 | 548 | @Test
|
547 |
| - public void writeTxDoesNotCommitWhenMarkedForFailure() |
| 549 | + public void readTxCommittedWithoutTxSuccess() |
548 | 550 | {
|
549 |
| - try ( Session session = neo4j.driver().session() ) |
| 551 | + try ( Driver driver = newDriverWithoutRetries(); |
| 552 | + Session session = driver.session() ) |
550 | 553 | {
|
551 |
| - int answer = session.writeTransaction( new TransactionWork<Integer>() |
| 554 | + assertNull( session.lastBookmark() ); |
| 555 | + |
| 556 | + long answer = session.readTransaction( new TransactionWork<Long>() |
| 557 | + { |
| 558 | + @Override |
| 559 | + public Long execute( Transaction tx ) |
| 560 | + { |
| 561 | + return tx.run( "RETURN 42" ).single().get( 0 ).asLong(); |
| 562 | + } |
| 563 | + } ); |
| 564 | + assertEquals( 42, answer ); |
| 565 | + |
| 566 | + // bookmark should be not-null after commit |
| 567 | + assertNotNull( session.lastBookmark() ); |
| 568 | + } |
| 569 | + } |
| 570 | + |
| 571 | + @Test |
| 572 | + public void writeTxCommittedWithoutTxSuccess() |
| 573 | + { |
| 574 | + try ( Driver driver = newDriverWithoutRetries() ) |
| 575 | + { |
| 576 | + try ( Session session = driver.session() ) |
| 577 | + { |
| 578 | + long answer = session.writeTransaction( new TransactionWork<Long>() |
| 579 | + { |
| 580 | + @Override |
| 581 | + public Long execute( Transaction tx ) |
| 582 | + { |
| 583 | + return tx.run( "CREATE (:Person {name: 'Thor Odinson'}) RETURN 42" ).single().get( 0 ).asLong(); |
| 584 | + } |
| 585 | + } ); |
| 586 | + assertEquals( 42, answer ); |
| 587 | + } |
| 588 | + |
| 589 | + try ( Session session = driver.session() ) |
| 590 | + { |
| 591 | + StatementResult result = session.run( "MATCH (p:Person {name: 'Thor Odinson'}) RETURN count(p)" ); |
| 592 | + assertEquals( 1, result.single().get( 0 ).asInt() ); |
| 593 | + } |
| 594 | + } |
| 595 | + } |
| 596 | + |
| 597 | + @Test |
| 598 | + public void readTxRolledBackWithTxFailure() |
| 599 | + { |
| 600 | + try ( Driver driver = newDriverWithoutRetries(); |
| 601 | + Session session = driver.session() ) |
| 602 | + { |
| 603 | + assertNull( session.lastBookmark() ); |
| 604 | + |
| 605 | + long answer = session.readTransaction( new TransactionWork<Long>() |
552 | 606 | {
|
553 | 607 | @Override
|
554 |
| - public Integer execute( Transaction tx ) |
| 608 | + public Long execute( Transaction tx ) |
555 | 609 | {
|
556 |
| - tx.run( "CREATE (:Person {name: 'Natasha Romanoff'})" ); |
| 610 | + StatementResult result = tx.run( "RETURN 42" ); |
557 | 611 | tx.failure();
|
558 |
| - return 42; |
| 612 | + return result.single().get( 0 ).asLong(); |
559 | 613 | }
|
560 | 614 | } );
|
| 615 | + assertEquals( 42, answer ); |
| 616 | + |
| 617 | + // bookmark should remain null after rollback |
| 618 | + assertNull( session.lastBookmark() ); |
| 619 | + } |
| 620 | + } |
| 621 | + |
| 622 | + @Test |
| 623 | + public void writeTxRolledBackWithTxFailure() |
| 624 | + { |
| 625 | + try ( Driver driver = newDriverWithoutRetries() ) |
| 626 | + { |
| 627 | + try ( Session session = driver.session() ) |
| 628 | + { |
| 629 | + int answer = session.writeTransaction( new TransactionWork<Integer>() |
| 630 | + { |
| 631 | + @Override |
| 632 | + public Integer execute( Transaction tx ) |
| 633 | + { |
| 634 | + tx.run( "CREATE (:Person {name: 'Natasha Romanoff'})" ); |
| 635 | + tx.failure(); |
| 636 | + return 42; |
| 637 | + } |
| 638 | + } ); |
| 639 | + |
| 640 | + assertEquals( 42, answer ); |
| 641 | + } |
| 642 | + |
| 643 | + try ( Session session = driver.session() ) |
| 644 | + { |
| 645 | + StatementResult result = session.run( "MATCH (p:Person {name: 'Natasha Romanoff'}) RETURN count(p)" ); |
| 646 | + assertEquals( 0, result.single().get( 0 ).asInt() ); |
| 647 | + } |
| 648 | + } |
| 649 | + } |
| 650 | + |
| 651 | + @Test |
| 652 | + public void readTxRolledBackWhenExceptionIsThrown() |
| 653 | + { |
| 654 | + try ( Driver driver = newDriverWithoutRetries(); |
| 655 | + Session session = driver.session() ) |
| 656 | + { |
| 657 | + assertNull( session.lastBookmark() ); |
| 658 | + |
| 659 | + try |
| 660 | + { |
| 661 | + session.readTransaction( new TransactionWork<Long>() |
| 662 | + { |
| 663 | + @Override |
| 664 | + public Long execute( Transaction tx ) |
| 665 | + { |
| 666 | + StatementResult result = tx.run( "RETURN 42" ); |
| 667 | + if ( result.single().get( 0 ).asLong() == 42 ) |
| 668 | + { |
| 669 | + throw new IllegalStateException(); |
| 670 | + } |
| 671 | + return 1L; |
| 672 | + } |
| 673 | + } ); |
| 674 | + fail( "Exception expected" ); |
| 675 | + } |
| 676 | + catch ( Exception e ) |
| 677 | + { |
| 678 | + assertThat( e, instanceOf( IllegalStateException.class ) ); |
| 679 | + } |
| 680 | + |
| 681 | + // bookmark should remain null after rollback |
| 682 | + assertNull( session.lastBookmark() ); |
| 683 | + } |
| 684 | + } |
561 | 685 |
|
| 686 | + @Test |
| 687 | + public void writeTxRolledBackWhenExceptionIsThrown() |
| 688 | + { |
| 689 | + try ( Driver driver = newDriverWithoutRetries() ) |
| 690 | + { |
| 691 | + try ( Session session = driver.session() ) |
| 692 | + { |
| 693 | + try |
| 694 | + { |
| 695 | + session.writeTransaction( new TransactionWork<Integer>() |
| 696 | + { |
| 697 | + @Override |
| 698 | + public Integer execute( Transaction tx ) |
| 699 | + { |
| 700 | + tx.run( "CREATE (:Person {name: 'Loki Odinson'})" ); |
| 701 | + throw new IllegalStateException(); |
| 702 | + } |
| 703 | + } ); |
| 704 | + fail( "Exception expected" ); |
| 705 | + } |
| 706 | + catch ( Exception e ) |
| 707 | + { |
| 708 | + assertThat( e, instanceOf( IllegalStateException.class ) ); |
| 709 | + } |
| 710 | + } |
| 711 | + |
| 712 | + try ( Session session = driver.session() ) |
| 713 | + { |
| 714 | + StatementResult result = session.run( "MATCH (p:Person {name: 'Natasha Romanoff'}) RETURN count(p)" ); |
| 715 | + assertEquals( 0, result.single().get( 0 ).asInt() ); |
| 716 | + } |
| 717 | + } |
| 718 | + } |
| 719 | + |
| 720 | + @Test |
| 721 | + public void readTxRolledBackWhenMarkedBothSuccessAndFailure() |
| 722 | + { |
| 723 | + try ( Driver driver = newDriverWithoutRetries(); |
| 724 | + Session session = driver.session() ) |
| 725 | + { |
| 726 | + assertNull( session.lastBookmark() ); |
| 727 | + |
| 728 | + long answer = session.readTransaction( new TransactionWork<Long>() |
| 729 | + { |
| 730 | + @Override |
| 731 | + public Long execute( Transaction tx ) |
| 732 | + { |
| 733 | + StatementResult result = tx.run( "RETURN 42" ); |
| 734 | + tx.success(); |
| 735 | + tx.failure(); |
| 736 | + return result.single().get( 0 ).asLong(); |
| 737 | + } |
| 738 | + } ); |
562 | 739 | assertEquals( 42, answer );
|
| 740 | + |
| 741 | + // bookmark should remain null after rollback |
| 742 | + assertNull( session.lastBookmark() ); |
563 | 743 | }
|
| 744 | + } |
564 | 745 |
|
565 |
| - try ( Session session = neo4j.driver().session() ) |
| 746 | + @Test |
| 747 | + public void writeTxRolledBackWhenMarkedBothSuccessAndFailure() |
| 748 | + { |
| 749 | + try ( Driver driver = newDriverWithoutRetries() ) |
566 | 750 | {
|
567 |
| - StatementResult result = session.run( "MATCH (p:Person {name: 'Natasha Romanoff'}) RETURN count(p)" ); |
568 |
| - assertEquals( 0, result.single().get( 0 ).asInt() ); |
| 751 | + try ( Session session = driver.session() ) |
| 752 | + { |
| 753 | + int answer = session.writeTransaction( new TransactionWork<Integer>() |
| 754 | + { |
| 755 | + @Override |
| 756 | + public Integer execute( Transaction tx ) |
| 757 | + { |
| 758 | + tx.run( "CREATE (:Person {name: 'Natasha Romanoff'})" ); |
| 759 | + tx.success(); |
| 760 | + tx.failure(); |
| 761 | + return 42; |
| 762 | + } |
| 763 | + } ); |
| 764 | + |
| 765 | + assertEquals( 42, answer ); |
| 766 | + } |
| 767 | + |
| 768 | + try ( Session session = driver.session() ) |
| 769 | + { |
| 770 | + StatementResult result = session.run( "MATCH (p:Person {name: 'Natasha Romanoff'}) RETURN count(p)" ); |
| 771 | + assertEquals( 0, result.single().get( 0 ).asInt() ); |
| 772 | + } |
| 773 | + } |
| 774 | + } |
| 775 | + |
| 776 | + @Test |
| 777 | + public void readTxRolledBackWhenMarkedAsSuccessAndThrowsException() |
| 778 | + { |
| 779 | + try ( Driver driver = newDriverWithoutRetries(); |
| 780 | + Session session = driver.session() ) |
| 781 | + { |
| 782 | + assertNull( session.lastBookmark() ); |
| 783 | + |
| 784 | + try |
| 785 | + { |
| 786 | + session.readTransaction( new TransactionWork<Long>() |
| 787 | + { |
| 788 | + @Override |
| 789 | + public Long execute( Transaction tx ) |
| 790 | + { |
| 791 | + tx.run( "RETURN 42" ); |
| 792 | + tx.success(); |
| 793 | + throw new IllegalStateException(); |
| 794 | + } |
| 795 | + } ); |
| 796 | + fail( "Exception expected" ); |
| 797 | + } |
| 798 | + catch ( Exception e ) |
| 799 | + { |
| 800 | + assertThat( e, instanceOf( IllegalStateException.class ) ); |
| 801 | + } |
| 802 | + |
| 803 | + // bookmark should remain null after rollback |
| 804 | + assertNull( session.lastBookmark() ); |
| 805 | + } |
| 806 | + } |
| 807 | + |
| 808 | + @Test |
| 809 | + public void writeTxRolledBackWhenMarkedAsSuccessAndThrowsException() |
| 810 | + { |
| 811 | + try ( Driver driver = newDriverWithoutRetries() ) |
| 812 | + { |
| 813 | + try ( Session session = driver.session() ) |
| 814 | + { |
| 815 | + try |
| 816 | + { |
| 817 | + session.writeTransaction( new TransactionWork<Integer>() |
| 818 | + { |
| 819 | + @Override |
| 820 | + public Integer execute( Transaction tx ) |
| 821 | + { |
| 822 | + tx.run( "CREATE (:Person {name: 'Natasha Romanoff'})" ); |
| 823 | + tx.success(); |
| 824 | + throw new IllegalStateException(); |
| 825 | + } |
| 826 | + } ); |
| 827 | + fail( "Exception expected" ); |
| 828 | + } |
| 829 | + catch ( Exception e ) |
| 830 | + { |
| 831 | + assertThat( e, instanceOf( IllegalStateException.class ) ); |
| 832 | + } |
| 833 | + } |
| 834 | + |
| 835 | + try ( Session session = driver.session() ) |
| 836 | + { |
| 837 | + StatementResult result = session.run( "MATCH (p:Person {name: 'Natasha Romanoff'}) RETURN count(p)" ); |
| 838 | + assertEquals( 0, result.single().get( 0 ).asInt() ); |
| 839 | + } |
569 | 840 | }
|
570 | 841 | }
|
571 | 842 |
|
@@ -668,6 +939,11 @@ public Void execute( Transaction tx )
|
668 | 939 | }
|
669 | 940 | }
|
670 | 941 |
|
| 942 | + private Driver newDriverWithoutRetries() |
| 943 | + { |
| 944 | + return newDriverWithFixedRetries( 0 ); |
| 945 | + } |
| 946 | + |
671 | 947 | private Driver newDriverWithFixedRetries( int maxRetriesCount )
|
672 | 948 | {
|
673 | 949 | DriverFactory driverFactory = new DriverFactoryWithFixedRetryLogic( maxRetriesCount );
|
|
0 commit comments