Java Code Examples for org.apache.curator.test.InstanceSpec#getRandomPort()

The following examples show how to use org.apache.curator.test.InstanceSpec#getRandomPort() . 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: TestStringsWithRestEasy.java    From curator with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
public void         setup() throws Exception
{
    RestEasyApplication.singletonsRef.set(new RestEasySingletons());

    ResteasyProviderFactory.setInstance(new ResteasyProviderFactory());

    HttpServletDispatcher   dispatcher = new HttpServletDispatcher();

    port = InstanceSpec.getRandomPort();
    server = new Server(port);
    Context root = new Context(server, "/", Context.SESSIONS);
    root.getInitParams().put("javax.ws.rs.Application", RestEasyApplication.class.getName());
    root.addServlet(new ServletHolder(dispatcher), "/*");
    root.addEventListener(new ResteasyBootstrap());
    server.start();
}
 
Example 2
Source File: KafkaEmbedded.java    From eagle with Apache License 2.0 6 votes vote down vote up
public KafkaEmbedded(Integer kafkaPort, Integer zookeeperPort) {
    try {
        zk = new ZookeeperEmbedded(zookeeperPort);
        zk.start();

        this.port = null != kafkaPort ? kafkaPort : InstanceSpec.getRandomPort();
        logDir = new File(System.getProperty("java.io.tmpdir"), "kafka/logs/kafka-test-" + kafkaPort);
        FileUtils.deleteQuietly(logDir);

        KafkaConfig config = buildKafkaConfig(zk.getConnectionString());
        kafka = new KafkaServerStartable(config);
        kafka.startup();
    } catch (Exception ex) {
        throw new RuntimeException("Could not start test broker", ex);
    }
}
 
Example 3
Source File: TestRemoteInstanceRequestClient.java    From exhibitor with Apache License 2.0 6 votes vote down vote up
@Test
public void     testConnectionTimeout() throws Exception
{
    int             port = InstanceSpec.getRandomPort();

    RemoteInstanceRequestClientImpl client = null;
    ServerSocket                    server = new ServerSocket(port, 0);
    try
    {
        client = new RemoteInstanceRequestClientImpl(new RemoteConnectionConfiguration());
        client.getWebResource(new URI("http://localhost:" + port), MediaType.WILDCARD_TYPE, Object.class);
    }
    catch ( Exception e )
    {
        Throwable cause = e.getCause();
        Assert.assertTrue(cause instanceof SocketTimeoutException);
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
        server.close();
    }
}
 
Example 4
Source File: TestRemoteInstanceRequestClient.java    From exhibitor with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "pass")
public void testHttpsPass(HttpsConfiguration httpsConf) throws Exception
{
    int port = InstanceSpec.getRandomPort();
    JettyServer server = new JettyServer(port, httpsConf);
    server.start();

    RemoteInstanceRequestHttpsClientImpl client = null;
    try
    {
        client = new RemoteInstanceRequestHttpsClientImpl(new RemoteConnectionConfiguration(), httpsConf);
        client.getWebResource(new URI("https://localhost:" + port + "/test"), MediaType.TEXT_PLAIN_TYPE, String.class);
    }
    finally
    {
        client.close();
        server.stop();
    }
}
 
Example 5
Source File: TestRemoteInstanceRequestClient.java    From exhibitor with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "fail", expectedExceptions = {Exception.class})
public void testHttpsFail(HttpsConfiguration httpsConf) throws Exception
{
    int port = InstanceSpec.getRandomPort();
    JettyServer server = new JettyServer(port, httpsConf);
    server.start();

    RemoteInstanceRequestHttpsClientImpl client = null;
    try
    {
        client = new RemoteInstanceRequestHttpsClientImpl(new RemoteConnectionConfiguration(), httpsConf);
        client.getWebResource(new URI("https://localhost:" + port + "/test"), MediaType.TEXT_PLAIN_TYPE, String.class);
    }
    finally
    {
        client.close();
        server.stop();
    }
}
 
