Skip to content

Commit

Permalink
fixed a flaky test (#566)
Browse files Browse the repository at this point in the history
Sometimes in `OAuthClientSuite.scala`, after invoking server.shutdown(), the HTTP server does not fully stop, leading to the port not being released. This PR adds an additional safeguard to ensure the test server releases the port properly.
  • Loading branch information
moderakh committed Aug 14, 2024
1 parent 0880727 commit 432f4c3
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,27 @@ class OAuthClientSuite extends SparkFunSuite {
.create()

server.start()

Thread.sleep(2000)
}

def stopServer(): Unit = {
if (server != null) {
server.shutdown(2, TimeUnit.SECONDS)
server.shutdown(5, TimeUnit.SECONDS)
server = null
}
waitForPortRelease(1080, 10000)
}

def waitForPortRelease(port: Int, timeoutMillis: Int): Unit = {
val startTime = System.currentTimeMillis()
while (System.currentTimeMillis() - startTime < timeoutMillis) {
try {
new java.net.Socket("localhost", port).close()
} catch {
case _: java.net.ConnectException => return // Port is released
}
Thread.sleep(100) // Wait for 100 milliseconds before checking again
}
throw new RuntimeException(s"Port $port is not released after $timeoutMillis milliseconds")
}

test("OAuthClient should parse token response correctly") {
Expand Down

0 comments on commit 432f4c3

Please sign in to comment.