Skip to content
This repository has been archived by the owner on Mar 13, 2021. It is now read-only.

Commit

Permalink
Allow multi-file URI to contain a fat jar
Browse files Browse the repository at this point in the history
There was an unecessary test that restricted fat jar classpath
building to a single valued function.uri. This change lifts the
restriction.

Also fixes tests that didn't randomize the grpc port.
  • Loading branch information
dsyer authored and trisberg committed Mar 14, 2018
1 parent e455911 commit be6f457
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
19 changes: 14 additions & 5 deletions src/main/java/io/projectriff/invoker/FunctionConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -133,16 +135,23 @@ public void init() {
}

private URL[] expand(URL[] urls) {
if (urls.length != 1 || !"file".equals(urls[0].getProtocol())) {
return urls;
List<URL> result = new ArrayList<>();
for (URL url : urls) {
result.addAll(expand(url));
}
return result.toArray(new URL[0]);
}

private List<URL> expand(URL url) {
if (!"file".equals(url.getProtocol())) {
return Collections.singletonList(url);
}
URL url = urls[0];
if (!url.toString().endsWith(".jar")) {
return urls;
return Collections.singletonList(url);
}
try {
JarFileArchive archive = new JarFileArchive(new File(url.toURI()));
return new ComputeLauncher(archive).getClassLoaderUrls();
return Arrays.asList(new ComputeLauncher(archive).getClassLoaderUrls());
}
catch (Exception e) {
throw new IllegalStateException("Cannot create class loader for " + url, e);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/projectriff/invoker/GrpcConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void setPort(int port) {

/** Start serving requests. */
@EventListener(ContextRefreshedEvent.class)
public void start() throws IOException {
public void start() {
try {
Function<Flux<?>, Flux<?>> function = catalog.lookup(Function.class,
functions.getFunctionName());
Expand All @@ -88,7 +88,7 @@ public void start() throws IOException {
this.server.start();
}
catch (IOException e) {
throw new IOException(String
throw new IllegalStateException(String
.format("gRPC server failed to start listening on port %d", port), e);
}
logger.info("Server started, listening on " + port);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
@TestPropertySource(properties = "function.uri=file:target/test-classes"
+ "?handler=io.projectriff.functions.Doubler,io.projectriff.functions.Frenchizer")
@TestPropertySource(properties = {"grpc.port=0", "function.uri=file:target/test-classes"
+ "?handler=io.projectriff.functions.Doubler,io.projectriff.functions.Frenchizer"})
public class ComposedJavaFunctionInvokerApplicationTests {

@Autowired
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/io/projectriff/invoker/FatJarPojoTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class FatJarPojoTests {
private File sampleJar;

private JavaFunctionInvokerApplication runner;
private File sampleDir;

@Before
public void init() {
Expand All @@ -60,6 +61,7 @@ public void close() throws Exception {

@Before
public void check() {
sampleDir = new File("target/it/pojo/target/classes");
sampleJar = new File(
"target/it/pojo/target/function-sample-pojo-1.0.0.BUILD-SNAPSHOT.jar");
Assume.assumeTrue("Sample jar does not exist: " + sampleJar, sampleJar.exists());
Expand All @@ -78,4 +80,16 @@ public void fatJar() throws Exception {
assertThat(result.getBody()).isEqualTo("[{\"value\":\"FOO\"}]");
}

@Test
public void fatJarAndDirectory() throws Exception {
runner.run("--server.port=" + port, "--grpc.port=0",
"--function.uri=" + sampleDir.toURI() + "," + sampleJar.toURI()
+ "?handler=uppercase&main=com.example.SampleApplication");
ResponseEntity<String> result = rest.exchange(RequestEntity
.post(new URI("http://localhost:" + port + "/"))
.contentType(MediaType.APPLICATION_JSON).body("{\"value\":\"foo\"}"),
String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[{\"value\":\"FOO\"}]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
@TestPropertySource(properties = "function.uri=file:target/test-classes"
+ "?handler=io.projectriff.functions.Doubler")
@TestPropertySource(properties = { "grpc.port=0", "function.uri=file:target/test-classes"
+ "?handler=io.projectriff.functions.Doubler" })
public class JavaFunctionInvokerApplicationTests {

@Autowired
Expand Down

0 comments on commit be6f457

Please sign in to comment.