public <FROM, TO> Function<FROM,TO> tx( Function<FROM,TO> function ) { return from -> { Function<GraphDatabaseService,TO> inner = graphDb -> function.apply( from ); return executeAndCommit( inner ); }; }
@Before public void given() { db.executeAndCommit( createIndex ); db.executeAndCommit( awaitIndexesOnline( 5, SECONDS ) ); }
@Test @SuppressWarnings( "unchecked" ) public void shouldAllowPropertyReadsInSchemaTransaction() { // given Pair<Node, Node> nodes = db.executeAndCommit( aPairOfNodes() ); Relationship relationship = db.executeAndCommit( relate( nodes ) ); db.executeAndCommit( propertyWrite( Node.class, nodes.first(), "key1", "value1" ) ); db.executeAndCommit( propertyWrite( Relationship.class, relationship, "key1", "value1" ) ); // when for ( Function<GraphDatabaseService, ?> operation : new Function[]{ propertyRead( Node.class, nodes.first(), "key1" ), propertyRead( Relationship.class, relationship, "key1" ), } ) { // then db.executeAndRollback( succeedAfterSchemaOperation( operation ) ); } }
@Test public void shouldAllowConcurrentCreationOfNonConflictingData() throws Exception { // given database.executeAndCommit( createUniquenessConstraint( "Label1", "key1" ) ); // when Future<Boolean> created = database.executeAndCommit( db -> { db.createNode( label( "Label1" ) ).setProperty( "key1", "value1" ); return otherThread.execute( createNode( db, "Label1", "key1", "value2" ) ); } ); // then assertTrue( "Node creation should succeed", created.get() ); }
@Test @SuppressWarnings( "unchecked" ) public void shouldNotAllowPropertyWritesInSchemaTransaction() { // given Pair<Node, Node> nodes = db.executeAndCommit( aPairOfNodes() ); Relationship relationship = db.executeAndCommit( relate( nodes ) ); // when for ( Function<GraphDatabaseService, ?> operation : new Function[]{ propertyWrite( Node.class, nodes.first(), "key1", "value1" ), propertyWrite( Relationship.class, relationship, "key1", "value1" ), } ) { // then db.executeAndRollback( expectFailureAfterSchemaOperation( operation ) ); } }
@Test public void shouldNotAllowRelationshipCreationInSchemaTransaction() { // given final Pair<Node, Node> nodes = db.executeAndCommit( aPairOfNodes() ); // then db.executeAndRollback( expectFailureAfterSchemaOperation( relate( nodes ) ) ); }
@Test public void shouldPreventConcurrentCreationOfConflictingData() throws Exception { // given database.executeAndCommit( createUniquenessConstraint( "Label1", "key1" ) ); // when Future<Boolean> created = database.executeAndCommit( db -> { db.createNode( label( "Label1" ) ).setProperty( "key1", "value1" ); try { return otherThread.execute( createNode( db, "Label1", "key1", "value1" ) ); } finally { assertThat( otherThread, isWaiting() ); } } ); // then assertFalse( "node creation should fail", created.get() ); }
@Test public void shouldAllowOtherTransactionToCompleteIfFirstTransactionRollsBack() throws Exception { // given database.executeAndCommit( createUniquenessConstraint( "Label1", "key1" ) ); // when Future<Boolean> created = database.executeAndRollback( db -> { db.createNode( label( "Label1" ) ).setProperty( "key1", "value1" ); try { return otherThread.execute( createNode( db, "Label1", "key1", "value1" ) ); } finally { assertThat( otherThread, isWaiting() ); } } ); // then assertTrue( "Node creation should succeed", created.get() ); }
public <FROM, TO> Function<FROM,TO> tx( Function<FROM,TO> function ) { return from -> { Function<GraphDatabaseService,TO> inner = graphDb -> function.apply( from ); return executeAndCommit( inner ); }; }