Skip to content

Commit

Permalink
Make RequestHandler a functional interface (#8)
Browse files Browse the repository at this point in the history
* Use TypeResolver to resolve generic types

* Make Pulsar Function a functional interface

* address comments
  • Loading branch information
sijie committed Mar 4, 2018
1 parent 9e7d30c commit 7c871b0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* deserialize this in any way they want before processing. Any output goes into
* OutputStream.
*/
@FunctionalInterface
public interface RawRequestHandler {
/**
* Process the input coming as an input stream. The process function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Map, and List types) and for org.Json type. If this serialization approach does not
* meet your needs, you can use the byte stream handler defined in RawRequestHandler.
*/
@FunctionalInterface
public interface RequestHandler<I, O> {
/**
* Process the input.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import static com.google.common.base.Charsets.UTF_8;
import static org.testng.Assert.*;

public class JavaInstanceTest {
Expand Down Expand Up @@ -81,12 +82,30 @@ public void testLongRunningFunction() throws Exception {
JavaInstance instance = new JavaInstance(config, new LongRunningHandler(), log);
String testString = "ABC123";
JavaInstance.ExecutionResult result =
instance.handleMessage("1", "random", testString.getBytes());
instance.handleMessage("1", "random", testString.getBytes(UTF_8));

assertNotNull(result.getTimeoutException());
assertNull(result.getUserException());
}

/**
* Verify that be able to run lambda functions.
* @throws Exception
*/
@Test
public void testLambda() throws Exception {
JavaInstanceConfig config = new JavaInstanceConfig();
config.setTimeBudgetInMs(2000);
JavaInstance instance = new JavaInstance(
config,
(RequestHandler<String, String>) (input, context) -> input + "-lambda",
log);
String testString = "ABC123";
JavaInstance.ExecutionResult result =
instance.handleMessage("1", "random", testString.getBytes(UTF_8));
assertEquals(testString + "-lambda", result.getResultValue());
}

/**
* Verify that JavaInstance does not support functions that are not native Java types
* @throws Exception
Expand Down

0 comments on commit 7c871b0

Please sign in to comment.