Skip to content

Commit

Permalink
Fix: check function implements correct interface (apache#4844)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrypeng authored and sijie committed Jul 30, 2019
1 parent b0793a1 commit 3c9136b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,20 @@ public static void inferMissingArguments(FunctionConfig functionConfig) {

private static void doJavaChecks(FunctionConfig functionConfig, ClassLoader clsLoader) {

try {
Class functionClass = clsLoader.loadClass(functionConfig.getClassName());

if (!org.apache.pulsar.functions.api.Function.class.isAssignableFrom(functionClass)
&& !java.util.function.Function.class.isAssignableFrom(functionClass)) {
throw new IllegalArgumentException(
String.format("Function class %s does not implement the correct interface",
functionClass.getName()));
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(
String.format("Function class %s must be in class path", functionConfig.getClassName()), e);
}

Class<?>[] typeArgs;
try {
typeArgs = FunctionCommon.getFunctionTypes(functionConfig, clsLoader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
Expand Down Expand Up @@ -109,6 +110,13 @@ public String process(String input, Context context) {
}
}

private static final class WrongFunction implements Consumer<String> {
@Override
public void accept(String s) {

}
}

private static final String tenant = "test-tenant";
private static final String namespace = "test-namespace";
private static final String function = "test-function";
Expand Down Expand Up @@ -438,6 +446,27 @@ public void testRegisterFunctionHttpUrl() {
}
}

@Test(expectedExceptions = RestException.class, expectedExceptionsMessageRegExp = "Function class .*. does not implement the correct interface")
public void testRegisterFunctionImplementWrongInterface() {
try {
testRegisterFunctionMissingArguments(
tenant,
namespace,
function,
mockedInputStream,
topicsToSerDeClassName,
mockedFormData,
outputTopic,
outputSerdeClassName,
WrongFunction.class.getName(),
parallelism,
null);
} catch (RestException re){
assertEquals(re.getResponse().getStatusInfo(), Response.Status.BAD_REQUEST);
throw re;
}
}

private void testRegisterFunctionMissingArguments(
String tenant,
String namespace,
Expand Down

0 comments on commit 3c9136b

Please sign in to comment.