Skip to content

Commit

Permalink
added tests to ensure MongoCursor is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
asereda-gs committed Dec 15, 2017
1 parent 3b3e69a commit 58ffa24
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
6 changes: 6 additions & 0 deletions mongo/pom.xml
Expand Up @@ -84,6 +84,12 @@
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
Expand Down
6 changes: 5 additions & 1 deletion mongo/src/org/immutables/mongo/repository/Repositories.java
Expand Up @@ -24,6 +24,7 @@
import com.google.gson.TypeAdapter;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.FindOneAndDeleteOptions;
import com.mongodb.client.model.FindOneAndReplaceOptions;
import com.mongodb.client.model.FindOneAndUpdateOptions;
Expand Down Expand Up @@ -319,7 +320,10 @@ public List<T> call() throws Exception {
}
}

return ImmutableList.copyOf(cursor);
// close properly
try (MongoCursor<T> iterator = cursor.iterator()) {
return ImmutableList.copyOf(iterator);
}
}
});
}
Expand Down
@@ -0,0 +1,93 @@
package org.immutables.mongo.fixture;

import com.google.common.util.concurrent.MoreExecutors;
import com.google.gson.GsonBuilder;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.immutables.mongo.repository.RepositorySetup;
import org.junit.Before;
import org.junit.Test;

import static org.immutables.check.Checkers.check;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* Set of tests to ensure mongo resources are properly closed.
*
* @see MongoCursor#close()
* @see <a href="https://docs.mongodb.com/v3.4/reference/method/cursor.close/">cursor.close</a>
*/
public class MongoCursorIsClosedTest {

private EntityRepository repository;

private MongoCursor<Entity> cursor;

@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
MongoDatabase db = mock(MongoDatabase.class);
MongoCollection collection = mock(MongoCollection.class);

when(db.getCollection(anyString())).thenReturn(collection);
when(collection.withDocumentClass(any(Class.class))).thenReturn(collection);
when(collection.withCodecRegistry(any(CodecRegistry.class))).thenReturn(collection);

RepositorySetup setup = RepositorySetup.builder().database(db)
.executor(MoreExecutors.newDirectExecutorService())
.gson(new GsonBuilder().registerTypeAdapterFactory(new GsonAdaptersEntity()).create())
.build();

this.repository = new EntityRepository(setup);

FindIterable<Entity> iterable = mock(FindIterable.class);
when(collection.find(any(Bson.class))).thenReturn(iterable);
MongoCursor<Entity> cursor = mock(MongoCursor.class);
when(iterable.iterator()).thenReturn(cursor);

this.cursor = cursor;
}

@Test
public void emptyResult() throws Exception {
when(cursor.hasNext()).thenReturn(false);
check(repository.findAll().fetchAll().getUnchecked()).isEmpty();

verify(cursor, times(1)).close();
}

@Test
public void singleResult() throws Exception {
when(cursor.hasNext()).thenReturn(true).thenReturn(false);
when(cursor.next()).thenReturn(ImmutableEntity.of("foo"));

check(repository.findAll().fetchAll().getUnchecked()).has(ImmutableEntity.of("foo"));

verify(cursor, times(1)).close();
}

@Test
public void onException() throws Exception {
when(cursor.hasNext()).thenReturn(true);
when(cursor.next()).thenThrow(new IllegalStateException("boom"));

try {
repository.findAll().fetchAll().getUnchecked();
fail("Shouldn't get here");
} catch (Exception ignore) {
// expected
}

verify(cursor, times(1)).close();
}
}

0 comments on commit 58ffa24

Please sign in to comment.