Last active
August 29, 2015 14:25
-
-
Save ibodrov/107651b3e033431cf10d 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-atomikos.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 | |
startTransaction(); | |
final Transaction longTx = suspendTransaction(); | |
// 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 | |
Thread t5 = new Thread() { | |
@Override | |
public void run() { | |
try { | |
resumeTransaction(longTx); | |
VersionManager vm = session.getWorkspace().getVersionManager(); | |
vm.checkin("/parent"); | |
commitTransaction(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}; | |
t5.start(); | |
t5.join(); | |
// 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