Skip to content

Commit

Permalink
Add a UT for ExecutorClassLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
taroplus committed Feb 26, 2017
1 parent e0d9538 commit 0a490bb
Showing 1 changed file with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ import java.nio.channels.{FileChannel, ReadableByteChannel}
import java.nio.charset.StandardCharsets
import java.nio.file.{Paths, StandardOpenOption}
import java.util
import java.util.{Arrays, Collections}
import javax.tools.{JavaFileObject, SimpleJavaFileObject, ToolProvider}

import scala.io.Source
import scala.language.implicitConversions

import com.google.common.io.Files
import org.mockito.Matchers.anyString
import org.mockito.Mockito._
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer
import org.scalatest.BeforeAndAfterAll
import org.scalatest.mock.MockitoSugar

import org.apache.spark._
import org.apache.spark.internal.Logging
import org.apache.spark.rpc.RpcEnv
Expand Down Expand Up @@ -77,6 +77,43 @@ class ExecutorClassLoaderSuite
}
}

test("child over system classloader") {
// JavaFileObject for scala.Option class
val scalaOptionFile = new SimpleJavaFileObject(
URI.create(s"string:///scala/Option.java"),
JavaFileObject.Kind.SOURCE) {

override def getCharContent(ignoreEncodingErrors: Boolean): CharSequence = {
"package scala; class Option {}"
}
}
// compile fake scala.Option class
ToolProvider
.getSystemJavaCompiler
.getTask(null, null, null, null, null, Collections.singletonList(scalaOptionFile)).call()

// create 'scala' dir in tempDir1
val scalaDir = new File(tempDir1, "scala")
assert(scalaDir.mkdir(), s"Failed to create 'scala' directory in $tempDir1")

// move the generated class into scala dir
val filename = "Option.class"
val result = new File(filename)
assert(result.exists(), "Compiled file not found: " + result.getAbsolutePath)

val out = new File(scalaDir, filename)
Files.move(result, out)
assert(out.exists(), "Destination file not moved: " + out.getAbsolutePath)

// construct class loader tree
val parentLoader = new URLClassLoader(urls2, null)
val classLoader = new ExecutorClassLoader(new SparkConf(), null, url1, parentLoader, true)

// load 'scala.Option'
val optionClass = Class.forName("scala.Option", false, classLoader)
assert(optionClass.getClassLoader == classLoader, "scala.Option didn't come from ExecutorClassLoader")
}

test("child first") {
val parentLoader = new URLClassLoader(urls2, null)
val classLoader = new ExecutorClassLoader(new SparkConf(), null, url1, parentLoader, true)
Expand Down

0 comments on commit 0a490bb

Please sign in to comment.