node-driver: Reuse address when polling for port, poll for correct condition\(!\)

This commit is contained in:
Andras Slemmer
2016-09-05 11:04:31 +01:00
parent 3854fec17f
commit 637a41401d

View File

@ -95,7 +95,7 @@ sealed class PortAllocation {
override fun nextPort(): Int { override fun nextPort(): Int {
return ServerSocket().use { return ServerSocket().use {
it.reuseAddress = true it.reuseAddress = true
it.bind(InetSocketAddress.createUnresolved("localhost", 0)) it.bind(InetSocketAddress("localhost", 0))
it.localPort it.localPort
} }
} }
@ -182,10 +182,13 @@ private fun getTimestampAsDirectoryName(): String {
fun addressMustBeBound(hostAndPort: HostAndPort) { fun addressMustBeBound(hostAndPort: HostAndPort) {
poll("address $hostAndPort to bind") { poll("address $hostAndPort to bind") {
try { try {
Socket(hostAndPort.hostText, hostAndPort.port).close() ServerSocket().use {
Unit it.reuseAddress = true
} catch (_exception: SocketException) { it.bind(InetSocketAddress(hostAndPort.hostText, hostAndPort.port))
}
null null
} catch (_exception: SocketException) {
Unit
} }
} }
} }
@ -193,10 +196,13 @@ fun addressMustBeBound(hostAndPort: HostAndPort) {
fun addressMustNotBeBound(hostAndPort: HostAndPort) { fun addressMustNotBeBound(hostAndPort: HostAndPort) {
poll("address $hostAndPort to unbind") { poll("address $hostAndPort to unbind") {
try { try {
Socket(hostAndPort.hostText, hostAndPort.port).close() ServerSocket().use {
null it.reuseAddress = true
} catch (_exception: SocketException) { it.bind(InetSocketAddress(hostAndPort.hostText, hostAndPort.port))
}
Unit Unit
} catch (_exception: SocketException) {
null
} }
} }
} }