Skip to content

Commit

Permalink
Simplify container factory
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkleeman committed Nov 4, 2024
1 parent 913a1f0 commit 36357a2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
3 changes: 2 additions & 1 deletion packages/restate-sdk-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
},
"devDependencies": {
"tsx": "^4.15.7",
"@restatedev/restate-sdk-testcontainers": "^1.4.0"
"@restatedev/restate-sdk-testcontainers": "^1.4.0",
"testcontainers": "^10.4.0"
},
"engines": {
"node": ">= 18.13"
Expand Down
8 changes: 3 additions & 5 deletions packages/restate-sdk-examples/test/testcontainers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
*/

import {
RestateTestEnvironment,
restateContainerFactory,
} from "@restatedev/restate-sdk-testcontainers";
import { RestateTestEnvironment } from "@restatedev/restate-sdk-testcontainers";
import { counter } from "../src/object.js";
import * as clients from "@restatedev/restate-sdk-clients";
import * as core from "@restatedev/restate-sdk-core";
import { describe, it, beforeAll, afterAll, expect } from "vitest";
import { GenericContainer } from "testcontainers";

describe("ExampleObject", () => {
let restateTestEnvironment: RestateTestEnvironment;
Expand Down Expand Up @@ -129,7 +127,7 @@ describe("Custom testcontainer config", () => {
restateTestEnvironment = await RestateTestEnvironment.start(
(restateServer) => restateServer.bind(counter),
() =>
restateContainerFactory("restatedev/restate:1.1")()
new GenericContainer("restatedev/restate:1.1")
.withEnvironment({ RESTATE_LOG_FORMAT: "json" })
.withLogConsumer((stream) => {
// eslint-disable-next-line no-console
Expand Down
5 changes: 1 addition & 4 deletions packages/restate-sdk-testcontainers/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,4 @@
* https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
*/

export {
RestateTestEnvironment,
restateContainerFactory,
} from "./restate_test_environment.js";
export { RestateTestEnvironment } from "./restate_test_environment.js";
37 changes: 14 additions & 23 deletions packages/restate-sdk-testcontainers/src/restate_test_environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ async function prepareRestateEndpoint(
// Prepare the restate testcontainer
async function prepareRestateTestContainer(
restateServerPort: number,
restateContainerFactory: () => GenericContainer = () => new GenericContainer("docker.io/restatedev/restate:1.1")
restateContainerFactory: () => GenericContainer
): Promise<StartedTestContainer> {
const restateContainer = restateContainerFactory();
const restateContainer = restateContainerFactory()
// Expose ports
.withExposedPorts(8080, 9070)
// Wait start on health checks
.withWaitStrategy(
Wait.forAll([
Wait.forHttp("/restate/health", 8080),
Wait.forHttp("/health", 9070),
])
);

// This MUST be executed before starting the restate container
// Expose host port to access the restate server
Expand Down Expand Up @@ -102,23 +111,6 @@ async function prepareRestateTestContainer(
}
}

export function restateContainerFactory(image: string): () => GenericContainer {
return () => {
const restateContainer = new GenericContainer(image)
// Expose ports
.withExposedPorts(8080, 9070)
// Wait start on health checks
.withWaitStrategy(
Wait.forAll([
Wait.forHttp("/restate/health", 8080),
Wait.forHttp("/health", 9070),
])
);

return restateContainer;
};
}

export class RestateTestEnvironment {
constructor(
readonly startedRestateHttpServer: http2.Http2Server,
Expand Down Expand Up @@ -154,16 +146,15 @@ export class RestateTestEnvironment {

public static async start(
mountServicesFn: (server: restate.RestateEndpoint) => void,
_restateContainerFactory: () => GenericContainer = restateContainerFactory(
"restatedev/restate:1.1"
)
restateContainerFactory: () => GenericContainer = () =>
new GenericContainer("restatedev/restate:1.1")
): Promise<RestateTestEnvironment> {
const startedRestateHttpServer = await prepareRestateEndpoint(
mountServicesFn
);
const startedRestateContainer = await prepareRestateTestContainer(
(startedRestateHttpServer.address() as net.AddressInfo).port,
_restateContainerFactory
restateContainerFactory
);
return new RestateTestEnvironment(
startedRestateHttpServer,
Expand Down

0 comments on commit 36357a2

Please sign in to comment.