Skip to content

Commit 61b5bef

Browse files
chore(implementation)!: use Jetty-12 core without servlets
Port the invoker to upgrade to Eclipse Jetty-12 version 12. Specifically using the new core APIs of Eclipse Jetty-12 that allow the overhead of a Servlet container to be avoided. BREAKING CHANGE: use Java 17 or above, as required by Eclipse Jetty-12. Signed-off-by: Lachlan Roberts <[email protected]>
1 parent ed98269 commit 61b5bef

File tree

2 files changed

+72
-58
lines changed

2 files changed

+72
-58
lines changed

invoker/core/src/test/java/com/google/cloud/functions/invoker/IntegrationTest.java

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,8 @@ abstract static class TestCase {
174174
abstract int expectedResponseCode();
175175

176176
/**
177-
* Expected response headers map, header name -> value.
178-
* Value "*" asserts the header is present with any value.
179-
* Value "-" asserts the header is not present.
177+
* Expected response headers map, header name -> value. Value "*" asserts the header is present
178+
* with any value. Value "-" asserts the header is not present.
180179
*
181180
* @return the expected response headers for this test case.
182181
*/
@@ -268,8 +267,7 @@ public void helloWorld() throws Exception {
268267
fullTarget("HelloWorld"),
269268
ImmutableList.of(
270269
TestCase.builder()
271-
.setExpectedResponseHeaders(ImmutableMap.of(
272-
"Content-Length", "*"))
270+
.setExpectedResponseHeaders(ImmutableMap.of("Content-Length", "*"))
273271
.setExpectedResponseText("hello\n")
274272
.build(),
275273
FAVICON_TEST_CASE,
@@ -315,23 +313,23 @@ public void bufferedWrites() throws Exception {
315313
TestCase.builder()
316314
.setUrl("/target?writes=2")
317315
.setExpectedResponseText("write 0\nwrite 1\n")
318-
.setExpectedResponseHeaders(ImmutableMap.of(
319-
"x-write-0", "true",
320-
"x-write-1", "true",
321-
"x-written", "true",
322-
"Content-Length", "16"
323-
))
316+
.setExpectedResponseHeaders(
317+
ImmutableMap.of(
318+
"x-write-0", "true",
319+
"x-write-1", "true",
320+
"x-written", "true",
321+
"Content-Length", "16"))
324322
.build(),
325323
TestCase.builder()
326324
.setUrl("/target?writes=2&flush=true")
327325
.setExpectedResponseText("write 0\nwrite 1\n")
328-
.setExpectedResponseHeaders(ImmutableMap.of(
329-
"x-write-0", "true",
330-
"x-write-1", "true",
331-
"x-written", "-",
332-
"Transfer-Encoding", "chunked"))
333-
.build()
334-
));
326+
.setExpectedResponseHeaders(
327+
ImmutableMap.of(
328+
"x-write-0", "true",
329+
"x-write-1", "true",
330+
"x-written", "-",
331+
"Transfer-Encoding", "chunked"))
332+
.build()));
335333
}
336334

