Improve timeout checks

Change-Id: Iec414b886ac1eebc66cf0f3c28c5a47aad04ce53
diff --git a/src/main/java/de/ids_mannheim/korap/KrillIndex.java b/src/main/java/de/ids_mannheim/korap/KrillIndex.java
index 540db59..976fe86 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillIndex.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillIndex.java
@@ -1496,6 +1496,7 @@
         final TimeOutThread tthread = new TimeOutThread();
         tthread.start();
         final long timeout = meta.getTimeOut();
+        boolean isTimeout = false;
 
         // See: http://www.ibm.com/developerworks/java/library/j-benchmark1/index.html
         long t1 = System.nanoTime();
@@ -1521,6 +1522,10 @@
 
                 int oldLocalDocID = -1;
 
+                if (isTimeout)
+                    break;
+
+                
                 /*
                  * Todo: There may be a way to know early if the bitset is emty
                  * by using LongBitSet - but this may not be as fast as I think.
@@ -1541,7 +1546,6 @@
                 final IndexReader lreader = atomic.reader();
                 int localDocID, docID;
 
-                boolean isTimeout = false;
                 // TODO: Get document information from Cache! Fieldcache?
                 for (; i < hits; i++) {
 
@@ -1646,6 +1650,7 @@
                     // Timeout!
                     if (tthread.getTime() > timeout) {
                         kr.setTimeExceeded(true);
+                        isTimeout=true;
                         break;
                     };
 
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestTimeout.java b/src/test/java/de/ids_mannheim/korap/index/TestTimeout.java
index 7c841e4..e7a63b6 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestTimeout.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestTimeout.java
@@ -32,4 +32,24 @@
         assertEquals(StatusCodes.RESPONSE_TIME_EXCEEDED,
                 kr.getWarning(0).getCode());
     }
+
+    @Test
+    public void testMultipleWarningMultilpleFragmentBug () throws IOException {
+        KrillIndex ki = new KrillIndex();
+        ki.addDoc(getClass().getResourceAsStream("/wiki/00001.json.gz"), true);
+        ki.commit();
+        ki.addDoc(getClass().getResourceAsStream("/wiki/00002.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());
+    }
+
+
 }