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
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class DockerDynamoDbServer private constructor(
)

object Factory : TestDynamoDbServer.Factory<DockerDynamoDbServer> {
override fun hostName(port: Int): String = app.cash.tempest.testing.internal.hostName(port)
override fun create(port: Int, onBeforeStartup: () -> Unit) = DockerDynamoDbServer(port, onBeforeStartup)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ import com.google.common.util.concurrent.AbstractIdleService

class DefaultTestDynamoDbClient(
override val tables: List<TestTable>,
private val hostName: String,
private val port: Int,
) : AbstractIdleService(), TestDynamoDbClient {
// Lazy so that hostName is resolved after the DynamoDB Local server is started,
// not at construction time when the placeholder ServerSocket is still holding the port.
private val hostName by lazy { hostName(port) }

override val dynamoDb by lazy { buildDynamoDb(hostName, port) }
override val dynamoDbStreams by lazy { buildDynamoDbStreams(hostName, port) }
override val dynamoDb = buildDynamoDb(hostName, port)
override val dynamoDbStreams = buildDynamoDbStreams(hostName, port)

override fun startUp() {
reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TestDynamoDbService private constructor(
): TestDynamoDbService {
val portHolder = port?.let { PortHolder(it) } ?: defaultPortHolder(serverFactory.toString())
return TestDynamoDbService(
DefaultTestDynamoDbClient(tables, portHolder.value),
DefaultTestDynamoDbClient(tables, serverFactory.hostName(portHolder.value), portHolder.value),
serverFactory.create(portHolder.value, portHolder.releasePort)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class JvmDynamoDbServer private constructor(
}

object Factory : TestDynamoDbServer.Factory<JvmDynamoDbServer> {
override fun hostName(port: Int) = "localhost"
override fun create(port: Int, onBeforeStartup: () -> Unit) = JvmDynamoDbServer(port, onBeforeStartup)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ interface TestDynamoDbServer : Service {
val port: Int

interface Factory<T : TestDynamoDbServer> {
/** The hostname the client should use to connect to the server. */
fun hostName(port: Int): String
fun create(port: Int): T = create(port) {}
fun create(port: Int, onBeforeStartup: () -> Unit): T
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package app.cash.tempest2.testing

import app.cash.tempest2.testing.internal.buildDynamoDb
import app.cash.tempest2.testing.internal.hostName
import com.github.dockerjava.api.model.ExposedPort
import com.github.dockerjava.api.model.Ports
import com.google.common.util.concurrent.AbstractIdleService
Expand Down Expand Up @@ -69,6 +70,7 @@ class DockerDynamoDbServer private constructor(
)

object Factory : TestDynamoDbServer.Factory<DockerDynamoDbServer> {
override fun hostName(port: Int): String = app.cash.tempest2.testing.internal.hostName(port)
override fun create(port: Int, onBeforeStartup: () -> Unit) = DockerDynamoDbServer(port, onBeforeStartup)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest

class DefaultTestDynamoDbClient(
override val tables: List<TestTable>,
private val hostName: String,
private val port: Int,
) : AbstractIdleService(), TestDynamoDbClient {
// Lazy so that hostName is resolved after the DynamoDB Local server is started,
// not at construction time when the placeholder ServerSocket is still holding the port.
private val hostName by lazy { hostName(port) }

override val dynamoDb by lazy { buildDynamoDb(hostName, port) }
override val asyncDynamoDb by lazy { buildAsyncDynamoDb(hostName, port) }
override val dynamoDbStreams by lazy { buildDynamoDbStreams(hostName, port) }
override val asyncDynamoDbStreams by lazy { buildAsyncDynamoDbStreams(hostName, port) }
override val dynamoDb = buildDynamoDb(hostName, port)
override val asyncDynamoDb = buildAsyncDynamoDb(hostName, port)
override val dynamoDbStreams = buildDynamoDbStreams(hostName, port)
override val asyncDynamoDbStreams = buildAsyncDynamoDbStreams(hostName, port)

override fun startUp() {
reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TestDynamoDbService(
): TestDynamoDbService {
val portHolder = port?.let { PortHolder(it) } ?: defaultPortHolder(serverFactory.toString())
return TestDynamoDbService(
DefaultTestDynamoDbClient(tables, portHolder.value),
DefaultTestDynamoDbClient(tables, serverFactory.hostName(portHolder.value), portHolder.value),
serverFactory.create(portHolder.value, portHolder.releasePort)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class JvmDynamoDbServer private constructor(
}

object Factory : TestDynamoDbServer.Factory<JvmDynamoDbServer> {
override fun hostName(port: Int) = "localhost"
override fun create(port: Int, onBeforeStartup: () -> Unit) = JvmDynamoDbServer(port, onBeforeStartup)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ interface TestDynamoDbServer : Service {
val port: Int

interface Factory<T : TestDynamoDbServer> {
/** The hostname the client should use to connect to the server. */
fun hostName(port: Int): String
fun create(port: Int): T = create(port) {}
fun create(port: Int, onBeforeStartup: () -> Unit): T
}
Expand Down
Loading