Skip to content

Commit 774e1ae

Browse files
Calling hasNext on closed cursor is throwing error. (#74)
1 parent 7dea177 commit 774e1ae

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

document-store/src/integrationTest/java/org/hypertrace/core/documentstore/mongo/MongoQueryExecutorIntegrationTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.hypertrace.core.documentstore.utils.Utils.createDocumentsFromResource;
2323
import static org.hypertrace.core.documentstore.utils.Utils.readFileFromResource;
2424
import static org.junit.Assert.assertThrows;
25+
import static org.junit.jupiter.api.Assertions.assertFalse;
2526

2627
import com.typesafe.config.Config;
2728
import com.typesafe.config.ConfigFactory;
@@ -104,6 +105,16 @@ public void testFindAll() throws IOException {
104105
assertSizeEqual(resultDocs, "mongo/collection_data.json");
105106
}
106107

108+
@Test
109+
public void testHasNext() throws IOException {
110+
Query query = Query.builder().build();
111+
112+
Iterator<Document> resultDocs = collection.find(query);
113+
assertSizeEqual(resultDocs, "mongo/collection_data.json");
114+
// hasNext should not throw error even after cursor is closed
115+
assertFalse(resultDocs.hasNext());
116+
}
117+
107118
@Test
108119
public void testFindSimple() throws IOException {
109120
List<SelectionSpec> selectionSpecs =

document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoCollection.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,14 +588,19 @@ private BasicDBObject selectionCriteriaForKeys(Set<Key> keys) {
588588

589589
private CloseableIterator<Document> convertToDocumentIterator(MongoCursor<BasicDBObject> cursor) {
590590
return new CloseableIterator<>() {
591+
private boolean closed = false;
592+
591593
@Override
592594
public void close() {
593-
cursor.close();
595+
if (!closed) {
596+
cursor.close();
597+
}
598+
closed = true;
594599
}
595600

596601
@Override
597602
public boolean hasNext() {
598-
boolean hasNext = cursor.hasNext();
603+
boolean hasNext = !closed && cursor.hasNext();
599604
if (!hasNext) {
600605
close();
601606
}

0 commit comments

Comments
 (0)