Skip to content

Commit 856156b

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 (cherry picked from commit 0a7091e) Signed-off-by: Andrew Or <[email protected]>
1 parent 75158a7 commit 856156b

1 file changed

Lines changed: 5 additions & 9 deletions

File tree

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import scala.io.Source
2424
import scala.language.postfixOps
2525
import scala.util.{Failure, Success, Try}
2626

27-
import org.eclipse.jetty.server.Server
2827
import org.eclipse.jetty.servlet.ServletContextHandler
2928
import org.scalatest.FunSuite
3029
import org.scalatest.concurrent.Eventually._
@@ -95,14 +94,8 @@ class UISuite extends FunSuite {
9594
}
9695

9796
test("jetty selects different port under contention") {
98-
val startPort = 4040
99-
val server = new Server(startPort)
100-
101-
Try { server.start() } match {
102-
case Success(s) =>
103-
case Failure(e) =>
104-
// Either case server port is busy hence setup for test complete
105-
}
97+
val server = new ServerSocket(0)
98+
val startPort = server.getLocalPort
10699
val serverInfo1 = JettyUtils.startJettyServer(
107100
"0.0.0.0", startPort, Seq[ServletContextHandler](), new SparkConf)
108101
val serverInfo2 = JettyUtils.startJettyServer(
@@ -113,6 +106,9 @@ class UISuite extends FunSuite {
113106
assert(boundPort1 != startPort)
114107
assert(boundPort2 != startPort)
115108
assert(boundPort1 != boundPort2)
109+
serverInfo1.server.stop()
110+
serverInfo2.server.stop()
111+
server.close()
116112
}
117113

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

0 commit comments

Comments
 (0)