Skip to content

Commit

Permalink
Add a test suite for the executor url class loader suite
Browse files Browse the repository at this point in the history
  • Loading branch information
holdenk committed Apr 8, 2014
1 parent 7ef4628 commit 22d83cb
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
31 changes: 31 additions & 0 deletions core/src/main/java/org/apache/spark/util/ParentClassLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.util;

/**
* A class loader which makes findClass accesiable to the child
*/
public class ParentClassLoader extends ClassLoader {
public ParentClassLoader(ClassLoader parent) {
super(parent);
}
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
return super.loadClass(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package org.apache.spark.executor

import java.net.{URLClassLoader, URL}

import org.apache.spark.util.ParentClassLoader

/**
* The addURL method in URLClassLoader is protected. We subclass it to make this accessible.
* We also make changes so user classes can come before the default classes.
Expand All @@ -36,11 +38,7 @@ private[spark] class ExecutorURLClassLoader(urls: Array[URL], parent: ClassLoade
}
}

object parentClassLoader extends ClassLoader(parent) {
override def findClass(name: String): Class[_] = {
super.findClass(name)
}
}
val parentClassLoader = new ParentClassLoader(parent)

override def findClass(name: String): Class[_] = {
if (!userFirst) {
Expand Down
Binary file added core/src/test/resources/fake-spark-class-2.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.executor

import java.io.File
import java.net.URLClassLoader

import org.scalatest.FunSuite

import org.apache.spark.test.FakeClass

class ExecutorURLClassLoaderSuite extends FunSuite {

val urls = List(new File("/home/holden/repos/spark/core/src/test/resources/fake-spark-class.jar").toURI.toURL).toArray
val urls2 = List(new File("/home/holden/repos/spark/core/src/test/resources/fake-spark-class-2.jar").toURI.toURL).toArray
test("child first") {
val parentLoader = new ExecutorURLClassLoader(urls2, null, false)
val classLoader = new ExecutorURLClassLoader(urls, parentLoader, true)
val fakeClass = classLoader.loadClass("org.apache.spark.test.FakeClass2").newInstance()
val fakeClassVersion = fakeClass.toString
assert(fakeClassVersion === "1")
}

test("parent first") {
val parentLoader = new URLClassLoader(urls2, null)
val classLoader = new ExecutorURLClassLoader(urls, parentLoader, false)
val fakeClass = classLoader.loadClass("org.apache.spark.test.FakeClass1").newInstance()
val fakeClassVersion = fakeClass.toString
assert(fakeClassVersion === "2")
}
}

0 comments on commit 22d83cb

Please sign in to comment.