Skip to content

Commit

Permalink
fix: dynamically select port for DDB Local (#1480)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianbotsf authored Dec 18, 2024
1 parent 557b8fa commit 49196bc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
20 changes: 17 additions & 3 deletions hll/dynamodb-mapper/dynamodb-mapper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer
import com.google.devtools.ksp.gradle.KspTaskJvm
import com.google.devtools.ksp.gradle.KspTaskMetadata
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
import java.net.ServerSocket
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import kotlin.properties.Delegates

description = "High level DynamoDbMapper client"
extra["displayName"] = "AWS :: SDK :: Kotlin :: HLL :: DynamoDbMapper"
Expand Down Expand Up @@ -128,29 +130,41 @@ if (project.NATIVE_ENABLED) {
}

open class DynamoDbLocalInstance : DefaultTask() {
private val port = 44212 // Keep in sync with DdbLocalTest.kt
private var port: Int by Delegates.notNull()

@OutputFile
val portFile = project.objects.fileProperty()

@Internal
var runner: DynamoDBProxyServer? = null
private set

@TaskAction
fun exec() {
println("Running DynamoDB local instance on port $port")
port = ServerSocket(0).use { it.localPort }

println("Starting DynamoDB local instance on port $port")
runner = ServerRunner
.createServerFromCommandLineArgs(arrayOf("-inMemory", "-port", port.toString(), "-disableTelemetry"))
.also { it.start() }

portFile.asFile.get().writeText(port.toString())
}

fun stop() {
runCatching { portFile.asFile.get().delete() }.onFailure { t -> println("Failed to delete $portFile: $t") }

runner?.let {
println("Stopping DynamoDB local instance on port $port")
it.stop()
}
}
}

val startDdbLocal = task<DynamoDbLocalInstance>("startDdbLocal")
val startDdbLocal = task<DynamoDbLocalInstance>("startDdbLocal") {
portFile.set(file("build/ddblocal/port.info")) // Keep in sync with DdbLocalTest.kt
outputs.upToDateWhen { false } // Always run this task even if a portFile already exists
}

tasks.withType<Test> {
dependsOn(startDdbLocal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ import aws.smithy.kotlin.runtime.net.Scheme
import aws.smithy.kotlin.runtime.net.url.Url
import io.kotest.core.spec.style.AnnotationSpec
import kotlinx.coroutines.runBlocking
import java.io.File
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

private const val DDB_LOCAL_PORT = 44212 // Keep in sync with build.gradle.kts

/**
* The base class for test classes which need DynamoDB Local running. This class provides a few convenience declarations
* for subclasses:
Expand All @@ -50,11 +49,14 @@ abstract class DdbLocalTest : AnnotationSpec() {
private val requestInterceptor = RequestCapturingInterceptor(this@DdbLocalTest.requests)

private val ddbHolder = lazy {
val portFile = File("build/ddblocal/port.info").absoluteFile // Keep in sync with build.gradle.kts
val port = portFile.readText().toInt()

DynamoDbClient {
endpointUrl = Url {
scheme = Scheme.HTTP
host = Host.Domain("localhost")
port = DDB_LOCAL_PORT
this.port = port
}

region = "DUMMY"
Expand Down

0 comments on commit 49196bc

Please sign in to comment.