Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to close reused sockets in TestDynamoDbService #185

Merged
merged 1 commit into from
Jun 3, 2024
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 @@ -4,6 +4,7 @@ import app.cash.tempest.testing.TestDynamoDbClient
import app.cash.tempest.testing.TestDynamoDbServer
import app.cash.tempest.testing.TestTable
import com.google.common.util.concurrent.AbstractIdleService
import java.net.ServerSocket
import java.util.concurrent.ConcurrentHashMap

/**
Expand Down Expand Up @@ -45,24 +46,27 @@ class TestDynamoDbService private constructor(
}

companion object {
private val defaultPorts = ConcurrentHashMap<String, Int>()
private fun defaultPort(key: String): PortHolder {
var releasePort: () -> Unit = {}
private val allocatedSockets = ConcurrentHashMap<String, ServerSocket>()
private fun defaultPortHolder(key: String): PortHolder {
// 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
val socket = allocatedSockets.getOrPut(key) { allocateRandomPort() }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously releasePort was a no-op if the key already existed in the map. Now it'll return a function which closes the socket (if not already closed). So if a test didn't call TestDynamoDbServer.startUp (which is where the passed in releasePort function is called before starting a ddb server), then another test reusing that port will close it prior to allocating it to ddb.

return PortHolder(socket.localPort) {
if (!socket.isClosed) {
socket.close()
}
}
return PortHolder(port, releasePort)
}

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())
fun create(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting.

serverFactory: TestDynamoDbServer.Factory<*>,
tables: List<TestTable>,
port: Int? = null
): TestDynamoDbService {
val portHolder = port?.let { PortHolder(it) } ?: defaultPortHolder(serverFactory.toString())
return TestDynamoDbService(
DefaultTestDynamoDbClient(tables, portHolder.value),
serverFactory.create(portHolder.value, portHolder.releasePort)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import java.net.InetSocketAddress
import java.net.ServerSocket
import java.net.Socket

fun pickRandomPort(): Int {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bringing this back because it was a public method I had unintentionally deleted in the previous PR.

ServerSocket(0).use { socket -> return socket.localPort }
}

fun allocateRandomPort(): ServerSocket {
val socket = ServerSocket(0)
Runtime.getRuntime().addShutdownHook(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import app.cash.tempest2.testing.TestDynamoDbClient
import app.cash.tempest2.testing.TestDynamoDbServer
import app.cash.tempest2.testing.TestTable
import com.google.common.util.concurrent.AbstractIdleService
import java.net.ServerSocket
import java.util.concurrent.ConcurrentHashMap

/**
Expand Down Expand Up @@ -45,24 +46,27 @@ class TestDynamoDbService(
}

companion object {
private val defaultPorts = ConcurrentHashMap<String, Int>()
private fun defaultPort(key: String): PortHolder {
var releasePort: () -> Unit = {}
private val allocatedSockets = ConcurrentHashMap<String, ServerSocket>()
private fun defaultPortHolder(key: String): PortHolder {
// 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
val socket = allocatedSockets.getOrPut(key) { allocateRandomPort() }
return PortHolder(socket.localPort) {
if (!socket.isClosed) {
socket.close()
}
}
return PortHolder(port, releasePort)
}

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())
fun create(
serverFactory: TestDynamoDbServer.Factory<*>,
tables: List<TestTable>,
port: Int? = null
): TestDynamoDbService {
val portHolder = port?.let { PortHolder(it) } ?: defaultPortHolder(serverFactory.toString())
return TestDynamoDbService(
DefaultTestDynamoDbClient(tables, portHolder.value),
serverFactory.create(portHolder.value, portHolder.releasePort)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import java.net.ServerSocket
import java.net.Socket
import java.net.URI

fun pickRandomPort(): Int {
ServerSocket(0).use { socket -> return socket.localPort }
}

fun allocateRandomPort(): ServerSocket {
val socket = ServerSocket(0)
Runtime.getRuntime().addShutdownHook(
Expand Down
Loading