337335
@Test
@@ -683,10 +681,12 @@ public void packageless() throws Exception {
683681
public void multipart() throws Exception {
684682
MultiPartRequestContent multiPartRequestContent = new MultiPartRequestContent();
685683
byte[] bytes = new byte[17];
686-
multiPartRequestContent.addPart(new ContentSourcePart("bytes", null,
687-
HttpFields.EMPTY, new ByteBufferRequestContent(ByteBuffer.wrap(bytes))));
688-
multiPartRequestContent.addPart(new MultiPart.ContentSourcePart("string", null,
689-
HttpFields.EMPTY, new StringRequestContent("1234567890")));
684+
multiPartRequestContent.addPart(
685+
new ContentSourcePart(
686+
"bytes", null, HttpFields.EMPTY, new ByteBufferRequestContent(ByteBuffer.wrap(bytes))));
687+
multiPartRequestContent.addPart(
688+
new MultiPart.ContentSourcePart(
689+
"string", null, HttpFields.EMPTY, new StringRequestContent("1234567890")));
690690
multiPartRequestContent.close();
691691

692692
String expectedResponse =
@@ -842,27 +842,39 @@ private void testFunction(
842842
String uri = "http://localhost:" + serverPort + testCase.url();
843843
Request request = httpClient.POST(uri);
844844

845-
request.headers(headers -> {
846-
testCase.httpContentType().ifPresent(contentType -> headers.put(HttpHeader.CONTENT_TYPE, contentType));
847-
testCase.httpHeaders().forEach(headers::put);
848-
});
845+
request.headers(
846+
headers -> {
847+
testCase
848+
.httpContentType()
849+
.ifPresent(contentType -> headers.put(HttpHeader.CONTENT_TYPE, contentType));
850+
testCase.httpHeaders().forEach(headers::put);
851+
});
849852
request.body(testCase.requestContent());
850853
ContentResponse response = request.send();
851854
expect
852855
.withMessage("Response to %s is %s %s", uri, response.getStatus(), response.getReason())
853856
.that(response.getStatus())
854857
.isEqualTo(testCase.expectedResponseCode());
855-
testCase.expectedResponseHeaders().ifPresent(expectedResponseHeaders -> {
856-
for (Map.Entry<String, String> entry : expectedResponseHeaders.entrySet()) {
857-
if ("*".equals(entry.getValue())) {
858-
expect.that(response.getHeaders().getFieldNamesCollection()).contains(entry.getKey());
859-
} else if ("-".equals(entry.getValue())) {
860-
expect.that(response.getHeaders().getFieldNamesCollection()).doesNotContain(entry.getKey());
861-
} else {
862-
expect.that(response.getHeaders().getValuesList(entry.getKey())).contains(entry.getValue());
863-
}
864-
}
865-
});
858+
testCase
859+
.expectedResponseHeaders()
860+
.ifPresent(
861+
expectedResponseHeaders -> {
862+
for (Map.Entry<String, String> entry : expectedResponseHeaders.entrySet()) {
863+
if ("*".equals(entry.getValue())) {
864+
expect
865+
.that(response.getHeaders().getFieldNamesCollection())
866+
.contains(entry.getKey());
867+
} else if ("-".equals(entry.getValue())) {
868+
expect
869+
.that(response.getHeaders().getFieldNamesCollection())
870+
.doesNotContain(entry.getKey());
871+
} else {
872+
expect
873+
.that(response.getHeaders().getValuesList(entry.getKey()))
874+
.contains(entry.getValue());
875+
}
876+
}
877+
});
866878
testCase
867879
.expectedResponseText()
868880
.ifPresent(text -> expect.that(response.getContentAsString()).isEqualTo(text));

invoker/core/src/test/java/com/google/cloud/functions/invoker/http/HttpTest.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ private interface HttpRequestTest {
115115

116116
/**
117117
* Tests methods on the {@link HttpRequest} object while the request is being serviced. We are not
118-
* guaranteed that the underlying {@link Request} object will still be valid when the
119-
* request completes, and in fact in Jetty it isn't. So we perform the checks in the context of
120-
* the handler, and report any exception back to the test method.
118+
* guaranteed that the underlying {@link Request} object will still be valid when the request
119+
* completes, and in fact in Jetty it isn't. So we perform the checks in the context of the
120+
* handler, and report any exception back to the test method.
121121
*/
122122
@Test
123123
public void httpRequestMethods() throws Exception {
@@ -198,12 +198,13 @@ private void httpRequestMethods(
198198
org.eclipse.jetty.client.Request request =
199199
httpClient
200200
.POST(uri)
201-
.headers(m -> {
202-
m.add(HttpHeader.CONTENT_TYPE, "text/plain; charset=utf-8");
203-
m.add("foo", "bar");
204-
m.add("foo", "baz");
205-
m.add("CaSe-SeNsItIvE", "VaLuE");
206-
})
201+
.headers(
202+
m -> {
203+
m.add(HttpHeader.CONTENT_TYPE, "text/plain; charset=utf-8");
204+
m.add("foo", "bar");
205+
m.add("foo", "baz");
206+
m.add("CaSe-SeNsItIvE", "VaLuE");
207+
})
207208
.body(new StringRequestContent(TEST_BODY));
208209
ContentResponse response = request.send();
209210
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
@@ -249,16 +250,18 @@ public void multiPartRequest() throws Exception {
249250
httpClient.start();
250251
String uri = "http://localhost:" + serverPort + "/";
251252
MultiPartRequestContent multiPart = new MultiPartRequestContent();
252-
HttpFields textHttpFields = HttpFields.build()
253-
.add("foo", "bar");
254-
multiPart.addPart(new MultiPart.ContentSourcePart("text", null, textHttpFields,
255-
new StringRequestContent(TEST_BODY)));
256-
HttpFields.Mutable bytesHttpFields = HttpFields.build()
257-
.add("foo", "baz")
258-
.add("foo", "buh");
253+
HttpFields textHttpFields = HttpFields.build().add("foo", "bar");
254+
multiPart.addPart(
255+
new MultiPart.ContentSourcePart(
256+
"text", null, textHttpFields, new StringRequestContent(TEST_BODY)));
257+
HttpFields.Mutable bytesHttpFields = HttpFields.build().add("foo", "baz").add("foo", "buh");
259258
assertThat(bytesHttpFields.getValuesList("foo")).containsExactly("baz", "buh");
260-
multiPart.addPart(new MultiPart.ContentSourcePart("binary", "/tmp/binary.x",
261-
bytesHttpFields, new ByteBufferRequestContent(ByteBuffer.wrap(RANDOM_BYTES))));
259+
multiPart.addPart(
260+
new MultiPart.ContentSourcePart(
261+
"binary",
262+
"/tmp/binary.x",
263+
bytesHttpFields,
264+
new ByteBufferRequestContent(ByteBuffer.wrap(RANDOM_BYTES))));
262265
multiPart.close();
263266
HttpRequestTest test =
264267
request -> {
@@ -290,9 +293,8 @@ public void multiPartRequest() throws Exception {
290293
};
291294
try (SimpleServer server = new SimpleServer(testHandler)) {
292295
testReference.set(test);
293-
org.eclipse.jetty.client.Request request = httpClient.POST(uri)
294-
.headers(m -> m.put("foo", "oof"))
295-
.body(multiPart);
296+
org.eclipse.jetty.client.Request request =
297+
httpClient.POST(uri).headers(m -> m.put("foo", "oof")).body(multiPart);
296298
ContentResponse response = request.send();
297299
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
298300
throwIfNotNull(exceptionReference.get());

0 commit comments

Comments
 (0)