Example 6
Source File: TestStringsWithJersey.java    From curator with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void         setup() throws Exception
{
    context = new StringDiscoveryContext(new MockServiceDiscovery<String>(), new RandomStrategy<String>(), 1000);
    serviceNamesMarshaller = new JsonServiceNamesMarshaller();
    serviceInstanceMarshaller = new JsonServiceInstanceMarshaller<String>(context);
    serviceInstancesMarshaller = new JsonServiceInstancesMarshaller<String>(context);

    Application                                     application = new DefaultResourceConfig()
    {
        @Override
        public Set<Class<?>> getClasses()
        {
            Set<Class<?>>       classes = Sets.newHashSet();
            classes.add(StringDiscoveryResource.class);
            return classes;
        }

        @Override
        public Set<Object> getSingletons()
        {
            Set<Object>     singletons = Sets.newHashSet();
            singletons.add(context);
            singletons.add(serviceNamesMarshaller);
            singletons.add(serviceInstanceMarshaller);
            singletons.add(serviceInstancesMarshaller);
            return singletons;
        }
    };
    ServletContainer        container = new ServletContainer(application);

    port = InstanceSpec.getRandomPort();
    server = new Server(port);
    Context root = new Context(server, "/", Context.SESSIONS);
    root.addServlet(new ServletHolder(container), "/*");
    server.start();
}
 
Example 7
Source File: TestObjectPayloadWithJersey.java    From curator with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void         setup() throws Exception
{
    context = new ServiceDetailsDiscoveryContext(new MockServiceDiscovery<ServiceDetails>(), new RandomStrategy<ServiceDetails>(), 1000);
    serviceNamesMarshaller = new JsonServiceNamesMarshaller();
    serviceInstanceMarshaller = new JsonServiceInstanceMarshaller<ServiceDetails>(context);
    serviceInstancesMarshaller = new JsonServiceInstancesMarshaller<ServiceDetails>(context);

    Application                                     application = new DefaultResourceConfig()
    {
        @Override
        public Set<Class<?>> getClasses()
        {
            Set<Class<?>>       classes = Sets.newHashSet();
            classes.add(ServiceDetailsDiscoveryResource.class);
            return classes;
        }

        @Override
        public Set<Object> getSingletons()
        {
            Set<Object>     singletons = Sets.newHashSet();
            singletons.add(context);
            singletons.add(serviceNamesMarshaller);
            singletons.add(serviceInstanceMarshaller);
            singletons.add(serviceInstancesMarshaller);
            return singletons;
        }
    };
    ServletContainer        container = new ServletContainer(application);

    port = InstanceSpec.getRandomPort();
    server = new Server(port);
    Context root = new Context(server, "/", Context.SESSIONS);
    root.addServlet(new ServletHolder(container), "/*");
    server.start();
}
 
Example 8
Source File: TestMapsWithJersey.java    From curator with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void         setup() throws Exception
{
    context = new MapDiscoveryContext(new MockServiceDiscovery<Map<String, String>>(), new RandomStrategy<Map<String, String>>(), 1000);
    serviceNamesMarshaller = new JsonServiceNamesMarshaller();
    serviceInstanceMarshaller = new JsonServiceInstanceMarshaller<Map<String, String>>(context);
    serviceInstancesMarshaller = new JsonServiceInstancesMarshaller<Map<String, String>>(context);

    Application                                     application = new DefaultResourceConfig()
    {
        @Override
        public Set<Class<?>> getClasses()
        {
            Set<Class<?>>       classes = Sets.newHashSet();
            classes.add(MapDiscoveryResource.class);
            return classes;
        }

        @Override
        public Set<Object> getSingletons()
        {
            Set<Object>     singletons = Sets.newHashSet();
            singletons.add(context);
            singletons.add(serviceNamesMarshaller);
            singletons.add(serviceInstanceMarshaller);
            singletons.add(serviceInstancesMarshaller);
            return singletons;
        }
    };
    ServletContainer        container = new ServletContainer(application);

    port = InstanceSpec.getRandomPort();
    server = new Server(port);
    Context root = new Context(server, "/", Context.SESSIONS);
    root.addServlet(new ServletHolder(container), "/*");
    server.start();
}
 
