Skip to content

Commit

Permalink
chore: move container class to impl package
Browse files Browse the repository at this point in the history
  • Loading branch information
octonato committed Dec 21, 2023
1 parent 9b468fa commit 9594bb6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import kalix.javasdk.impl.MessageCodec;
import kalix.javasdk.impl.ProxyInfoHolder;
import kalix.javasdk.testkit.EventingTestKit.IncomingMessages;
import kalix.javasdk.testkit.impl.KalixRuntimeContainer;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -48,8 +49,8 @@
import java.util.function.BiFunction;
import java.util.stream.Collectors;

import static kalix.javasdk.testkit.KalixProxyContainer.DEFAULT_GOOGLE_PUBSUB_PORT;
import static kalix.javasdk.testkit.KalixProxyContainer.DEFAULT_KAFKA_PORT;
import static kalix.javasdk.testkit.impl.KalixRuntimeContainer.DEFAULT_GOOGLE_PUBSUB_PORT;
import static kalix.javasdk.testkit.impl.KalixRuntimeContainer.DEFAULT_KAFKA_PORT;
import static kalix.javasdk.testkit.KalixTestKit.Settings.EventingSupport.GOOGLE_PUBSUB;
import static kalix.javasdk.testkit.KalixTestKit.Settings.EventingSupport.KAFKA;
import static kalix.javasdk.testkit.KalixTestKit.Settings.EventingSupport.TEST_BROKER;
Expand Down Expand Up @@ -433,7 +434,7 @@ public String toString() {
private boolean started = false;
private String proxyHost;
private int proxyPort;
private Optional<KalixProxyContainer> proxyContainer = Optional.empty();
private Optional<KalixRuntimeContainer> runtimeContainer = Optional.empty();
private KalixRunner runner;
private ActorSystem testSystem;
private EventingTestKit eventingTestKit;
Expand Down Expand Up @@ -491,7 +492,7 @@ public KalixTestKit start(final Config config) {
throw new IllegalStateException("KalixTestkit already started");

Boolean useTestContainers = Optional.ofNullable(System.getenv("KALIX_TESTKIT_USE_TEST_CONTAINERS")).map(Boolean::valueOf).orElse(true);
int port = userFunctionPort(useTestContainers);
int port = userServicePort(useTestContainers);
Map<String, Object> conf = new HashMap<>();
conf.put("kalix.user-function-port", port);
// don't kill the test JVM when terminating the KalixRunner
Expand Down Expand Up @@ -537,11 +538,11 @@ private int eventingTestKitPort(Boolean useTestContainers) {
private void runProxy(Boolean useTestContainers, int port, int grpcEventingBackendPort) {

if (useTestContainers) {
var proxyContainer = new KalixProxyContainer(settings.eventingSupport, port, grpcEventingBackendPort);
this.proxyContainer = Optional.of(proxyContainer);
proxyContainer.addEnv("SERVICE_NAME", settings.serviceName);
proxyContainer.addEnv("ACL_ENABLED", Boolean.toString(settings.aclEnabled));
proxyContainer.addEnv("VIEW_FEATURES_ALL", Boolean.toString(settings.advancedViews));
var runtimeContainer = new KalixRuntimeContainer(settings.eventingSupport, port, grpcEventingBackendPort);
this.runtimeContainer = Optional.of(runtimeContainer);
runtimeContainer.addEnv("SERVICE_NAME", settings.serviceName);
runtimeContainer.addEnv("ACL_ENABLED", Boolean.toString(settings.aclEnabled));
runtimeContainer.addEnv("VIEW_FEATURES_ALL", Boolean.toString(settings.advancedViews));

List<String> javaOptions = new ArrayList<>();
javaOptions.add("-Dlogback.configurationFile=logback-dev-mode.xml");
Expand Down Expand Up @@ -573,13 +574,13 @@ private void runProxy(Boolean useTestContainers, int port, int grpcEventingBacke
});

log.debug("Running container with javaOptions=" + javaOptions);
proxyContainer.addEnv("JAVA_TOOL_OPTIONS", String.join(" ", javaOptions));
settings.workflowTickInterval.ifPresent(tickInterval -> proxyContainer.addEnv("WORKFLOW_TICK_INTERVAL", tickInterval.toMillis() + ".millis"));
runtimeContainer.addEnv("JAVA_TOOL_OPTIONS", String.join(" ", javaOptions));
settings.workflowTickInterval.ifPresent(tickInterval -> runtimeContainer.addEnv("WORKFLOW_TICK_INTERVAL", tickInterval.toMillis() + ".millis"));

proxyContainer.start();
runtimeContainer.start();

proxyPort = proxyContainer.getProxyPort();
proxyHost = proxyContainer.getHost();
proxyPort = runtimeContainer.getProxyPort();
proxyHost = runtimeContainer.getHost();

} else {
proxyPort = 9000;
Expand Down Expand Up @@ -614,11 +615,11 @@ private void runProxy(Boolean useTestContainers, int port, int grpcEventingBacke
holder.overrideTracingCollectorEndpoint(""); //emulating ProxyInfo with disabled tracing.
}

private int userFunctionPort(Boolean useTestContainers) {
private int userServicePort(Boolean useTestContainers) {
if (useTestContainers) {
return availableLocalPort();
} else {
return KalixProxyContainer.DEFAULT_USER_FUNCTION_PORT;
return KalixRuntimeContainer.DEFAULT_USER_SERVICE_PORT;
}
}

Expand Down Expand Up @@ -806,7 +807,7 @@ private void throwMissingConfigurationException(String hint) {
*/
public void stop() {
try {
proxyContainer.ifPresent(container -> container.stop());
runtimeContainer.ifPresent(container -> container.stop());
} catch (Exception e) {
log.error("KalixTestkit proxy container failed to stop", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

package kalix.javasdk.testkit;
package kalix.javasdk.testkit.impl;

import kalix.javasdk.testkit.BuildInfo;
import kalix.javasdk.testkit.KalixTestKit.Settings.EventingSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -29,16 +30,16 @@
import static kalix.javasdk.testkit.KalixTestKit.Settings.EventingSupport.KAFKA;

/** Docker test container of Kalix Runtime for local development and testing. */
public class KalixProxyContainer extends GenericContainer<KalixProxyContainer> {
public class KalixRuntimeContainer extends GenericContainer<KalixRuntimeContainer> {

/** Default Testcontainers DockerImageName for the Kalix Runtime. */
public static final DockerImageName DEFAULT_PROXY_IMAGE_NAME;
public static final DockerImageName DEFAULT_RUNTIME_IMAGE_NAME;

/** Default proxy port (9000). */
public static final int DEFAULT_PROXY_PORT = 9000;
/** Default runtime port (9000). */
public static final int DEFAULT_RUNTIME_PORT = 9000;

/** Default user function port (8080). */
public static final int DEFAULT_USER_FUNCTION_PORT = 8080;
/** Default user service port (8080). */
public static final int DEFAULT_USER_SERVICE_PORT = 8080;

/** Default local port where the Google Pub/Sub emulator is available (8085). */
public static final int DEFAULT_GOOGLE_PUBSUB_PORT = 8085;
Expand All @@ -48,10 +49,10 @@ public class KalixProxyContainer extends GenericContainer<KalixProxyContainer> {
static {
String customImage = System.getenv("KALIX_TESTKIT_PROXY_IMAGE");
if (customImage == null) {
DEFAULT_PROXY_IMAGE_NAME = DockerImageName.parse(BuildInfo.runtimeImage()).withTag(BuildInfo.runtimeVersion());
DEFAULT_RUNTIME_IMAGE_NAME = DockerImageName.parse(BuildInfo.runtimeImage()).withTag(BuildInfo.runtimeVersion());
} else {
Logger logger = LoggerFactory.getLogger(KalixProxyContainer.class);
DEFAULT_PROXY_IMAGE_NAME = DockerImageName.parse(customImage);
Logger logger = LoggerFactory.getLogger(KalixRuntimeContainer.class);
DEFAULT_RUNTIME_IMAGE_NAME = DockerImageName.parse(customImage);
logger.info("Using custom runtime image [{}]", customImage);
}
}
Expand All @@ -60,31 +61,31 @@ public class KalixProxyContainer extends GenericContainer<KalixProxyContainer> {
private final int eventingPort;
private final EventingSupport eventingSupport;

public KalixProxyContainer() {
this(DEFAULT_USER_FUNCTION_PORT);
public KalixRuntimeContainer() {
this(DEFAULT_USER_SERVICE_PORT);
}

public KalixProxyContainer(final int userFunctionPort) {
this(DEFAULT_PROXY_IMAGE_NAME, EventingSupport.TEST_BROKER, userFunctionPort, DEFAULT_GOOGLE_PUBSUB_PORT);
public KalixRuntimeContainer(final int userFunctionPort) {
this(DEFAULT_RUNTIME_IMAGE_NAME, EventingSupport.TEST_BROKER, userFunctionPort, DEFAULT_GOOGLE_PUBSUB_PORT);
}

public KalixProxyContainer(EventingSupport eventingSupport, final int userFunctionPort, int eventingPort) {
this(DEFAULT_PROXY_IMAGE_NAME, eventingSupport, userFunctionPort, eventingPort);
public KalixRuntimeContainer(EventingSupport eventingSupport, final int userFunctionPort, int eventingPort) {
this(DEFAULT_RUNTIME_IMAGE_NAME, eventingSupport, userFunctionPort, eventingPort);
}

public KalixProxyContainer(
public KalixRuntimeContainer(
final DockerImageName dockerImageName,
EventingSupport eventingSupport,
final int userFunctionPort,
final int userServicePort,
final int eventingPort) {
super(dockerImageName);
this.userFunctionPort = userFunctionPort;
this.userFunctionPort = userServicePort;
this.eventingPort = eventingPort;
this.eventingSupport = eventingSupport;
withExposedPorts(DEFAULT_PROXY_PORT);
withExposedPorts(DEFAULT_RUNTIME_PORT);
withEnv("USER_SERVICE_HOST", "host.testcontainers.internal");
withEnv("USER_SERVICE_PORT", String.valueOf(userFunctionPort));
withEnv("HTTP_PORT", String.valueOf(DEFAULT_PROXY_PORT));
withEnv("USER_SERVICE_PORT", String.valueOf(userServicePort));
withEnv("HTTP_PORT", String.valueOf(DEFAULT_RUNTIME_PORT));
if ("false".equals(System.getenv("VERSION_CHECK_ON_STARTUP"))) {
withEnv("VERSION_CHECK_ON_STARTUP", "false");
}
Expand Down Expand Up @@ -114,6 +115,6 @@ public void start() {
* @return port for the local Kalix service
*/
public int getProxyPort() {
return getMappedPort(DEFAULT_PROXY_PORT);
return getMappedPort(DEFAULT_RUNTIME_PORT);
}
}

0 comments on commit 9594bb6

Please sign in to comment.