diff --git a/.travis.yml b/.travis.yml index 9cce8d6c4be2d690cb3df402752eca83f968c48e..a0dcb824026d2d282887b3115927cd084e506fa9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,3 +10,7 @@ jdk: install: mvn install -q -DskipTests=true script: mvn test -q + +# Services to start for tests. +services: + - mongodb diff --git a/mongodb/pom.xml b/mongodb/pom.xml index 3038e17d4e10c229d2c472bb5aee2388c65dfdd2..96f6e6582e1f3ed577f8721f8446c8f93d950310 100644 --- a/mongodb/pom.xml +++ b/mongodb/pom.xml @@ -42,12 +42,6 @@ <version>4.12</version> <scope>test</scope> </dependency> - <dependency> - <groupId>de.flapdoodle.embed</groupId> - <artifactId>de.flapdoodle.embed.mongo</artifactId> - <version>1.47.3</version> - <scope>test</scope> - </dependency> </dependencies> <build> diff --git a/mongodb/src/main/java/com/yahoo/ycsb/db/MongoDbClient.java b/mongodb/src/main/java/com/yahoo/ycsb/db/MongoDbClient.java index 5163f05c7a43a85f61fc30272db3f29e7ac05335..a2e50b329cd3b4a9c1ad72c0b2f9b9d8bd45311b 100644 --- a/mongodb/src/main/java/com/yahoo/ycsb/db/MongoDbClient.java +++ b/mongodb/src/main/java/com/yahoo/ycsb/db/MongoDbClient.java @@ -129,7 +129,7 @@ public class MongoDbClient extends DB { Document query = new Document("_id", key); DeleteResult result = collection.withWriteConcern(writeConcern) .deleteOne(query); - if (result.getDeletedCount() == 0) { + if (result.wasAcknowledged() && result.getDeletedCount() == 0) { System.err.println("Nothing deleted for key " + key); return 1; } @@ -253,11 +253,14 @@ public class MongoDbClient extends DB { if (batchSize <= 1) { UpdateResult result = collection.withWriteConcern(writeConcern) .replaceOne(criteria, toInsert, UPSERT); - if (result.getMatchedCount() > 0 - || result.getModifiedCount() > 0 + if (!result.wasAcknowledged() + || result.getMatchedCount() > 0 + || (result.isModifiedCountAvailable() && (result + .getModifiedCount() > 0)) || result.getUpsertedId() != null) { return 0; } + System.err.println("Nothing inserted for key " + key); return 1; } @@ -272,7 +275,8 @@ public class MongoDbClient extends DB { BulkWriteResult result = collection.withWriteConcern( writeConcern).bulkWrite(bulkInserts, new BulkWriteOptions().ordered(false)); - if (result.getInsertedCount() == bulkInserts.size()) { + if (!result.wasAcknowledged() + || result.getInsertedCount() == bulkInserts.size()) { bulkInserts.clear(); return 0; } @@ -445,7 +449,7 @@ public class MongoDbClient extends DB { UpdateResult result = collection.withWriteConcern(writeConcern) .updateOne(query, update); - if (result.getMatchedCount() == 0) { + if (result.wasAcknowledged() && result.getMatchedCount() == 0) { System.err.println("Nothing updated for key " + key); return 1; } diff --git a/mongodb/src/test/java/com/yahoo/ycsb/db/AbstractDBTestCases.java b/mongodb/src/test/java/com/yahoo/ycsb/db/AbstractDBTestCases.java index 503a83f34772d7921f8318ddd5f4615442c4fcc6..79340e8016f11ab4ee6bb13368d207cd4eeb7b01 100644 --- a/mongodb/src/test/java/com/yahoo/ycsb/db/AbstractDBTestCases.java +++ b/mongodb/src/test/java/com/yahoo/ycsb/db/AbstractDBTestCases.java @@ -17,20 +17,21 @@ package com.yahoo.ycsb.db; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeNoException; -import java.io.File; import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; import java.util.Collections; import java.util.HashMap; import java.util.Set; import java.util.Vector; -import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -38,79 +39,47 @@ import com.yahoo.ycsb.ByteArrayByteIterator; import com.yahoo.ycsb.ByteIterator; import com.yahoo.ycsb.DB; -import de.flapdoodle.embed.mongo.Command; -import de.flapdoodle.embed.mongo.MongodExecutable; -import de.flapdoodle.embed.mongo.MongodProcess; -import de.flapdoodle.embed.mongo.MongodStarter; -import de.flapdoodle.embed.mongo.config.ArtifactStoreBuilder; -import de.flapdoodle.embed.mongo.config.IMongodConfig; -import de.flapdoodle.embed.mongo.config.MongodConfigBuilder; -import de.flapdoodle.embed.mongo.config.Net; -import de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder; -import de.flapdoodle.embed.mongo.distribution.Version; -import de.flapdoodle.embed.process.io.directories.FixedPath; -import de.flapdoodle.embed.process.runtime.Network; - /** * MongoDbClientTest provides runs the basic DB test cases. + * <p> + * The tests will be skipped if MongoDB is not running on port 27017 on the + * local machine. See the README.md for how to get MongoDB running. + * </p> */ @SuppressWarnings("boxing") public abstract class AbstractDBTestCases { - /** The running Mongodb process. */ - private static MongodProcess ourMongod = null; - - /** The handle to the running server. */ - private static MongodExecutable ourMongodExecutable = null; - - /** The directory to download the MongoDB executables to. */ - private static final File TMP_DIR = new File("target/mongodb"); + /** The default port for MongoDB. */ + private static final int MONGODB_DEFAULT_PORT = 27017; /** - * Start a test mongd instance. + * Verifies the mongod process (or some process) is running on port 27017, + * if not the tests are skipped. */ @BeforeClass public static void setUpBeforeClass() { - TMP_DIR.mkdirs(); - - MongodStarter starter = MongodStarter - .getInstance(new RuntimeConfigBuilder() - .defaults(Command.MongoD) - .artifactStore( - new ArtifactStoreBuilder() - .defaults(Command.MongoD) - .useCache(false) - .tempDir( - new FixedPath(TMP_DIR - .getAbsolutePath()))) - .build()); - int port = 27017; - + // Test if we can connect. + Socket socket = null; try { - IMongodConfig mongodConfig = new MongodConfigBuilder() - .version(Version.Main.PRODUCTION) - .net(new Net(port, Network.localhostIsIPv6())).build(); - - ourMongodExecutable = starter.prepare(mongodConfig); - ourMongod = ourMongodExecutable.start(); + // Connect + socket = new Socket(InetAddress.getLocalHost(), + MONGODB_DEFAULT_PORT); + assertThat("Socket is not bound.", socket.getLocalPort(), not(-1)); } - catch (IOException error) { - assumeNoException(error); + catch (IOException connectFailed) { + assumeNoException("MongoDB is not running. Skipping tests.", + connectFailed); } - } - - /** - * Stops the test server. - */ - @AfterClass - public static void tearDownAfterClass() { - if (ourMongod != null) { - ourMongod.stop(); - ourMongod = null; - } - if (ourMongodExecutable != null) { - ourMongodExecutable.stop(); - ourMongodExecutable = null; + finally { + if (socket != null) { + try { + socket.close(); + } + catch (IOException ignore) { + // Ignore. + } + } + socket = null; } }