Example 9
Source File: EphemeralKafkaBrokerTest.java    From kafka-junit with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartAndStop() throws Exception {
    int kafkaPort = InstanceSpec.getRandomPort();
    int zkPort = InstanceSpec.getRandomPort();
    final EphemeralKafkaBroker broker = EphemeralKafkaBroker.create(kafkaPort, zkPort);
    CompletableFuture<Void> res = broker.start();
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        //Ignore
    }

    assertThat(broker.isRunning()).isTrue();
    assertThat(broker.getKafkaPort().get()).isEqualTo(kafkaPort);
    assertThat(broker.getZookeeperPort().get()).isEqualTo(zkPort);
    assertThat(broker.getBrokerList().isPresent()).isTrue();
    assertThat(broker.getZookeeperConnectString().isPresent()).isTrue();
    assertThat(broker.getLogDir().isPresent()).isTrue();

    Path logDir = Paths.get(broker.getLogDir().get());
    assertThat(Files.exists(logDir)).isTrue();

    broker.stop();
    assertThat(res.isDone()).isTrue();
    assertThat(broker.isRunning()).isFalse();
    assertThat(broker.getBrokerList().isPresent()).isFalse();
    assertThat(broker.getZookeeperConnectString().isPresent()).isFalse();
    assertThat(Files.exists(logDir)).isFalse();
}
 
Example 10
Source File: EphemeralKafkaCluster.java    From kafka-junit with Apache License 2.0 5 votes vote down vote up
private EphemeralKafkaBroker addBroker(Properties overrideBrokerProperties) throws Exception {
    final int brokerPort = InstanceSpec.getRandomPort();
    Properties brokerConfigProperties = new Properties();
    brokerConfigProperties.setProperty(KafkaConfig.BrokerIdProp(), brokers.size() + "");
    brokerConfigProperties.setProperty(KafkaConfig.ZkConnectProp(), zookeeper.getConnectString());
    brokerConfigProperties.setProperty(KafkaConfig.ControlledShutdownEnableProp(), false + "");
    brokerConfigProperties.setProperty(KafkaConfig.ControlledShutdownMaxRetriesProp(), "1");
    brokerConfigProperties.setProperty(KafkaConfig.DeleteTopicEnableProp(), true + "");
    brokerConfigProperties.setProperty(KafkaConfig.PortProp(), "" + brokerPort);
    brokerConfigProperties.setProperty(KafkaConfig.SslEnabledProtocolsProp(), false + "");
    brokerConfigProperties.setProperty(KafkaConfig.AutoCreateTopicsEnableProp(), true + "");
    brokerConfigProperties.setProperty(KafkaConfig.ReplicaSocketTimeoutMsProp(), "300");
    brokerConfigProperties.setProperty(KafkaConfig.ReplicaFetchWaitMaxMsProp(), "100");
    brokerConfigProperties.setProperty(KafkaConfig.ControllerSocketTimeoutMsProp(), "10");

    brokerConfigProperties.setProperty(KafkaConfig.OffsetsTopicReplicationFactorProp(), numBroker + "");
    brokerConfigProperties.setProperty(KafkaConfig.LeaderImbalanceCheckIntervalSecondsProp(), 1 + "");
    brokerConfigProperties.setProperty(KafkaConfig.ZkSessionTimeoutMsProp(), 200 + "");
    brokerConfigProperties.setProperty(KafkaConfig.GroupInitialRebalanceDelayMsDoc(), 200 + "");
    brokerConfigProperties.setProperty(KafkaConfig.AdvertisedHostNameProp(), "localhost");
    brokerConfigProperties.setProperty(KafkaConfig.AdvertisedPortProp(), brokerPort + "");
    brokerConfigProperties.setProperty(KafkaConfig.AdvertisedListenersProp(), "PLAINTEXT://localhost:" + brokerPort);
    brokerConfigProperties.setProperty(KafkaConfig.HostNameProp(), "localhost");
    brokerConfigProperties.setProperty(KafkaConfig.MinInSyncReplicasProp(), Math.max(1, numBroker - 1) + "");
    if(!overrideBrokerProperties.isEmpty()){
        overrideBrokerProperties.forEach((k, v) -> brokerConfigProperties.put(k, v));
    }
    final EphemeralKafkaBroker broker = new EphemeralKafkaBroker(zookeeper, brokerPort, brokerConfigProperties);
    broker.start().get();
    brokers.add(broker);
    return broker;
}
 
Example 11
Source File: KafkaTestClusterTest.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test a cluster instance with listeners on specified ports, where a port is duplicated.
 */
