Skip to content

Commit

Permalink
Merge pull request #30366 from flapdoodle-oss/update-flapdoodle-depen…
Browse files Browse the repository at this point in the history
…dency

Upgrade flapdoodle embed mongo to 4.4.0
  • Loading branch information
gsmet authored Jan 27, 2023
2 parents e7e382a + 6458bff commit de777d5
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 553 deletions.
2 changes: 1 addition & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
<okhttp.version>3.14.9</okhttp.version><!-- keep in sync with okio -->
<okio.version>1.17.2</okio.version><!-- keep in sync with okhttp -->
<hibernate-quarkus-local-cache.version>0.1.1</hibernate-quarkus-local-cache.version>
<flapdoodle.mongo.version>3.5.3</flapdoodle.mongo.version>
<flapdoodle.mongo.version>4.4.0</flapdoodle.mongo.version>
<quarkus-spring-api.version>5.2.SP7</quarkus-spring-api.version>
<quarkus-spring-data-api.version>2.1.SP2</quarkus-spring-data-api.version>
<quarkus-spring-security-api.version>5.4.Final</quarkus-spring-security-api.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package io.quarkus.mongodb;

import static de.flapdoodle.embed.process.config.process.ProcessOutput.builder;

import java.io.IOException;

import org.jboss.logging.Logger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;

import de.flapdoodle.embed.mongo.Command;
import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.Defaults;
import de.flapdoodle.embed.mongo.config.MongoCmdOptions;
import de.flapdoodle.embed.mongo.config.MongodConfig;
import de.flapdoodle.embed.mongo.commands.MongodArguments;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.config.RuntimeConfig;
import de.flapdoodle.embed.process.io.Processors;
import de.flapdoodle.embed.mongo.transitions.Mongod;
import de.flapdoodle.embed.mongo.transitions.RunningMongodProcess;
import de.flapdoodle.reverse.TransitionWalker;
import de.flapdoodle.reverse.transitions.Start;

public class MongoTestBase {

private static final Logger LOGGER = Logger.getLogger(MongoTestBase.class);
private static MongodExecutable MONGO;
private static TransitionWalker.ReachedState<RunningMongodProcess> MONGO;

protected static String getConfiguredConnectionString() {
return getProperty("connection_string");
Expand All @@ -42,77 +37,47 @@ protected static String getProperty(String name) {

@BeforeAll
public static void startMongoDatabase() throws IOException {
try {
//JDK bug workaround
//https://github.com/quarkusio/quarkus/issues/14424
//force class init to prevent possible deadlock when done by mongo threads
Class.forName("sun.net.ext.ExtendedSocketOptions", true, ClassLoader.getSystemClassLoader());
} catch (ClassNotFoundException e) {
}
forceExtendedSocketOptionsClassInit();

String uri = getConfiguredConnectionString();
// This switch allow testing against a running mongo database.
if (uri == null) {
Version.Main version = Version.Main.V4_0;
int port = 27018;
LOGGER.infof("Starting Mongo %s on port %s", version, port);
MongodConfig config = MongodConfig.builder()
.version(version)
.net(new Net("127.0.0.1", port, false))
.cmdOptions(MongoCmdOptions.builder()
.useNoJournal(false)
.build())
.build();
MONGO = getMongodExecutable(config);
try {
MONGO.start();
} catch (Exception e) {
//every so often mongo fails to start on CI runs
//see if this helps
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {

}
MONGO.start();
}
MONGO = Mongod.instance()
.withNet(Start.to(Net.class).initializedWith(Net.builder()
.from(Net.defaults())
.port(port)
.build()))
.withMongodArguments(Start.to(MongodArguments.class)
.initializedWith(MongodArguments.defaults().withUseNoJournal(false)))
.start(version);

} else {
LOGGER.infof("Using existing Mongo %s", uri);
}
}

private static MongodExecutable getMongodExecutable(MongodConfig config) {
try {
return doGetExecutable(config);
} catch (Exception e) {
// sometimes the download process can time out so just sleep and try again
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {

}
return doGetExecutable(config);
}
}

private static MongodExecutable doGetExecutable(MongodConfig config) {
RuntimeConfig runtimeConfig = Defaults.runtimeConfigFor(Command.MongoD)
.processOutput(builder()
.output(Processors.silent())
.error(Processors.silent())
.commands(Processors.silent())
.build())
.build();
return MongodStarter.getInstance(runtimeConfig).prepare(config);
}

@AfterAll
public static void stopMongoDatabase() {
if (MONGO != null) {
try {
MONGO.stop();
MONGO.close();
} catch (Exception e) {
LOGGER.error("Unable to stop MongoDB", e);
}
}
}

public static void forceExtendedSocketOptionsClassInit() {
try {
//JDK bug workaround
//https://github.com/quarkusio/quarkus/issues/14424
//force class init to prevent possible deadlock when done by mongo threads
Class.forName("sun.net.ext.ExtendedSocketOptions", true, ClassLoader.getSystemClassLoader());
} catch (ClassNotFoundException e) {
}
}
}
Loading

0 comments on commit de777d5

Please sign in to comment.