Skip to content

Commit

Permalink
Start rewriting the ExecutorURLClassLoaderSuite to not use the hard c…
Browse files Browse the repository at this point in the history
…oded classes
  • Loading branch information
holdenk committed Apr 8, 2014
1 parent 858aba2 commit 9f68f10
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
32 changes: 32 additions & 0 deletions core/src/test/scala/org/apache/spark/TestUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ object TestUtils {
createJar(files, jarFile)
}

/**
* Create a jar that defines classes with the given names.
*
* Note: if this is used during class loader tests, class names should be unique
* in order to avoid interference between tests.
*/
def createJarWithClassesAndValue(classNames: Seq[String], value: Integer): URL = {
val tempDir = Files.createTempDir()
val files = for (name <- classNames) yield createCompiledClassWithValue(name, value, tempDir)
val jarFile = new File(tempDir, "testJar-%s.jar".format(System.currentTimeMillis()))
createJar(files, jarFile)
}


/**
* Create a jar file that contains this set of files. All files will be located at the root
* of the jar.
Expand Down Expand Up @@ -95,4 +109,22 @@ object TestUtils {
result.renameTo(out)
out
}

/** Creates a compiled class with the given name. Class file will be placed in destDir. */
def createCompiledClassWithValue(className: String, value: Integer, destDir: File): File = {
val compiler = ToolProvider.getSystemJavaCompiler
val sourceFile = new JavaSourceFromString(className,
"public class " + className + " { override String toString() { return \"" + value + "\";}}")

// Calling this outputs a class file in pwd. It's easier to just rename the file than
// build a custom FileManager that controls the output location.
compiler.getTask(null, null, null, null, null, Seq(sourceFile)).call()

val fileName = className + ".class"
val result = new File(fileName)
if (!result.exists()) throw new Exception("Compiled file not found: " + fileName)
val out = new File(destDir, fileName)
result.renameTo(out)
out
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import java.net.URLClassLoader

import org.scalatest.FunSuite

import org.apache.spark.TestUtils

class ExecutorURLClassLoaderSuite extends FunSuite {

val spark_home = sys.env.get("SPARK_HOME").orElse(sys.props.get("spark.home")).get
val urls = List(new File(spark_home + "/core/src/test/resources/fake-spark-class.jar").toURI.toURL).toArray
val urls2 = List(new File(spark_home + "/core/src/test/resources/fake-spark-class-2.jar").toURI.toURL).toArray
val classNames = List("org.apache.spark.test.FakeClass1",
"org.apache.spark.test.FakeClass2")
val urls = List(TestUtils.createJarWithClassesAndValue(classNames, 1)).toArray
val urls2 = List(TestUtils.createJarWithClassesAndValue(classNames, 2)).toArray

test("child first") {
val parentLoader = new URLClassLoader(urls2, null)
Expand Down

0 comments on commit 9f68f10

Please sign in to comment.