de.flapdoodle.embed.process.runtime.Network Java Examples

The following examples show how to use de.flapdoodle.embed.process.runtime.Network. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoNotebookRepoTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws IOException {
  String bindIp = "localhost";
  ServerSocket socket = new ServerSocket(0);
  int port = socket.getLocalPort();
  socket.close();

  IMongodConfig mongodConfig = new MongodConfigBuilder()
      .version(Version.Main.PRODUCTION)
      .net(new Net(bindIp, port, Network.localhostIsIPv6()))
      .build();

  mongodExecutable = MongodStarter.getDefaultInstance()
      .prepare(mongodConfig);
  mongodExecutable.start();

  System.setProperty(ZEPPELIN_NOTEBOOK_MONGO_URI.getVarName(), "mongodb://" + bindIp + ":" + port);
  zConf = new ZeppelinConfiguration();
  notebookRepo = new MongoNotebookRepo();
  notebookRepo.init(zConf);
}
 
Example #2
Source File: EmbeddedMongoHelper.java    From edison-microservice with Apache License 2.0 6 votes vote down vote up
public static void startMongoDB() throws IOException {
    if (!started.compareAndSet(false, true)) {
        throw new RuntimeException("Embedded mongo already running, call stopMongoDB before starting it again!");
    }
    final String bindIp = "localhost";
    try {
        final int port = Network.getFreeServerPort();
        final IMongodConfig mongodConfig = new MongodConfigBuilder()
                .version(Version.Main.PRODUCTION)
                .net(new Net(bindIp, port, Network.localhostIsIPv6()))
                .build();
        final MongodStarter runtime = MongodStarter.getInstance(new RuntimeConfigBuilder()
                .defaultsWithLogger(Command.MongoD, LOG)
                .build());
        mongodExecutable = runtime.prepare(mongodConfig, Distribution.detectFor(Version.Main.PRODUCTION));
        mongodProcess = mongodExecutable.start();
        mongoClient = new MongoClient(bindIp, port);
    } catch (final IOException e) {
        stopMongoDB();
        throw e;
    }
}
 
Example #3
Source File: MongoDbReadWriteIT.java    From beam with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  int port = NetworkTestHelper.getAvailableLocalPort();
  LOG.info("Starting MongoDB embedded instance on {}", port);
  IMongodConfig mongodConfig =
      new MongodConfigBuilder()
          .version(Version.Main.PRODUCTION)
          .configServer(false)
          .replication(new Storage(MONGODB_LOCATION.getRoot().getPath(), null, 0))
          .net(new Net(hostname, port, Network.localhostIsIPv6()))
          .cmdOptions(
              new MongoCmdOptionsBuilder()
                  .syncDelay(10)
                  .useNoPrealloc(true)
                  .useSmallFiles(true)
                  .useNoJournal(true)
                  .verbose(false)
                  .build())
          .build();
  mongodExecutable = mongodStarter.prepare(mongodConfig);
  mongodProcess = mongodExecutable.start();
  client = new MongoClient(hostname, port);

  mongoSqlUrl = String.format("mongodb://%s:%d/%s/%s", hostname, port, database, collection);
}
 
Example #4
Source File: MongoBaseTest.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void startMongo() throws Exception {
  String uri = getConnectionString();
  if (uri == null ) {
    Version.Main version = Version.Main.V3_4;
    int port = 27018;
    System.out.println("Starting Mongo " + version + " on port " + port);
    IMongodConfig config = new MongodConfigBuilder().
      version(version).
      net(new Net(port, Network.localhostIsIPv6())).
      build();
    exe = MongodStarter.getDefaultInstance().prepare(config);
    exe.start();
  } else {
    System.out.println("Using existing Mongo " + uri);
  }
}
 
