-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
/** | ||
|
@@ -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() } | ||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 callTestDynamoDbServer.startUp
(which is where the passed inreleasePort
function is called before starting a ddb server), then another test reusing that port will close it prior to allocating it to ddb.