You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by ti...@apache.org on 2018/01/18 17:08:41 UTC

[aries-tx-control] 01/04: tx-control-service-local spec compliance

This is an automated email from the ASF dual-hosted git repository.

timothyjward pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/aries-tx-control.git

commit a2b1a35cfc4080b1ae890e6e4a88705712d0222a
Author: Tim Ward <ti...@apache.org>
AuthorDate: Thu Jan 18 16:29:22 2018 +0000

    tx-control-service-local spec compliance
    
    Registering a pre-completion callback should throw an IllegalStateException once pre-completion has started
---
 .gitignore                                             |  4 ++++
 .../service/common/impl/NoTransactionContextTest.java  | 17 +++++++++++++++++
 .../service/local/impl/TransactionContextImpl.java     |  9 ++++++---
 .../service/local/impl/TransactionContextTest.java     | 18 ++++++++++++++++++
 4 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4191720
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+.project
+.settings/
+.classpath
+target/
diff --git a/tx-control-services/tx-control-service-common/src/test/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextTest.java b/tx-control-services/tx-control-service-common/src/test/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextTest.java
index 9426bb5..4cf4c81 100644
--- a/tx-control-services/tx-control-service-common/src/test/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextTest.java
+++ b/tx-control-services/tx-control-service-common/src/test/java/org/apache/aries/tx/control/service/common/impl/NoTransactionContextTest.java
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
 import static org.osgi.service.transaction.control.TransactionStatus.NO_TRANSACTION;
 
 import java.util.concurrent.atomic.AtomicInteger;
@@ -132,6 +133,22 @@ public class NoTransactionContextTest {
 	}
 
 	@Test
+	public void testPreCompletionRegisterPreCompletion() throws Exception {
+		
+		AtomicInteger value = new AtomicInteger(0);
+		
+		ctx.preCompletion(() -> ctx.preCompletion(() -> value.compareAndSet(1, 5)));
+		
+		assertEquals(0, value.getAndSet(1));
+		
+		ctx.finish();
+		
+		assertTrue(ctx.firstUnexpectedException.get() instanceof IllegalStateException);
+		
+		assertEquals(1, value.get());
+	}
+
+	@Test
 	public void testPostCompletion() throws Exception {
 		
 		AtomicInteger value = new AtomicInteger(0);
diff --git a/tx-control-services/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionContextImpl.java b/tx-control-services/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionContextImpl.java
index 1c6c14e..8d72ee4 100644
--- a/tx-control-services/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionContextImpl.java
+++ b/tx-control-services/tx-control-service-local/src/main/java/org/apache/aries/tx/control/service/local/impl/TransactionContextImpl.java
@@ -42,6 +42,8 @@ public class TransactionContextImpl extends AbstractTransactionContextImpl imple
 	final List<LocalResource> resources = new ArrayList<>();
 
 	private final boolean readOnly;
+	
+	private boolean workBodyFinished;
 
 	private AtomicReference<TransactionStatus> tranStatus = new AtomicReference<>(ACTIVE);
 
@@ -115,8 +117,8 @@ public class TransactionContextImpl extends AbstractTransactionContextImpl imple
 
 	@Override
 	public void preCompletion(Runnable job) throws IllegalStateException {
-		if (tranStatus.get().compareTo(MARKED_ROLLBACK) > 0) {
-			throw new IllegalStateException("The current transaction is in state " + tranStatus);
+		if (workBodyFinished) {
+			throw new IllegalStateException("The current transactional work has finished executing so a pre-completion callback can no longer be registered");
 		}
 
 		preCompletion.add(job);
@@ -126,7 +128,7 @@ public class TransactionContextImpl extends AbstractTransactionContextImpl imple
 	public void postCompletion(Consumer<TransactionStatus> job) throws IllegalStateException {
 		TransactionStatus status = tranStatus.get();
 		if (status == COMMITTED || status == ROLLED_BACK) {
-			throw new IllegalStateException("The current transaction is in state " + tranStatus);
+			throw new IllegalStateException("The current transaction is complete so a post-completion callback can no longer be registered");
 		}
 
 		postCompletion.add(job);
@@ -168,6 +170,7 @@ public class TransactionContextImpl extends AbstractTransactionContextImpl imple
 
 	@Override
 	public void finish() {
+		workBodyFinished = true;
 		
 		beforeCompletion(() -> setRollbackOnly());
 
diff --git a/tx-control-services/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionContextTest.java b/tx-control-services/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionContextTest.java
index 634bfc9..814804c 100644
--- a/tx-control-services/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionContextTest.java
+++ b/tx-control-services/tx-control-service-local/src/test/java/org/apache/aries/tx/control/service/local/impl/TransactionContextTest.java
@@ -327,6 +327,24 @@ public class TransactionContextTest {
 		
 		Mockito.verify(localResource).rollback();
 	}
+	
+	@Test
+	public void testLocalResourcePreCompletionRegisterPreCompletion() throws Exception {
+		
+		ctx.registerLocalResource(localResource);
+		
+		Mockito.doAnswer(i -> {
+			assertEquals(ROLLING_BACK, ctx.getTransactionStatus());
+			return null;
+		}).when(localResource).rollback();
+		
+		
+		ctx.preCompletion(() -> ctx.preCompletion(() -> {}));
+		
+		ctx.finish();
+		
+		Mockito.verify(localResource).rollback();
+	}
 
 	@Test
 	public void testLocalResourcePostCommitException() throws Exception {

-- 
To stop receiving notification emails like this one, please contact
"commits@aries.apache.org" <co...@aries.apache.org>.