You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2016/10/26 18:48:51 UTC

activemq git commit: https://issues.apache.org/jira/browse/AMQ-6484

Repository: activemq
Updated Branches:
  refs/heads/master 1a91decf9 -> b34336cc0


https://issues.apache.org/jira/browse/AMQ-6484

make the validators thread safe

Project: http://git-wip-us.apache.org/repos/asf/activemq/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/b34336cc
Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/b34336cc
Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/b34336cc

Branch: refs/heads/master
Commit: b34336cc0adeb8908580631f979715852458a69b
Parents: 1a91dec
Author: Timothy Bish <ta...@gmail.com>
Authored: Wed Oct 26 14:48:42 2016 -0400
Committer: Timothy Bish <ta...@gmail.com>
Committed: Wed Oct 26 14:48:42 2016 -0400

----------------------------------------------------------------------
 .../amqp/client/AmqpFrameValidator.java         | 37 +++++++++----------
 .../transport/amqp/client/AmqpValidator.java    | 39 ++++++++++----------
 2 files changed, 37 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq/blob/b34336cc/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpFrameValidator.java
----------------------------------------------------------------------
diff --git a/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpFrameValidator.java b/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpFrameValidator.java
index 7588572..fa17db8 100644
--- a/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpFrameValidator.java
+++ b/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpFrameValidator.java
@@ -16,6 +16,8 @@
  */
 package org.apache.activemq.transport.amqp.client;
 
+import java.util.concurrent.atomic.AtomicReference;
+
 import org.apache.qpid.proton.amqp.Binary;
 import org.apache.qpid.proton.amqp.transport.Attach;
 import org.apache.qpid.proton.amqp.transport.Begin;
@@ -29,12 +31,11 @@ import org.apache.qpid.proton.amqp.transport.Transfer;
 
 /**
  * Abstract base for a validation hook that is used in tests to check
- * the state of a remote resource after a variety of lifecycle events.
+ * the values of incoming or outgoing AMQP frames.
  */
 public class AmqpFrameValidator {
 
-    private boolean valid = true;
-    private String errorMessage;
+    private AtomicReference<String> errorMessage = new AtomicReference<String>();
 
     public void inspectOpen(Open open, Binary encoded) {
 
@@ -73,31 +74,29 @@ public class AmqpFrameValidator {
     }
 
     public boolean isValid() {
-        return valid;
-    }
-
-    protected void setValid(boolean valid) {
-        this.valid = valid;
+        return errorMessage.get() != null;
     }
 
-    public String getErrorMessage() {
-        return errorMessage;
+    public final void clearErrorMessage() {
+        errorMessage.set(null);
     }
 
-    protected void setErrorMessage(String errorMessage) {
-        this.errorMessage = errorMessage;
+    public final String getErrorMessage() {
+        return errorMessage.get();
     }
 
-    protected void markAsInvalid(String errorMessage) {
-        if (valid) {
-            setValid(false);
-            setErrorMessage(errorMessage);
+    protected final boolean markAsInvalid(String message) {
+        if (message == null) {
+            throw new NullPointerException("Provided error message cannot be null!");
         }
+
+        return errorMessage.compareAndSet(null, message);
     }
 
-    public void assertValid() {
-        if (!isValid()) {
-            throw new AssertionError(errorMessage);
+    public final void assertValid() {
+        final String assertionErrorMessage = errorMessage.get();
+        if (assertionErrorMessage != null) {
+            throw new AssertionError(assertionErrorMessage);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/activemq/blob/b34336cc/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpValidator.java
----------------------------------------------------------------------
diff --git a/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpValidator.java b/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpValidator.java
index fc088fc..c8a20ac 100644
--- a/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpValidator.java
+++ b/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/client/AmqpValidator.java
@@ -16,19 +16,20 @@
  */
 package org.apache.activemq.transport.amqp.client;
 
+import java.util.concurrent.atomic.AtomicReference;
+
 import org.apache.qpid.proton.engine.Connection;
 import org.apache.qpid.proton.engine.Receiver;
 import org.apache.qpid.proton.engine.Sender;
 import org.apache.qpid.proton.engine.Session;
 
 /**
- * Abstract base for a validation hook that is used in tests to check
- * the state of a remote resource after a variety of lifecycle events.
+ * Abstract base for a validation hook that is used in tests to check the state
+ * of a remote resource after a variety of lifecycle events.
  */
 public class AmqpValidator {
 
-    private boolean valid = true;
-    private String errorMessage;
+    private AtomicReference<String> errorMessage = new AtomicReference<String>();
 
     public void inspectOpenedResource(Connection connection) {
 
@@ -71,31 +72,29 @@ public class AmqpValidator {
     }
 
     public boolean isValid() {
-        return valid;
-    }
-
-    protected void setValid(boolean valid) {
-        this.valid = valid;
+        return errorMessage.get() != null;
     }
 
-    public String getErrorMessage() {
-        return errorMessage;
+    public final void clearErrorMessage() {
+        errorMessage.set(null);
     }
 
-    protected void setErrorMessage(String errorMessage) {
-        this.errorMessage = errorMessage;
+    public final String getErrorMessage() {
+        return errorMessage.get();
     }
 
-    protected void markAsInvalid(String errorMessage) {
-        if (valid) {
-            setValid(false);
-            setErrorMessage(errorMessage);
+    protected final boolean markAsInvalid(String message) {
+        if (message == null) {
+            throw new NullPointerException("Provided error message cannot be null!");
         }
+
+        return errorMessage.compareAndSet(null, message);
     }
 
-    public void assertValid() {
-        if (!isValid()) {
-            throw new AssertionError(errorMessage);
+    public final void assertValid() {
+        final String assertionErrorMessage = errorMessage.get();
+        if (assertionErrorMessage != null) {
+            throw new AssertionError(assertionErrorMessage);
         }
     }
 }