Skip to content

Commit

Permalink
use a random free port in KsqlRestClientTest (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
dguy authored Nov 14, 2017
1 parent 9296fe4 commit b37f144
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -32,18 +34,19 @@
import io.confluent.ksql.rest.server.KsqlRestConfig;
import io.confluent.ksql.rest.server.computation.CommandId;
import io.confluent.ksql.rest.server.mock.MockApplication;
import io.confluent.ksql.rest.server.utils.TestUtils;

public class KsqlRestClientTest {

MockApplication mockApplication;
int portNumber = 59098;
KsqlRestConfig ksqlRestConfig;
KsqlRestClient ksqlRestClient;

@Before
public void init() throws Exception {
final int port = TestUtils.randomFreeLocalPort();
Map<String, Object> props = new HashMap<>();
props.put(KsqlRestConfig.LISTENERS_CONFIG, "http://localhost:59098");
props.put(KsqlRestConfig.LISTENERS_CONFIG, "http://localhost:" + port);
// props.put(KsqlRestConfig.PORT_CONFIG, String.valueOf(portNumber));
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "ksql_config_test");
Expand All @@ -52,7 +55,7 @@ public void init() throws Exception {
mockApplication = new MockApplication(ksqlRestConfig);
mockApplication.start();

ksqlRestClient = new KsqlRestClient("http://localhost:" + portNumber);
ksqlRestClient = new KsqlRestClient("http://localhost:" + port);
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,19 @@

package io.confluent.ksql.rest.server.utils;

import org.apache.kafka.clients.consumer.ConsumerRecord;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.confluent.ksql.rest.server.computation.Command;
import io.confluent.ksql.rest.server.computation.CommandId;
import io.confluent.ksql.util.KsqlConfig;
import io.confluent.ksql.util.Pair;

public class TestUtils {

public static KsqlConfig getMockKsqlConfig() {
Map<String, Object> props = new HashMap<>();
props.put("application.id", "ksqlStatementExecutorTest");
props.put("bootstrap.servers", "localhost:9092");
return new KsqlConfig(props);
}

public List<Pair<CommandId, Command>> getAllPriorCommandRecords() {
List<Pair<CommandId, Command>> priorCommands = new ArrayList<>();

Expand Down Expand Up @@ -74,4 +65,11 @@ public List<Pair<CommandId, Command>> getAllPriorCommandRecords() {

return priorCommands;
}

public static int randomFreeLocalPort() throws IOException {
ServerSocket s = new ServerSocket(0);
int port = s.getLocalPort();
s.close();
return port;
}
}

0 comments on commit b37f144

Please sign in to comment.