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