private static Integer randomOrDefaultServerPort() { try { return Network.getFreeServerPort(); } catch (IOException e) { return 27017; } }
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); } }
public Net() throws UnknownHostException, IOException { this(Network.getFreeServerPort()); }
public static int getPreferredFreeServerPort(InetAddress hostAddress, int preferredPort) throws IOException { try (ServerSocket serverSocket = new ServerSocket(preferredPort, 0, hostAddress)) { return preferredPort; } catch (IOException e) { return getFreeServerPort(hostAddress); } }
private int getPort() { if (port == null) { try { port = Network.getFreeServerPort(); } catch (IOException ex) { LOG.error("Could not get free server port"); } } return port; }
@Override public void before() throws Throwable { LOG.info("Graphite Mock before..."); server = ServerSocketChannel.open(); server.configureBlocking(false); port = Network.getFreeServerPort(); LOG.info("GraphiteMockServer starting : port={}", port); server.socket().bind(new InetSocketAddress(port)); selector = Selector.open(); server.register(selector, SelectionKey.OP_ACCEPT); thread = new Thread(this); thread.start(); LOG.info("... done Graphite Mock before"); }
public Net() throws IOException { this(null, Network.getFreeServerPort(), Network.localhostIsIPv6()); }
public Net() throws IOException { this(getLocalHost().getHostAddress(), getFreeServerPort()); }
public static int getFreeServerPort() throws IOException { return getFreeServerPort(getLocalHost()); }
public static int getFreeServerPort() throws IOException { return getFreeServerPort(getLocalHost()); }
@Override public void before() throws Throwable { server = ServerSocketChannel.open(); server.configureBlocking(false); int maxTries = 3; int sleepInSec = 2; for (int i = 0; i < maxTries; i++) { try { port = Network.getFreeServerPort(); server.socket().bind(new InetSocketAddress(port)); LOG.info("StatsdMockServer started : port={}", port); } catch (Throwable e) { int lastTry = maxTries - 1; if (i < lastTry) { LOG.warn(format("Starting StatsdMockServer: try %s of %s failed. Retry after %s seconds.", i + 1, maxTries, sleepInSec), e); sleep(sleepInSec * 1000); } else { LOG.error(format("Starting StatsdMockServer: %s of %s failed. No retries left.", i + 1, maxTries)); throw e; } } } selector = Selector.open(); server.register(selector, SelectionKey.OP_ACCEPT); thread = new Thread(this); thread.start(); }
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; } }
@BeforeClass public static void setUpClass() throws Exception { RuntimeConfig config = RuntimeConfig.getInstance(LOGGER); MongodStarter runtime = MongodStarter.getInstance(config); int freeServerPort = Network.getFreeServerPort(); mongodExecutable = runtime.prepare(new MongodConfig(Version.Main.V2_3, freeServerPort, Network.localhostIsIPv6())); mongodProcess = mongodExecutable.start(); binaryStore = new MongodbBinaryStore("localhost", freeServerPort, "test-" + UUID.randomUUID(), null, null, null); binaryStore.setMimeTypeDetector(DEFAULT_DETECTOR); binaryStore.start(); }
public synchronized void start() throws IOException { if (process != null) { throw new IllegalStateException(); } Command command = Command.Postgres; IDownloadConfig downloadConfig = new PostgresDownloadConfigBuilder() .defaultsForCommand(command) .artifactStorePath(new FixedPath(artifactStorePath)) .build(); ArtifactStoreBuilder artifactStoreBuilder = new PostgresArtifactStoreBuilder() .defaults(command) .download(downloadConfig); LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor("started", new HashSet<>(singletonList("failed")), new Slf4jStreamProcessor(getLogger("postgres"), Slf4jLevel.TRACE)); IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder() .defaults(command) .processOutput(new ProcessOutput(logWatch, logWatch, logWatch)) .artifactStore(artifactStoreBuilder) .build(); PostgresStarter<PostgresExecutable, PostgresProcess> starter = new PostgresStarter<>(PostgresExecutable.class, runtimeConfig); PostgresConfig config = new PostgresConfig(version, new AbstractPostgresConfig.Net(host, port == 0 ? Network.getFreeServerPort() : port), new AbstractPostgresConfig.Storage(dbName), new AbstractPostgresConfig.Timeout(), new AbstractPostgresConfig.Credentials(username, password)); process = starter.prepare(config).start(); jdbcUrl = "jdbc:postgresql://" + config.net().host() + ":" + config.net().port() + "/" + config.storage().dbName(); }