Example #5
Source File: AbstractMongoDBResource.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void importData(String dbname, String collection, String file, ImportFormat format, ImportOption... options) throws IOException {
  int p = port.orElseThrow(() -> new IllegalStateException("Trying to access client before server has been started"));

  MongoImportConfigBuilder builder = new MongoImportConfigBuilder()
      .version(version)
      .net(new Net(p, Network.localhostIsIPv6()))
      .db(dbname)
      .collection(collection)
      .importFile(file)
      .type(format.name().toLowerCase(Locale.ROOT));

  for(ImportOption option: options) {
    if (option instanceof ImportOptions) {
      setOption(builder, (ImportOptions) option);
      continue;
    }

    throw new IllegalArgumentException("Unknown option " + option);
  }

  MongoImportExecutable executable = Environment.prepareMongoImport(builder.build());
  executable.start();
}
 
Example #6
Source File: EmbeddedMongo.java    From spring-data-examples with Apache License 2.0 6 votes vote down vote up
/**
 * Create a default {@code mongos} config.
 *
 * @param version
 * @param port
 * @param cmdOptions
 * @param configServerReplicaSet
 * @param configServerPort
 * @return
 */
private static IMongosConfig defaultMongosConfig(IFeatureAwareVersion version, int port, IMongoCmdOptions cmdOptions,
		String configServerReplicaSet, int configServerPort) {

	try {

		MongosConfigBuilder builder = new MongosConfigBuilder() //
				.version(version) //
				.withLaunchArgument("--quiet", null) //
				.net(new Net(LOCALHOST, port, Network.localhostIsIPv6())) //
				.cmdOptions(cmdOptions);

		if (StringUtils.hasText(configServerReplicaSet)) {
			builder = builder.replicaSet(configServerReplicaSet) //
					.configDB(LOCALHOST + ":" + configServerPort);
		}

		return builder.build();
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example #7
Source File: EmbeddedMongoDb.java    From render with GNU General Public License v2.0 6 votes vote down vote up
public void importCollection(final String collectionName,
                             final File jsonFile,
                             final Boolean jsonArray,
                             final Boolean upsert,
                             final Boolean drop) throws IOException {

    final IMongoImportConfig mongoImportConfig = new MongoImportConfigBuilder()
            .version(version)
            .net(new Net(port, Network.localhostIsIPv6()))
            .db(db.getName())
            .collection(collectionName)
            .upsert(upsert)
            .dropCollection(drop)
            .jsonArray(jsonArray)
            .importFile(jsonFile.getAbsolutePath())
            .build();

    final MongoImportExecutable mongoImportExecutable =
            MongoImportStarter.getInstance(MONGO_IMPORT_RUNTIME_CONFIG).prepare(mongoImportConfig);

    mongoImportExecutable.start();
}
 
Example #8
Source File: EmbeddedMongoDb.java    From render with GNU General Public License v2.0 6 votes vote down vote up
public EmbeddedMongoDb(final String dbName)
        throws IOException {

    this.version = Version.Main.V4_0;
    this.port = 12345;

    // see MongodForTestsFactory for example verbose startup options
    this.mongodExecutable = STARTER.prepare(
            new MongodConfigBuilder()
                    .version(version)
                    .net(new Net(port, Network.localhostIsIPv6()))

                    // use ephemeralForTest storage engine to fix super slow run times on Mac
                    // see https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/166
                    .cmdOptions(new MongoCmdOptionsBuilder().useStorageEngine("ephemeralForTest").build())

                    .build());

    this.mongodProcess = mongodExecutable.start();

    this.mongoClient = new MongoClient("localhost", port);

    this.db = mongoClient.getDatabase(dbName);
}
 
Example #9
Source File: MongoTestBase.java    From vertx-mongo-client with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void startMongo() throws Exception {
  String uri = getConnectionString();
  if (uri == null ) {
    Version.Main version = Version.Main.V3_4;
    int port = 27018;
    System.out.println("Starting Mongo " + version + " on port " + port);
    IMongodConfig config = new MongodConfigBuilder().
      version(version).
      net(new Net(port, Network.localhostIsIPv6())).
      build();
    exe = MongodStarter.getDefaultInstance().prepare(config);
    exe.start();
  } else {
    System.out.println("Using existing Mongo " + uri);
  }
}
 
Example #10
Source File: SimpleEmbedMongo.java    From eagle with Apache License 2.0 5 votes vote down vote up
public void start() throws Exception {
    MongodStarter starter = MongodStarter.getDefaultInstance();
    mongodExe = starter.prepare(new MongodConfigBuilder().version(Version.V3_2_1)
            .net(new Net(27017, Network.localhostIsIPv6())).build());
    mongod = mongodExe.start();

    client = new MongoClient("localhost");
}
 
Example #11
Source File: MyFirstVerticleTest.java    From my-vertx-first-app with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void initialize() throws IOException {
  MongodStarter starter = MongodStarter.getDefaultInstance();

  IMongodConfig mongodConfig = new MongodConfigBuilder()
      .version(Version.Main.PRODUCTION)
      .net(new Net(MONGO_PORT, Network.localhostIsIPv6()))
      .build();

  MongodExecutable mongodExecutable = starter.prepare(mongodConfig);
  MONGO = mongodExecutable.start();
}
 
Example #12
Source File: EmbeddedMongo.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
private static Integer randomOrDefaultServerPort() {

		try {
			return Network.getFreeServerPort();
		} catch (IOException e) {
			return 27017;
		}
	}
 
Example #13
Source File: EmbeddedMongo.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
Integer[] defaultPortsIfRequired(Integer[] ports) {

			if (!ObjectUtils.isEmpty(ports)) {
				return ports;
			}

			try {
				return new Integer[] { Network.getFreeServerPort(), Network.getFreeServerPort(), Network.getFreeServerPort() };
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
 
Example #14
Source File: EmbeddedMongo.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Create a default {@code mongod} config.
 *
 * @param version
 * @param port
 * @param cmdOptions
 * @param configServer
 * @param shardServer
 * @param replicaSet
 * @return
 */
private static IMongodConfig defaultMongodConfig(IFeatureAwareVersion version, int port, IMongoCmdOptions cmdOptions,
		boolean configServer, boolean shardServer, String replicaSet) {

	try {

		MongodConfigBuilder builder = new MongodConfigBuilder() //
				.version(version) //
				.withLaunchArgument("--quiet") //
				.net(new Net(LOCALHOST, port, Network.localhostIsIPv6())) //
				.configServer(configServer).cmdOptions(cmdOptions); //

		if (StringUtils.hasText(replicaSet)) {

			builder = builder //
					.replication(new Storage(null, replicaSet, 0));

			if (!configServer) {
				builder = builder.shardServer(shardServer);
			} else {
				builder = builder.shardServer(false);
			}
		}

		return builder.build();
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example #15
Source File: MongoDb3TestRule.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final String value = Objects.requireNonNull(System.getProperty(portSystemPropertyName),
                    "System property '" + portSystemPropertyName + "' is null");
            final int port = Integer.parseInt(value);
            mongodExecutable = starter.prepare(
            // @formatter:off
                    new MongodConfigBuilder()
                        .version(Version.Main.PRODUCTION)
                        .timeout(new Timeout(BUILDER_TIMEOUT_MILLIS))
                        .net(
                                new Net("localhost", port, Network.localhostIsIPv6()))
                        .build());
            // @formatter:on
            mongodProcess = mongodExecutable.start();
            mongoClient = new MongoClient("localhost", port);
            try {
                base.evaluate();
            } finally {
                if (mongodProcess != null) {
                    mongodProcess.stop();
                    mongodProcess = null;
                }
                if (mongodExecutable != null) {
                    mongodExecutable.stop();
                    mongodExecutable = null;
                }
            }
        }
    };
}
 
Example #16
Source File: MongoDBITBase.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public void startDB() throws Exception {
    MongodStarter starter = MongodStarter.getDefaultInstance();

    String bindIp = "localhost";
    int port = 27018;

    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(bindIp, port, Network.localhostIsIPv6()))
            .build();

    MongodExecutable mongodExecutable = null;

    mongodExecutable = starter.prepare(mongodConfig);

    //give time for previous DB close to finish and port to be released"
    Thread.sleep(200L);
    mongod = mongodExecutable.start();
    setClient();
}
 
Example #17
Source File: MongoDBSessionDataStorageTest.java    From pippo with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUpClass() throws IOException {
    Integer port = Network.getFreeServerPort();
    MongodStarter starter = MongodStarter.getDefaultInstance();
    mongodExe = starter.prepare(
            new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(port, Network.localhostIsIPv6()))
            .build());
    mongod = mongodExe.start();
    mongoClient = new MongoClient(HOST, port);
}
 
Example #18
Source File: ErrorLogsCounterManualTest.java    From tutorials with MIT License 5 votes vote down vote up
private MongoTemplate initMongoTemplate() throws IOException {
    mongodExecutable = starter.prepare(new MongodConfigBuilder()
      .version(Version.Main.PRODUCTION)
      .net(new Net(SERVER, PORT, Network.localhostIsIPv6()))
      .build());
    mongoDaemon = mongodExecutable.start();

    MongoClient mongoClient = new MongoClient(SERVER, PORT);
    db = mongoClient.getDatabase(DB_NAME);

    return new MongoTemplate(mongoClient, DB_NAME);
}
 
Example #19
Source File: EmbeddedMongoDBSetup.java    From tutorials with MIT License 5 votes vote down vote up
private void initdb() throws IOException {
    _mongodExe = starter.prepare(new MongodConfigBuilder()
            .version(Version.Main.DEVELOPMENT)
            .net(new Net(MONGODB_HOST, MONGODB_PORT, Network.localhostIsIPv6()))
            .build());
    _mongod = _mongodExe.start();
}
 
Example #20
Source File: MongoDbInit.java    From tutorials with MIT License 5 votes vote down vote up
public static void startMongoDb() throws IOException {
    _mongodExe = starter.prepare(new MongodConfigBuilder()
            .version(Version.Main.DEVELOPMENT)
            .net(new Net("localhost", 27017, Network.localhostIsIPv6()))
            .build());
    _mongod = _mongodExe.start();
}
 
Example #21
Source File: ManualEmbeddedMongoDbIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@BeforeEach
void setup() throws Exception {
    String ip = "localhost";
    int randomPort = SocketUtils.findAvailableTcpPort();

    IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION)
        .net(new Net(ip, randomPort, Network.localhostIsIPv6()))
        .build();

    MongodStarter starter = MongodStarter.getDefaultInstance();
    mongodExecutable = starter.prepare(mongodConfig);
    mongodExecutable.start();
    mongoTemplate = new MongoTemplate(new MongoClient(ip, randomPort), "test");
}
 
