Repository: searchbox-io/Jest (java)

Issue:
NPE In Node Discovery If Elasticsearch is Down
NodeChecker results in NPE if elasticsearch cluster is unavailable on the first run:

```
Caused by: java.lang.NullPointerException
    at io.searchbox.client.config.discovery.NodeChecker.removeNodeAndUpdateServers(NodeChecker.java:114)
    at io.searchbox.client.config.discovery.NodeChecker.runOneIteration(NodeChecker.java:70)
    at com.google.common.util.concurrent.AbstractScheduledService$ServiceDelegate$Task.run(AbstractScheduledService.java:189)
    at com.google.common.util.concurrent.Callables$3.run(Callables.java:100)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
```


Pull request description:
NPE in NodeChecker On First Run w/ ES Cluster Down
Fixes #375 - resolves an NPE on the first NodeChecker run if elasticsearch is down.


Patch:
diff --git a/jest-common/src/main/java/io/searchbox/client/config/discovery/NodeChecker.java b/jest-common/src/main/java/io/searchbox/client/config/discovery/NodeChecker.java
index efead34a..ebf5a522 100644
--- a/jest-common/src/main/java/io/searchbox/client/config/discovery/NodeChecker.java
+++ b/jest-common/src/main/java/io/searchbox/client/config/discovery/NodeChecker.java
@@ -56,7 +56,8 @@ public class NodeChecker extends AbstractScheduledService {
                 clientConfig.getDiscoveryFrequency(),
                 clientConfig.getDiscoveryFrequencyTimeUnit()
         );
-		this.bootstrapServerList = ImmutableSet.copyOf(clientConfig.getServerList());
+        this.bootstrapServerList = ImmutableSet.copyOf(clientConfig.getServerList());
+        this.discoveredServerList = new LinkedHashSet<String>();
     }
 
     @Override


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/client/config/discovery/NodeCheckerTest.java b/jest-common/src/test/java/io/searchbox/client/config/discovery/NodeCheckerTest.java
index 695b1846..5b191b19 100644
--- a/jest-common/src/test/java/io/searchbox/client/config/discovery/NodeCheckerTest.java
+++ b/jest-common/src/test/java/io/searchbox/client/config/discovery/NodeCheckerTest.java
@@ -18,6 +18,7 @@ import java.util.Set;
 import java.util.concurrent.TimeUnit;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.mockito.Matchers.isA;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
@@ -247,7 +248,6 @@ public class NodeCheckerTest {
         assertEquals("http://localhost:9200", servers.iterator().next());
     }
 
-
     @Test
     public void testNodesInfoExceptionRemovesServerFromList() throws Exception {
         NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig);
@@ -320,4 +320,15 @@ public class NodeCheckerTest {
         assertEquals("http://localhost:9200", serversItr.next());
     }
 
+    @Test
+    public void testNodesInfoExceptionBeforeNodesDiscovered() throws Exception {
+        NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig);
+        when(jestClient.execute(isA(Action.class))).thenThrow(new CouldNotConnectException("http://localhost:9200", new IOException("Test HttpHostException")));
+
+        nodeChecker.runOneIteration();
+
+        assertNotNull(nodeChecker.discoveredServerList);
+        assertEquals(0, nodeChecker.discoveredServerList.size());
+	}
+
 }