Skip to content

Commit

Permalink
Revert "Hold onto randomly allocated ports to avoid port conflicts in…
Browse files Browse the repository at this point in the history
… concurrent tests (#182)"

This reverts commit 38c0913.
  • Loading branch information
mmollaverdi committed Jun 3, 2024
1 parent d65a914 commit 10fbe18
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ import com.github.dockerjava.api.model.Ports
import com.google.common.util.concurrent.AbstractIdleService

class DockerDynamoDbServer private constructor(
override val port: Int,
private val onBeforeStartup: () -> Unit
override val port: Int
) : AbstractIdleService(), TestDynamoDbServer {

override val id = "tempest-docker-dynamodb-local-$port"

override fun startUp() {
onBeforeStartup()
composer.start()

// Temporary client to block until the container is running
Expand Down Expand Up @@ -70,6 +68,6 @@ class DockerDynamoDbServer private constructor(
)

object Factory : TestDynamoDbServer.Factory<DockerDynamoDbServer> {
override fun create(port: Int, onBeforeStartup: () -> Unit) = DockerDynamoDbServer(port, onBeforeStartup)
override fun create(port: Int) = DockerDynamoDbServer(port)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,21 @@ class TestDynamoDbService private constructor(

companion object {
private val defaultPorts = ConcurrentHashMap<String, Int>()
private fun defaultPort(key: String): PortHolder {
var releasePort: () -> Unit = {}
private fun defaultPort(key: String): Int {
// Only pick random port once to share one test server with multiple tests.
val port = defaultPorts.getOrPut(key) {
val socket = allocateRandomPort()
releasePort = { socket.close() }
socket.localPort
}
return PortHolder(port, releasePort)
return defaultPorts.getOrPut(key, ::pickRandomPort)
}

private val runningServers = ConcurrentHashMap.newKeySet<String>()
private val log = getLogger<TestDynamoDbService>()

@JvmStatic
fun create(serverFactory: TestDynamoDbServer.Factory<*>, tables: List<TestTable>, port: Int? = null): TestDynamoDbService {
val portHolder = port?.let { PortHolder(it) } ?: defaultPort(serverFactory.toString())
val port = port ?: defaultPort(serverFactory.toString())
return TestDynamoDbService(
DefaultTestDynamoDbClient(tables, portHolder.value),
serverFactory.create(portHolder.value, portHolder.releasePort)
DefaultTestDynamoDbClient(tables, port),
serverFactory.create(port)
)
}
}

private data class PortHolder(val value: Int, val releasePort: () -> Unit = {})
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ import java.net.InetSocketAddress
import java.net.ServerSocket
import java.net.Socket

fun allocateRandomPort(): ServerSocket {
val socket = ServerSocket(0) //use { socket -> return socket.localPort }
Runtime.getRuntime().addShutdownHook(
Thread { socket.close() }
)
return socket
fun pickRandomPort(): Int {
ServerSocket(0).use { socket -> return socket.localPort }
}

private const val CONNECT_TIMEOUT_MILLIS = 1_000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import com.google.common.util.concurrent.AbstractIdleService
import java.io.File

class JvmDynamoDbServer private constructor(
override val port: Int,
private val onBeforeStartup: () -> Unit
override val port: Int
) : AbstractIdleService(), TestDynamoDbServer {

override val id = "tempest-jvm-dynamodb-local-$port"
Expand All @@ -34,7 +33,6 @@ class JvmDynamoDbServer private constructor(
val libraryFile = libsqlite4javaNativeLibrary()
System.setProperty("sqlite4java.library.path", libraryFile.parent)

onBeforeStartup()
server = ServerRunner.createServerFromCommandLineArgs(
arrayOf("-inMemory", "-port", port.toString())
)
Expand Down Expand Up @@ -89,6 +87,6 @@ class JvmDynamoDbServer private constructor(
}

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

interface Factory<T : TestDynamoDbServer> {
fun create(port: Int): T = create(port) {}
fun create(port: Int, onBeforeStartup: () -> Unit): T
fun create(port: Int): T
}
}

0 comments on commit 10fbe18

Please sign in to comment.