@Test
void testListenerWithSpecificPortRepeated() throws Exception {
    // Explicitly define our port
    final int port1 = InstanceSpec.getRandomPort();
    final int port2 = InstanceSpec.getRandomPort();
    final int port3 = InstanceSpec.getRandomPort();

    // Create plain listeners using the same port.
    final BrokerListener plainListener1 = new PlainListener()
        .onPorts(port1, port2);

    final BrokerListener plainListener2 = new PlainListener()
        .onPorts(port3, port1);

    final List<BrokerListener> listeners = new ArrayList<>();
    listeners.add(plainListener1);
    listeners.add(plainListener2);

    // Define how many brokers.
    final int numberOfBrokers = 2;

    // Speed up shutdown in our tests
    final Properties overrideProperties = getDefaultBrokerOverrideProperties();

    // Create our test server instance
    try (final KafkaTestCluster kafkaTestCluster = new KafkaTestCluster(numberOfBrokers, overrideProperties, listeners)) {

        // Start broker, this should throw an exception
        Assertions.assertThrows(RuntimeException.class, kafkaTestCluster::start);
    }
}
 
Example 12
Source File: AbstractListener.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Internal method to get the next assigned port.  If called more times than configured ports,
 * this method will generate a random port to be used.
 *
 * @return next configured port to use.
 */
public int getNextPort() {
    if (ports == null || ports.length == 0 || portIndex >= ports.length) {
        // Return random Port
        return InstanceSpec.getRandomPort();
    }
    return ports[portIndex++];
}
 
Example 13
Source File: StartStopWithoutInitialQuorumTest.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(CentralDogmaBuilder builder) {
    // Set up a cluster of two replicas where the second replica is always unavailable,
    final int quorumPort = InstanceSpec.getRandomPort();
    final int electionPort = InstanceSpec.getRandomPort();
    final int clientPort = InstanceSpec.getRandomPort();

    builder.replication(new ZooKeeperReplicationConfig(
            1, ImmutableMap.of(1, new ZooKeeperAddress("127.0.0.1",
                                                       quorumPort, electionPort, clientPort),
                               2, new ZooKeeperAddress("127.0.0.1", 1, 1, 1))));
}
 
Example 14
Source File: KafkaEmbedded.java    From eagle with Apache License 2.0 4 votes vote down vote up
public KafkaEmbedded() {
    this(InstanceSpec.getRandomPort(), InstanceSpec.getRandomPort());
}
 
Example 15
Source File: KafkaTestClusterTest.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test a cluster instance with listeners on specified ports.
 */
@Test
void testListenerWithSpecificPort() throws Exception {
    // Explicitly define our port
    final int exportedPort1 = InstanceSpec.getRandomPort();
    final int exportedPort2 = InstanceSpec.getRandomPort();

    // Create default plain listener
    final BrokerListener plainListener = new PlainListener()
        .onPorts(exportedPort1, exportedPort2);
    final List<BrokerListener> listeners = Collections.singletonList(plainListener);

    final String topicName = "TestTopic-" + System.currentTimeMillis();
    final int expectedMsgCount = 2;
    final int numberOfBrokers = 2;

    // Speed up shutdown in our tests
    final Properties overrideProperties = getDefaultBrokerOverrideProperties();

    // Create our test server instance
    try (final KafkaTestCluster kafkaTestCluster = new KafkaTestCluster(numberOfBrokers, overrideProperties, listeners)) {
        // Start broker
        kafkaTestCluster.start();

        // Validate connect string is as expected.
        final String connectString = kafkaTestCluster.getKafkaConnectString();
        final String expectedConnectString = "PLAINTEXT://localhost:" + exportedPort1 + ",PLAINTEXT://localhost:" + exportedPort2;
        Assertions.assertEquals(expectedConnectString, connectString, "Should be using our specified ports");

        // Create KafkaTestUtils
        final KafkaTestUtils kafkaTestUtils = new KafkaTestUtils(kafkaTestCluster);

        // Create topic
        kafkaTestUtils.createTopic(topicName, 1, (short) numberOfBrokers);

        // Publish 2 messages into topic
        kafkaTestUtils.produceRecords(expectedMsgCount, topicName, 0);

        // Sanity test - Consume the messages back out before shutting down broker.
        final List<ConsumerRecord<byte[], byte[]>> records = kafkaTestUtils.consumeAllRecordsFromTopic(topicName);
        Assertions.assertNotNull(records);
        Assertions.assertEquals(expectedMsgCount, records.size(), "Should have found 2 records.");
    }
}
 
Example 16
Source File: KafkaTestServerTest.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test a single server instance with listener on specified port.
 */
@Test
void testListenerWithSpecificPort() throws Exception {
    // Explicitly define our port
    final int exportedPort = InstanceSpec.getRandomPort();

    // Create default plain listener
    final BrokerListener plainListener = new PlainListener()
        .onPorts(exportedPort);

    final String topicName = "TestTopic-" + System.currentTimeMillis();
    final int expectedMsgCount = 2;

    final Properties overrideProperties = getDefaultBrokerOverrideProperties();

    // Create our test server instance
    try (final KafkaTestServer kafkaTestServer = new KafkaTestServer(overrideProperties, Collections.singletonList(plainListener))) {
        // Start broker
        kafkaTestServer.start();

        // Validate connect string is as expected.
        final String connectString = kafkaTestServer.getKafkaConnectString();
        Assertions.assertEquals("PLAINTEXT://localhost:" + exportedPort, connectString, "Should be using our specified port");

        // Now validate that the service functions.
        // Create KafkaTestUtils
        final KafkaTestUtils kafkaTestUtils = new KafkaTestUtils(kafkaTestServer);

        // Create topic
        kafkaTestUtils.createTopic(topicName, 1, (short) 1);

        // Publish 2 messages into topic
        kafkaTestUtils.produceRecords(expectedMsgCount, topicName, 0);

        // Sanity test - Consume the messages back out before shutting down broker.
        final List<ConsumerRecord<byte[], byte[]>> records = kafkaTestUtils.consumeAllRecordsFromTopic(topicName);
        Assertions.assertNotNull(records);
        Assertions.assertEquals(expectedMsgCount, records.size(), "Should have found 2 records.");

        // Call stop/close on the broker
        kafkaTestServer.stop();
    }
}
 
Example 17
Source File: ReplicatedLoginAndLogoutTest.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
@BeforeEach
void setUp() throws Exception {
    final int port1 = InstanceSpec.getRandomPort();
    final int zkQuorumPort1 = InstanceSpec.getRandomPort();
    final int zkElectionPort1 = InstanceSpec.getRandomPort();
    final int zkClientPort1 = InstanceSpec.getRandomPort();

    final int port2 = InstanceSpec.getRandomPort();
    final int zkQuorumPort2 = InstanceSpec.getRandomPort();
    final int zkElectionPort2 = InstanceSpec.getRandomPort();
    final int zkClientPort2 = InstanceSpec.getRandomPort();

    final Map<Integer, ZooKeeperAddress> servers = ImmutableMap.of(
            1, new ZooKeeperAddress("127.0.0.1", zkQuorumPort1, zkElectionPort1, zkClientPort1),
            2, new ZooKeeperAddress("127.0.0.1", zkQuorumPort2, zkElectionPort2, zkClientPort2));

    final AuthProviderFactory factory = new TestAuthProviderFactory();

    replica1 = new CentralDogmaBuilder(tempDir.newFolder().toFile())
            .port(port1, SessionProtocol.HTTP)
            .authProviderFactory(factory)
            .webAppEnabled(true)
            .mirroringEnabled(false)
            .gracefulShutdownTimeout(new GracefulShutdownTimeout(0, 0))
            .replication(new ZooKeeperReplicationConfig(1, servers))
            .build();

    replica2 = new CentralDogmaBuilder(tempDir.newFolder().toFile())
            .port(port2, SessionProtocol.HTTP)
            .authProviderFactory(factory)
            .webAppEnabled(true)
            .mirroringEnabled(false)
            .gracefulShutdownTimeout(new GracefulShutdownTimeout(0, 0))
            .replication(new ZooKeeperReplicationConfig(2, servers))
            .build();

    client1 = WebClient.of("http://127.0.0.1:" + port1);
    client2 = WebClient.of("http://127.0.0.1:" + port2);

    final CompletableFuture<Void> f1 = replica1.start();
    final CompletableFuture<Void> f2 = replica2.start();

    f1.join();
    f2.join();

    curator = CuratorFrameworkFactory.newClient("127.0.0.1:" + zkClientPort1,
                                                new RetryUntilElapsed(10000, 100));
    curator.start();
    assertThat(curator.blockUntilConnected(10, TimeUnit.SECONDS)).isTrue();
}