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

Conversation

mmollaverdi
Copy link
Contributor

Follow-up from #184 and #182

The previous implementation fails with a port already bound error, if a first test creates a test dynamodb server, but doesn't start and then a second test tries to start the server, because the socket allocated in the first test and reused in the second test doesn't get closed (due to first test not calling startUp).

This PR makes it so that we close the socket if it's not closed even when it's reused across tests.

@@ -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.

}

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.

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.

@kyeotic kyeotic merged commit 2a33625 into cashapp:main Jun 3, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants