File tree Expand file tree Collapse file tree 2 files changed +18
-2
lines changed
integrationTest/java/org/hypertrace/core/documentstore/mongo
main/java/org/hypertrace/core/documentstore/mongo Expand file tree Collapse file tree 2 files changed +18
-2
lines changed Original file line number Diff line number Diff line change 2222import static org .hypertrace .core .documentstore .utils .Utils .createDocumentsFromResource ;
2323import static org .hypertrace .core .documentstore .utils .Utils .readFileFromResource ;
2424import static org .junit .Assert .assertThrows ;
25+ import static org .junit .jupiter .api .Assertions .assertFalse ;
2526
2627import com .typesafe .config .Config ;
2728import 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 =
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments