Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove choosePort and rely on dynamic, OS-allocated ports during tests #414

Open
wants to merge 1 commit into
base: 3.0.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,6 @@ public abstract class ClusterTestHarness {

public static final int DEFAULT_NUM_BROKERS = 1;

/**
* Choose a number of random available ports
*/
public static int[] choosePorts(int count) {
try {
ServerSocket[] sockets = new ServerSocket[count];
int[] ports = new int[count];
for (int i = 0; i < count; i++) {
sockets[i] = new ServerSocket(0);
ports[i] = sockets[i].getLocalPort();
}
for (int i = 0; i < count; i++)
sockets[i].close();
return ports;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* Choose an available port
*/
public static int choosePort() {
return choosePorts(1)[0];
}


private int numBrokers;
private boolean withSchemaRegistry;

Expand Down Expand Up @@ -164,30 +137,26 @@ public void setUp() throws Exception {
SecurityProtocol.PLAINTEXT);

if (withSchemaRegistry) {
int schemaRegPort = choosePort();
schemaRegProperties.put(SchemaRegistryConfig.PORT_CONFIG,
((Integer) schemaRegPort).toString());
schemaRegProperties.put(SchemaRegistryConfig.PORT_CONFIG, "0");
schemaRegProperties.put(SchemaRegistryConfig.KAFKASTORE_CONNECTION_URL_CONFIG,
zkConnect);
schemaRegProperties.put(SchemaRegistryConfig.KAFKASTORE_TOPIC_CONFIG,
SchemaRegistryConfig.DEFAULT_KAFKASTORE_TOPIC);
schemaRegProperties.put(SchemaRegistryConfig.COMPATIBILITY_CONFIG,
schemaRegCompatibility);
schemaRegConnect = String.format("http://localhost:%d", schemaRegPort);

schemaRegApp =
new SchemaRegistryRestApplication(new SchemaRegistryConfig(schemaRegProperties));
schemaRegServer = schemaRegApp.createServer();
schemaRegServer.start();
schemaRegConnect = String.format("http://localhost:%d", schemaRegApp.localPorts().get(0));
}

int restPort = choosePort();
restProperties.put(KafkaRestConfig.PORT_CONFIG, ((Integer) restPort).toString());
restProperties.put(KafkaRestConfig.PORT_CONFIG, "0");
restProperties.put(KafkaRestConfig.ZOOKEEPER_CONNECT_CONFIG, zkConnect);
if (withSchemaRegistry) {
restProperties.put(KafkaRestConfig.SCHEMA_REGISTRY_URL_CONFIG, schemaRegConnect);
}
restConnect = String.format("http://localhost:%d", restPort);

restConfig = new KafkaRestConfig(restProperties);
restApp = new TestKafkaRestApplication(restConfig, getZkUtils(restConfig),
Expand All @@ -198,6 +167,7 @@ public void setUp() throws Exception {
getSimpleConsumerManager(restConfig));
restServer = restApp.createServer();
restServer.start();
restConnect = String.format("http://localhost:%d", restApp.localPorts().get(0));
}

protected ZkUtils getZkUtils(KafkaRestConfig appConfig) {
Expand Down