Skip to content
Merged
Show file tree
Hide file tree
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
39 changes: 39 additions & 0 deletions src/Instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,45 @@ test('behavior: messages', async () => {
`)
})

test('behavior: dynamic host/port via setEndpoint', async () => {
const foo = Instance.define(() => {
return {
name: 'foo',
host: 'localhost',
port: 3000,
async start(_, { setEndpoint }) {
setEndpoint?.({ host: '192.168.1.100', port: 9999 })
},
async stop() {},
}
})

const instance = foo()
expect(instance.host).toEqual('localhost')
expect(instance.port).toEqual(3000)

await instance.start()
expect(instance.host).toEqual('192.168.1.100')
expect(instance.port).toEqual(9999)
})

test('behavior: start() returning void keeps original host/port', async () => {
const foo = Instance.define(() => {
return {
name: 'foo',
host: 'localhost',
port: 3000,
async start() {},
async stop() {},
}
})

const instance = foo()
await instance.start()
expect(instance.host).toEqual('localhost')
expect(instance.port).toEqual(3000)
})

test('options: timeout', async () => {
const foo = Instance.define(() => {
return {
Expand Down
18 changes: 14 additions & 4 deletions src/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ export function define<
const options = options_ || parametersOrOptions || {}

const instance = fn(parameters)
const { _internal, host, name, port, start, stop } = {
const { _internal, name, start, stop } = {
...instance,
...createParameters,
port: createParameters.port ?? instance.port,
}
let host = instance.host
let port = createParameters?.port ?? instance.port
const { messageBuffer = 20, timeout } = options

let restartResolver = Promise.withResolvers<void>()
Expand Down Expand Up @@ -159,9 +160,13 @@ export function define<
return messages
},
},
host,
get host() {
return host
},
name,
port,
get port() {
return port
},
get status() {
if (restarting) return 'restarting'
return status
Expand Down Expand Up @@ -193,6 +198,10 @@ export function define<
},
{
emitter,
setEndpoint(endpoint) {
if (endpoint.host) host = endpoint.host
if (endpoint.port) port = endpoint.port
},
status: this.status,
},
)
Expand Down Expand Up @@ -302,6 +311,7 @@ export declare namespace define {

export type InstanceStartOptions_internal = {
emitter: EventEmitter<EventTypes>
setEndpoint?(endpoint: { host?: string; port?: number }): void
status: Instance['status']
}

Expand Down
17 changes: 11 additions & 6 deletions src/testcontainers/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,21 @@ export const tempo = Instance.define((parameters?: tempo.Parameters) => {
host: args.host ?? 'localhost',
name,
port: args.port ?? 8545,
async start({ port = args.port }, { emitter }) {
async start({ port = args.port }, { emitter, setEndpoint }) {
const promise = Promise.withResolvers<void>()

const containerPort = port ?? 8545

const c = new GenericContainer(image)
.withPullPolicy(PullPolicy.alwaysPull())
.withPlatform('linux/x86_64')
.withNetworkMode('host')
.withExposedPorts(containerPort)
.withExtraHosts([
{ host: 'host.docker.internal', ipAddress: 'host-gateway' },
{ host: 'localhost', ipAddress: 'host-gateway' },
])
.withName(containerName)
.withEnvironment({ RUST_LOG })
.withCommand(command({ ...args, port }))
.withCommand(command({ ...args, port: containerPort }))
.withWaitStrategy(
Wait.forLogMessage(/Received (block|new payload) from consensus engine/),
)
Expand All @@ -83,8 +84,12 @@ export const tempo = Instance.define((parameters?: tempo.Parameters) => {
.withStartupTimeout(10_000)

c.start()
.then((c) => {
container = c
.then((started) => {
container = started
setEndpoint?.({
host: started.getHost(),
port: started.getMappedPort(containerPort),
})
promise.resolve()
})
.catch(promise.reject)
Expand Down
Loading