Created
July 23, 2015 09:42
-
-
Save ibodrov/54b98df8ec8fa1604754 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testCommit() throws Exception { | |
// Start the repository using the JBoss Transactions transaction manager ... | |
InputStream config = getClass().getClassLoader().getResourceAsStream("config/repo-config-inmemory-jbosstxn.json"); | |
assertThat(config, is(notNullValue())); | |
startRepositoryWithConfiguration(config); | |
// STEP 1: create and checkin parent nodes | |
Thread t1 = new Thread() { | |
@Override | |
public void run() { | |
try { | |
startTransaction(); | |
Node root = session.getRootNode(); | |
Node parent = root.addNode("parent"); | |
parent.addMixin("mix:versionable"); | |
parent.addNode("nested"); | |
session.save(); | |
VersionManager vm = session.getWorkspace().getVersionManager(); | |
vm.checkin("/parent"); | |
commitTransaction(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}; | |
t1.start(); | |
t1.join(); | |
// STEP 2: checkout, create child and checkin | |
Thread t2 = new Thread() { | |
@Override | |
public void run() { | |
try { | |
startTransaction(); | |
VersionManager vm = session.getWorkspace().getVersionManager(); | |
vm.checkout("/parent"); | |
Node nested = session.getNode("/parent/nested"); | |
nested.addNode("child"); | |
session.save(); | |
vm.checkin("/parent"); | |
commitTransaction(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}; | |
t2.start(); | |
t2.join(); | |
// long transaction | |
ExecutorService executor = Executors.newFixedThreadPool(1); | |
final Transaction longTx = executor.submit(new Callable<Transaction>() { | |
@Override | |
public Transaction call() throws Exception { | |
System.out.println("started @ " + Thread.currentThread().getId()); | |
startTransaction(); | |
return suspendTransaction(); | |
} | |
}).get(); | |
// STEP 3: resume, checkout, suspend | |
Thread t3 = new Thread() { | |
@Override | |
public void run() { | |
try { | |
resumeTransaction(longTx); | |
VersionManager vm = session.getWorkspace().getVersionManager(); | |
vm.checkout("/parent"); | |
suspendTransaction(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}; | |
t3.start(); | |
t3.join(); | |
// STEP 4: resume, remove child, suspend | |
Thread t4 = new Thread() { | |
@Override | |
public void run() { | |
try { | |
resumeTransaction(longTx); | |
session.removeItem("/parent/nested/child"); | |
session.save(); | |
suspendTransaction(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}; | |
t4.start(); | |
t4.join(); | |
// STEP 5: check if child is still exists outside of longTx | |
try { | |
Session s = repository.login(); | |
s.getNode("/parent/nested/child"); | |
s.logout(); | |
} catch (Exception e) { | |
fail("should be ok"); | |
} | |
// STEP 6: resume, checkin, commit | |
executor.submit(new Callable<Void>() { | |
@Override | |
public Void call() throws Exception { | |
resumeTransaction(longTx); | |
VersionManager vm = session.getWorkspace().getVersionManager(); | |
vm.checkin("/parent"); | |
commitTransaction(); | |
System.out.println("commited @ " + Thread.currentThread().getId()); | |
return null; | |
} | |
}).get(); | |
// STEP 7: check if child is gone | |
try { | |
Session s = repository.login(); | |
s.getNode("/parent/nested/child"); | |
fail("should fail"); | |
} catch (Exception e) { | |
} | |
} | |
protected Transaction suspendTransaction() throws SystemException { | |
TransactionManager txnMgr = transactionManager(); | |
return txnMgr.suspend(); | |
} | |
protected void resumeTransaction(Transaction t) throws InvalidTransactionException, IllegalStateException, SystemException { | |
TransactionManager txnMgr = transactionManager(); | |
txnMgr.resume(t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment