-
Notifications
You must be signed in to change notification settings - Fork 33
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
Conversation
@@ -32,6 +32,10 @@ import java.net.InetSocketAddress | |||
import java.net.ServerSocket | |||
import java.net.Socket | |||
|
|||
fun pickRandomPort(): Int { |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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() } |
There was a problem hiding this comment.
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.
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.