Example #22
Source File: MongoDb4TestRule.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final String value = Objects.requireNonNull(System.getProperty(portSystemPropertyName),
                    "System property '" + portSystemPropertyName + "' is null");
            final int port = Integer.parseInt(value);
            mongodExecutable = starter.prepare(
            // @formatter:off
                    new MongodConfigBuilder().version(Version.Main.PRODUCTION)
                            .timeout(new Timeout(BUILDER_TIMEOUT_MILLIS))
                            .net(new Net("localhost", port, Network.localhostIsIPv6())).build());
            // @formatter:on
            mongodProcess = mongodExecutable.start();
            mongoClient = MongoClients.create("mongodb://localhost:" + port);
            try {
                base.evaluate();
            } finally {
                if (mongodProcess != null) {
                    mongodProcess.stop();
                    mongodProcess = null;
                }
                if (mongodExecutable != null) {
                    mongodExecutable.stop();
                    mongodExecutable = null;
                }
            }
        }
    };
}
 
Example #23
Source File: WithEmbeddedMongo.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
@BeforeClass
static void setUpMongo() throws IOException {
    MongodStarter starter = MongodStarter.getDefaultInstance();

    String bindIp = "localhost";
    int port = 27017;
    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.DEVELOPMENT)
            .net(new Net(bindIp, port, Network.localhostIsIPv6()))
            .build();

    MongodExecutable mongodExecutable = starter.prepare(mongodConfig);
    MONGO_HOLDER.set(mongodExecutable);
    mongodExecutable.start();
}
 
Example #24
Source File: ReactiveMongoClientTestConfiguration.java    From mongodb-aggregate-query-support with Apache License 2.0 5 votes vote down vote up
private IMongodConfig newMongodConfig(final IFeatureAwareVersion version) throws IOException {
  MongoCmdOptionsBuilder builder = new MongoCmdOptionsBuilder().useSmallFiles(false).useNoPrealloc(false);
  return new MongodConfigBuilder().version(version)
                                  .cmdOptions(builder.build())
                                  .net(new Net(LOCALHOST, Network.getFreeServerPort(),
                                                                Network.localhostIsIPv6())).build();
}
 
Example #25
Source File: MongoTestBase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void startMongoDatabase() throws IOException {
    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);
        IMongodConfig config = new MongodConfigBuilder()
                .version(version)
                .net(new Net(port, Network.localhostIsIPv6()))
                .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();
        }
    } else {
        LOGGER.infof("Using existing Mongo %s", uri);
    }
}
 
Example #26
Source File: MongoWithReplicasTestBase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static IMongodConfig buildMongodConfiguration(String url, int port, final boolean configureReplicaSet)
        throws IOException {
    final MongodConfigBuilder builder = new MongodConfigBuilder()
            .version(Version.Main.V4_0)
            .net(new Net(url, port, Network.localhostIsIPv6()));
    if (configureReplicaSet) {
        builder.withLaunchArgument("--replSet", "test001");
        builder.cmdOptions(new MongoCmdOptionsBuilder()
                .syncDelay(10)
                .useSmallFiles(true)
                .useNoJournal(false)
                .build());
    }
    return builder.build();
}
 
