Skip to content

Commit

Permalink
Replicate failing to open port on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
mdedetrich committed Dec 7, 2022
1 parent 973cd63 commit 3621404
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ jobs:
shell: bash
run: |
# test building sbtn on Windows
sbt "commandProj/testOnly xsbt.IPCSpec"
sbt "-Dsbt.io.virtual=false" nativeImage
# test launcher script
echo build using JDK 8, test using JDK 8, on Windows
Expand Down
9 changes: 7 additions & 2 deletions main-command/src/main/scala/xsbt/IPC.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import scala.util.control.NonFatal
object IPC {
private val portMin = 1025
private val portMax = 65536
private val loopback = InetAddress.getByName(null)
private[xsbt] val loopback = InetAddress.getByName(null)

def client[T](port: Int)(f: IPC => T): T = ipc(new Socket(loopback, port))(f)

Expand All @@ -34,7 +34,12 @@ object IPC {

def createServer(attempts: Int): ServerSocket =
if (attempts > 0) {
try new ServerSocket(nextPort, 1, loopback)
val backlog =
if (System.getProperty("os.name").toLowerCase.contains("win"))
2
else
1
try new ServerSocket(nextPort, backlog, loopback)
catch { case NonFatal(_) => createServer(attempts - 1) }
} else sys.error("Could not connect to socket: maximum attempts exceeded")

Expand Down
30 changes: 30 additions & 0 deletions main-command/src/test/scala/xsbt/IPCSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* sbt
* Copyright 2011 - 2018, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* Licensed under Apache License 2.0 (see LICENSE)
*/

package xsbt

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers._

class IPCSpec extends AnyFlatSpec {
"server" should "find free open ports and close them" in {
noException should be thrownBy {
val server = IPC.unmanagedServer
(1 until 500).foreach { index =>
try {
IPC.client(server.port)(identity)
} catch {
case e: Throwable =>
val message = s"failure index is $index"
info(message)
throw e
}
}
server.close()
}
}
}

0 comments on commit 3621404

Please sign in to comment.