knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pzamp...@apache.org
Subject knox git commit: KNOX-1283 - DefaultURLManager markFailed method throws NPE if urls is empty
Date Fri, 27 Apr 2018 15:04:28 GMT
Repository: knox
Updated Branches:
  refs/heads/master 12a1fd65d -> c061d20a8


KNOX-1283 - DefaultURLManager markFailed method throws NPE if urls is empty


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

Branch: refs/heads/master
Commit: c061d20a8135ff80d8c99cb5403a5f295b1c2a63
Parents: 12a1fd6
Author: Phil Zampino <pzampino@apache.org>
Authored: Fri Apr 27 10:42:50 2018 -0400
Committer: Phil Zampino <pzampino@apache.org>
Committed: Fri Apr 27 10:43:20 2018 -0400

----------------------------------------------------------------------
 .../ha/provider/impl/DefaultURLManager.java     | 28 +++++++++++---------
 .../ha/provider/impl/DefaultURLManagerTest.java | 17 ++++++++++++
 2 files changed, 32 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/c061d20a/gateway-provider-ha/src/main/java/org/apache/knox/gateway/ha/provider/impl/DefaultURLManager.java
----------------------------------------------------------------------
diff --git a/gateway-provider-ha/src/main/java/org/apache/knox/gateway/ha/provider/impl/DefaultURLManager.java
b/gateway-provider-ha/src/main/java/org/apache/knox/gateway/ha/provider/impl/DefaultURLManager.java
index f6ad025..8ee59a7 100644
--- a/gateway-provider-ha/src/main/java/org/apache/knox/gateway/ha/provider/impl/DefaultURLManager.java
+++ b/gateway-provider-ha/src/main/java/org/apache/knox/gateway/ha/provider/impl/DefaultURLManager.java
@@ -80,19 +80,21 @@ public class DefaultURLManager implements URLManager {
   @Override
   public synchronized void markFailed(String url) {
     String top = urls.peek();
-    boolean pushToBottom = false;
-    URI topUri = URI.create(top);
-    URI incomingUri = URI.create(url);
-    String topHostPort = topUri.getHost() + ":" + topUri.getPort();
-    String incomingHostPort = incomingUri.getHost() + ":" + incomingUri.getPort();
-    if ( topHostPort.equals(incomingHostPort) ) {
-      pushToBottom = true;
-    }
-    //put the failed url at the bottom
-    if ( pushToBottom ) {
-      String failed = urls.poll();
-      urls.offer(failed);
-      LOG.markedFailedUrl(failed, urls.peek());
+    if (top != null) {
+      boolean pushToBottom = false;
+      URI topUri = URI.create(top);
+      URI incomingUri = URI.create(url);
+      String topHostPort = topUri.getHost() + ":" + topUri.getPort();
+      String incomingHostPort = incomingUri.getHost() + ":" + incomingUri.getPort();
+      if (topHostPort.equals(incomingHostPort)) {
+        pushToBottom = true;
+      }
+      //put the failed url at the bottom
+      if (pushToBottom) {
+        String failed = urls.poll();
+        urls.offer(failed);
+        LOG.markedFailedUrl(failed, urls.peek());
+      }
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/c061d20a/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/DefaultURLManagerTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/DefaultURLManagerTest.java
b/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/DefaultURLManagerTest.java
index a2cfa54..84defbc 100644
--- a/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/DefaultURLManagerTest.java
+++ b/gateway-provider-ha/src/test/java/org/apache/knox/gateway/ha/provider/impl/DefaultURLManagerTest.java
@@ -23,6 +23,7 @@ import java.util.ArrayList;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 public class DefaultURLManagerTest {
 
@@ -89,4 +90,20 @@ public class DefaultURLManagerTest {
       assertEquals(url3, manager.getActiveURL());
    }
 
+   /**
+    * KNOX-1283
+    */
+   @Test
+   public void testMarkFailedWithEmptyURLs() {
+      ArrayList<String> urls = new ArrayList<>();
+      DefaultURLManager manager = new DefaultURLManager();
+      manager.setURLs(urls);
+      assertTrue(manager.getURLs().isEmpty());
+      try {
+         manager.markFailed("http://localhost:8080");
+      } catch (NullPointerException e) {
+         fail("Empty URL list should not result in NPE.");
+      }
+   }
+
 }


Mime
View raw message