Example #27
Source File: MongoTestBase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void startMongoDatabase() throws IOException {
    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);
        IMongodConfig config = new MongodConfigBuilder()
                .version(version)
                .net(new Net(port, Network.localhostIsIPv6()))
                .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();
        }
    } else {
        LOGGER.infof("Using existing Mongo %s", uri);
    }
}
 
Example #28
Source File: MongoWithReplicasTestBase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static IMongodConfig buildMongodConfiguration(String url, int port, final boolean configureReplicaSet)
        throws IOException {
    final MongodConfigBuilder builder = new MongodConfigBuilder()
            .version(Version.Main.V4_0)
            .net(new Net(url, port, Network.localhostIsIPv6()));
    if (configureReplicaSet) {
        builder.withLaunchArgument("--replSet", "test001");
        builder.cmdOptions(new MongoCmdOptionsBuilder()
                .syncDelay(10)
                .useSmallFiles(true)
                .useNoJournal(false)
                .build());
    }
    return builder.build();
}
 
Example #29
Source File: AbstractMongoDbTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void startEmbeddedMongoDb() throws IOException {
    MongodStarter starter = getDefaultInstance();

    port = Network.getFreeServerPort();

    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(HOST, port, Network.localhostIsIPv6()))
            .build();
    mongodExecutable = starter.prepare(mongodConfig);
    mongodExecutable.start();
}
 
Example #30
Source File: EmbedMongoConfiguration.java    From syndesis with Apache License 2.0 5 votes vote down vote up
static IMongodConfig createEmbeddedMongoConfiguration() {
    try {
        final Path storagePath = Files.createTempDirectory("embeddeddmongo");
        storagePath.toFile().deleteOnExit();
        return new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(PORT, Network.localhostIsIPv6()))
            .replication(new Storage(storagePath.toString(),
                REPLICA_SET, 5000))
            .stopTimeoutInMillis(10000)
            .build();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}