Skip to content

Commit 0a7091e

Browse files
committed
[SPARK-3555] Fix UISuite race condition
The test "jetty selects different port under contention" is flaky. If another process binds to 4040 before the test starts, then the first server we start there will fail, and the subsequent servers we start thereafter may successfully bind to 4040 if it was released between the servers starting. Instead, we should just let Java find a random free port for us and hold onto it for the duration of the test. Author: Andrew Or <[email protected]> Closes #2418 from andrewor14/fix-port-contention and squashes the following commits: 0cd4974 [Andrew Or] Stop them servers a7071fe [Andrew Or] Pick random port instead of 4040
1 parent a6e1712 commit 0a7091e

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

core/src/test/scala/org/apache/spark/ui/UISuite.scala

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import javax.servlet.http.HttpServletRequest
2323
import scala.io.Source
2424
import scala.util.{Failure, Success, Try}
2525

26-
import org.eclipse.jetty.server.Server
2726
import org.eclipse.jetty.servlet.ServletContextHandler
2827
import org.scalatest.FunSuite
2928
import org.scalatest.concurrent.Eventually._
@@ -108,14 +107,8 @@ class UISuite extends FunSuite {
108107
}
109108

110109
test("jetty selects different port under contention") {
111-
val startPort = 4040
112-
val server = new Server(startPort)
113-
114-
Try { server.start() } match {
115-
case Success(s) =>
116-
case Failure(e) =>
117-
// Either case server port is busy hence setup for test complete
118-
}
110+
val server = new ServerSocket(0)
111+
val startPort = server.getLocalPort
119112
val serverInfo1 = JettyUtils.startJettyServer(
120113
"0.0.0.0", startPort, Seq[ServletContextHandler](), new SparkConf)
121114
val serverInfo2 = JettyUtils.startJettyServer(
@@ -126,6 +119,9 @@ class UISuite extends FunSuite {
126119
assert(boundPort1 != startPort)
127120
assert(boundPort2 != startPort)
128121
assert(boundPort1 != boundPort2)
122+
serverInfo1.server.stop()
123+
serverInfo2.server.stop()
124+
server.close()
129125
}
130126

131127
test("jetty binds to port 0 correctly") {

0 commit comments

Comments
 (0)