Skip to content

Commit

Permalink
FEAT: Add random port maaping.
Browse files Browse the repository at this point in the history
  • Loading branch information
brido4125 committed Jun 13, 2024
1 parent 4a9c2b0 commit 61cd7b7
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 233 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.jam2in.arcus.testcontainers;

import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.PortBinding;
import com.github.dockerjava.api.model.Ports;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;

import org.testcontainers.containers.GenericContainer;
Expand Down Expand Up @@ -71,11 +68,11 @@ public class ArcusClusterContainer extends GenericContainer<ArcusClusterContaine

private ArcusClusterContainer(DockerImageName imageName, ArcusContainerProps props) {
this.network = Network.newNetwork();
this.zkContainer = new ZookeeperContainer(network, props.getZkPort());
this.zkContainer = new ZookeeperContainer(network);
this.serviceCode = props.getServiceCode();

for (int i = 0; i < props.getClusterSize(); i++) {
String address = "cache" + (i + 1) + ":" + (props.getPort() + i);
String address = "cache" + (i + 1) + ":" + getPort();
cacheNodes.add(address);
containers.add(new ArcusContainer(imageName, address, network, props.getMemorySize()));
}
Expand Down Expand Up @@ -122,6 +119,19 @@ public static ArcusClusterContainer create(DockerImageName imageName, ArcusConta
return new ArcusClusterContainer(imageName, props);
}

/**
* invoke for creating ArcusClientPool.
* @return a cluster zk host posts address.
*/
public String getHostPorts() {
return "127.0.0.1" + ":" + getFirstMappedPort();
}

@Override
public Integer getFirstMappedPort() {
return zkContainer.getFirstMappedPort();
}

@Override
public void start() {
zkContainer.start();
Expand Down Expand Up @@ -170,19 +180,27 @@ private void createZnode() throws IOException, InterruptedException {
}
}

static int getPort() {
try (ServerSocket socket = new ServerSocket(0)) {
socket.setReuseAddress(true);
return socket.getLocalPort();
} catch (IOException e) {
throw new RuntimeException("memcached node port exception.", e);
}
}

/**
* This class represents a Zookeeper container that extends the GenericContainer class.
*/
private static class ZookeeperContainer extends GenericContainer<ZookeeperContainer> {
private static final DockerImageName DEFAULT_ZK_IMAGE_NAME = DockerImageName.parse("zookeeper:3.5.9");

public ZookeeperContainer(Network network, int port) {
public ZookeeperContainer(Network network) {
super(DEFAULT_ZK_IMAGE_NAME);
withNetwork(network);
withEnv("ZOO_MY_ID", "1");
withCreateContainerCmdModifier(cmd -> {
cmd.withHostName("zoo1");
cmd.getHostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(port), new ExposedPort(DEFAULT_ZK_CONTAINER_PORT)));
});
withExposedPorts(DEFAULT_ZK_CONTAINER_PORT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ArcusContainer extends GenericContainer<ArcusContainer> {

private ArcusContainer(DockerImageName dockerImageName, ArcusContainerProps props) {
super(dockerImageName);
setupContainer(props.getPort(), props.getMemorySize());
setupContainer(ArcusClusterContainer.getPort(), props.getMemorySize());
}

ArcusContainer(DockerImageName imageName, String address, Network network, int memSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ public class ArcusContainerProps {
private final String serviceCode;
private final int clusterSize;
private final int memorySize;
private final int port;
private final int zkPort;

protected ArcusContainerProps(Builder builder) {
this.serviceCode = builder.serviceCode;
this.clusterSize = builder.clusterSize;
this.memorySize = builder.memorySize;
this.port = builder.port;
this.zkPort = builder.zkPort;
}

public String getServiceCode() {
Expand All @@ -31,13 +27,6 @@ public int getMemorySize() {
return memorySize;
}

public int getPort() {
return port;
}

public int getZkPort() {
return zkPort;
}

/**
* A builder class for creating an instance of ArcusContainerProps with custom properties.
Expand All @@ -46,8 +35,6 @@ public static class Builder {
private String serviceCode = "test";
private int clusterSize = 3;
private int memorySize = 64;
private int port = 11211;
private int zkPort = 2181;

/**
* Sets the service code for configuring an instance of ArcusContainerProps.
Expand Down Expand Up @@ -94,37 +81,6 @@ public Builder memorySize(int memorySize) {
return this;
}

/**
* Sets the port for configuring an instance of ArcusContainerProps.
*
* @param port The port to be set. Must be greater than 0.
* @return The Builder object.
* @throws IllegalArgumentException If the port is smaller than or equal to 0.
*/
public Builder port(int port) {
if (port < 0 || port > 65535) {
throw new IllegalArgumentException("Invalid port number.");
}
this.port = port;
return this;
}

/**
* Sets the ZooKeeper port for configuring an instance of ArcusContainerProps.
* Only using with cluster option.
*
* @param zkPort The ZooKeeper port to be set. Must be greater than 0.
* @return The Builder object.
* @throws IllegalArgumentException If the zkPort is smaller than or equal to 0.
*/
public Builder zkPort(int zkPort) {
if (zkPort < 0 || zkPort > 65535) {
throw new IllegalArgumentException("Invalid zookeeper port number.");
}
this.zkPort = zkPort;
return this;
}

public ArcusContainerProps build() {
return new ArcusContainerProps(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,45 @@

import org.junit.jupiter.api.Test;

import org.testcontainers.utility.DockerImageName;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.assertj.core.api.Assertions.assertThat;

@Testcontainers
class ArcusClusterContainerTest extends ArcusClusterTestBase {

@Container
private final ArcusClusterContainer clusterContainer = ArcusClusterContainer.create(
ArcusContainerProps.builder().build());

@Test
void createArcusContainerSingle() throws ExecutionException, InterruptedException {
//given
ArcusClientPool arcusClient = ArcusClient.createArcusClientPool("test", new ConnectionFactoryBuilder(), 2);
ArcusClientPool arcusClient = ArcusClient.createArcusClientPool(ARCUS_CLUSTER_CONTAINER.getHostPorts(),
"test", new ConnectionFactoryBuilder(), 2);

//when
OperationFuture<Boolean> set = arcusClient.set("test", 10, "testValue");

//then
assertThat(ARCUS_CLUSTER_CONTAINER.isCreated()).isTrue();
assertThat(ARCUS_CLUSTER_CONTAINER.isRunning()).isTrue();
// assertThat(ARCUS_CLUSTER_CONTAINER.isHealthy()).isTrue();
assertThat(set.get()).isTrue();
}

@Test
void testCreateMethodWithoutParams() {
//when
ArcusClusterContainer container = ArcusClusterContainer.create();
//then
assertThat(container).isNotNull();
}

@Test
void testCreateMethodWithPropsParam(){
void createNotSharedClusterTest() throws ExecutionException, InterruptedException {
//given
ArcusContainerProps props = new ArcusContainerProps.Builder().build();
//when
ArcusClusterContainer container = ArcusClusterContainer.create(props);
//then
assertThat(container).isNotNull();
}
ArcusClientPool arcusClient = ArcusClient.createArcusClientPool(clusterContainer.getHostPorts(),
"test", new ConnectionFactoryBuilder(), 2);

@Test
void testCreateMethodWithImageNameParam() {
//given
DockerImageName imageName = DockerImageName.parse("jam2in/arcus-memcached:develop");
//when
ArcusClusterContainer container = ArcusClusterContainer.create(imageName);
//then
assertThat(container).isNotNull();
}
OperationFuture<Boolean> set = arcusClient.set("test", 10, "testValue");

@Test
void testCreateMethodWithImageNameAndPropsParams() {
//given
DockerImageName imageName = DockerImageName.parse("jam2in/arcus-memcached:develop");
ArcusContainerProps props = new ArcusContainerProps.Builder().build();
//when
ArcusClusterContainer container = ArcusClusterContainer.create(imageName, props);
//then
assertThat(container).isNotNull();
assertThat(clusterContainer.isCreated()).isTrue();
assertThat(clusterContainer.isRunning()).isTrue();
assertThat(set.get()).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class ArcusClusterContainerTestOther extends ArcusClusterTestBase {
@Test
void createArcusContainerSingle() throws ExecutionException, InterruptedException {
//given
ArcusClientPool arcusClient = ArcusClient.createArcusClientPool("test", new ConnectionFactoryBuilder(), 2);
ArcusClientPool arcusClient = ArcusClient.createArcusClientPool(ARCUS_CLUSTER_CONTAINER.getHostPorts(),
"test", new ConnectionFactoryBuilder(), 2);

//when
OperationFuture<Boolean> set = arcusClient.set("test2", 10, "testValue2");
Expand Down

This file was deleted.

Loading

0 comments on commit 61cd7b7

Please sign in to comment.