Skip to content

Commit

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

This reverts commit 4b6b047.
  • Loading branch information
mmollaverdi committed Jun 3, 2024
1 parent 4b6b047 commit d65a914
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import java.net.ServerSocket
import java.net.Socket

fun allocateRandomPort(): ServerSocket {
val socket = ServerSocket(0)
val socket = ServerSocket(0) //use { socket -> return socket.localPort }
Runtime.getRuntime().addShutdownHook(
Thread { socket.close() }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException

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

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

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

// Temporary client to block until the container is running
Expand Down Expand Up @@ -69,6 +67,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(

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 @@ -33,12 +33,8 @@ import java.net.ServerSocket
import java.net.Socket
import java.net.URI

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

private val AWS_CREDENTIALS_PROVIDER = StaticCredentialsProvider.create(
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 = "tempest2-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 d65a914

Please sign in to comment.