Fixed #50 multiple timeout warnings

Change-Id: Id997c315725c9ee9f0a2ace2b6858519e1ad749c
diff --git a/Changes b/Changes
index f23dbba..b7d9b32 100644
--- a/Changes
+++ b/Changes
@@ -1,4 +1,4 @@
-0.58.5 2019-02-25
+0.58.5 2019-03-04
     - [bugfix] Fix bug where duplicate keys occured in
       field data output (diewald)
     - [bugfix] Fix bug where fields already set where lifted
@@ -7,6 +7,7 @@
     - [feature] Added upsert method to index and fix #43
       by adding indexCreationDate and indexLastModified field
       (diewald)
+    - [bugfix] Fixed #50 multiple timeout warnings (margaretha) 
 
 0.58.4 2019-02-05
     - [cleanup] Remove deprecated methods setLicense/getLicense,
diff --git a/src/main/java/de/ids_mannheim/korap/KrillIndex.java b/src/main/java/de/ids_mannheim/korap/KrillIndex.java
index 7520f96..7c9a19d 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillIndex.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillIndex.java
@@ -318,7 +318,7 @@
 
 
     // Close index reader
-    private void closeReader () throws IOException {
+    public void closeReader () throws IOException {
         if (readerOpen) {
             this.reader.close();
             readerOpen = false;
@@ -1527,6 +1527,7 @@
                 final IndexReader lreader = atomic.reader();
                 int localDocID, docID;
 
+                boolean isTimeout = false;
                 // TODO: Get document information from Cache! Fieldcache?
                 for (; i < hits; i++) {
 
@@ -1540,6 +1541,7 @@
                     // Timeout!
                     if (tthread.getTime() > timeout) {
                         kr.setTimeExceeded(true);
+                        isTimeout=true;
                         break;
                     };
 
@@ -1621,7 +1623,7 @@
                 };
 
                 // Can be disabled TEMPORARILY
-                while (!cutoff && spans.next()) {
+                while (!cutoff && !isTimeout && spans.next()) {
 
                     // TODO: Deprecated
                     if (limit > 0 && i >= limit)
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestTimeout.java b/src/test/java/de/ids_mannheim/korap/index/TestTimeout.java
new file mode 100644
index 0000000..7c841e4
--- /dev/null
+++ b/src/test/java/de/ids_mannheim/korap/index/TestTimeout.java
@@ -0,0 +1,35 @@
+package de.ids_mannheim.korap.index;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.spans.SpanQuery;
+import org.apache.lucene.search.spans.SpanTermQuery;
+import org.junit.Test;
+
+import de.ids_mannheim.korap.Krill;
+import de.ids_mannheim.korap.KrillIndex;
+import de.ids_mannheim.korap.KrillMeta;
+import de.ids_mannheim.korap.response.Result;
+import de.ids_mannheim.korap.util.StatusCodes;
+
+public class TestTimeout {
+
+    @Test
+    public void testMultipleWarningBug () throws IOException {
+        KrillIndex ki = new KrillIndex();
+        ki.addDoc(getClass().getResourceAsStream("/wiki/00001.json.gz"), true);
+        ki.commit();
+
+        SpanQuery q = new SpanTermQuery(new Term("tokens", "s:der"));
+        Krill ks = new Krill(q);
+        KrillMeta meta = ks.getMeta();
+        meta.setTimeOut(-1);
+        Result kr = ks.apply(ki);
+        assertEquals(1, kr.getWarnings().size());
+        assertEquals(StatusCodes.RESPONSE_TIME_EXCEEDED,
+                kr.getWarning(0).getCode());
+    }
+}