@@ -20,7 +20,7 @@ package org.apache.spark.util
2020import scala .util .Random
2121
2222import java .io .{File , ByteArrayOutputStream , ByteArrayInputStream , FileOutputStream }
23- import java .net .URI
23+ import java .net .{ BindException , ServerSocket , URI }
2424import java .nio .{ByteBuffer , ByteOrder }
2525
2626import com .google .common .base .Charsets
@@ -265,4 +265,36 @@ class UtilsSuite extends FunSuite {
265265 Array (" hdfs:/a.jar" , " s3:/another.jar" ))
266266 }
267267
268+ test(" isBindCollision" ) {
269+ // Negatives
270+ assert(! Utils .isBindCollision(null ))
271+ assert(! Utils .isBindCollision(new Exception ))
272+ assert(! Utils .isBindCollision(new Exception (new Exception )))
273+ assert(! Utils .isBindCollision(new Exception (new BindException )))
274+ assert(! Utils .isBindCollision(new Exception (new BindException (" Random message" ))))
275+
276+ // Positives
277+ val be = new BindException (" Address already in use" )
278+ val be1 = new Exception (new BindException (" Address already in use" ))
279+ val be2 = new Exception (new Exception (new BindException (" Address already in use" )))
280+ assert(Utils .isBindCollision(be))
281+ assert(Utils .isBindCollision(be1))
282+ assert(Utils .isBindCollision(be2))
283+
284+ // Actual bind exception
285+ var server1 : ServerSocket = null
286+ var server2 : ServerSocket = null
287+ try {
288+ server1 = new java.net.ServerSocket (0 )
289+ server2 = new java.net.ServerSocket (server1.getLocalPort)
290+ } catch {
291+ case e : Exception =>
292+ assert(e.isInstanceOf [java.net.BindException ])
293+ assert(Utils .isBindCollision(e))
294+ } finally {
295+ Option (server1).foreach(_.close())
296+ Option (server2).foreach(_.close())
297+ }
298+ }
299+
268300}
0 commit comments