Skip to content

Commit 14b4dc3

Browse files
Update the version of the CloudEvents SDK to 2.0.0RC2. (GoogleCloudPlatform#79)
Since the API is hopefully stable now, rename `ExperimentalCloudEventsFunction` to `CloudEventsFunction`. Add support for a `FUNCTION_SIGNATURE_TYPE` of `cloudevent`. Treat it the same as `background`. (We don't really need `FUNCTION_SIGNATURE_TYPE`, since the interface implemented by the function class tells us what the signature is.) Simplify the integration test slightly by removing some intermediary helper methods.
1 parent 0bf9faf commit 14b4dc3

File tree

9 files changed

+48
-53
lines changed

9 files changed

+48
-53
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ properties
4444

4545
# IDE
4646
.vscode/
47+
/invoker/target/

functions-framework-api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<dependency>
5353
<groupId>io.cloudevents</groupId>
5454
<artifactId>cloudevents-api</artifactId>
55-
<version>2.0.0-milestone4</version>
55+
<version>2.0.0.RC2</version>
5656
</dependency>
5757
</dependencies>
5858

functions-framework-api/src/main/java/com/google/cloud/functions/ExperimentalCloudEventsFunction.java renamed to functions-framework-api/src/main/java/com/google/cloud/functions/CloudEventsFunction.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
/**
66
* Represents a Cloud Function that is activated by an event and parsed into a {@link CloudEvent} object.
7-
* Because the {@link CloudEvent} API is not yet stable, a function implemented using this class may not
8-
* build or work correctly with later versions of that API. Once the API is stable, this interface will
9-
* become {@code CloudEventsFunction} and will also be stable.
107
*/
118
@FunctionalInterface
12-
public interface ExperimentalCloudEventsFunction {
9+
public interface CloudEventsFunction {
1310
/**
1411
* Called to service an incoming event. This interface is implemented by user code to
1512
* provide the action for a given background function. If this method throws any exception

invoker/core/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@
5252
<dependency>
5353
<groupId>io.cloudevents</groupId>
5454
<artifactId>cloudevents-core</artifactId>
55-
<version>2.0.0-milestone4</version>
55+
<version>2.0.0.RC2</version>
5656
</dependency>
5757
<dependency>
5858
<groupId>io.cloudevents</groupId>
5959
<artifactId>cloudevents-http-basic</artifactId>
60-
<version>2.0.0-milestone4</version>
60+
<version>2.0.0.RC2</version>
6161
</dependency>
6262
<dependency>
6363
<groupId>io.cloudevents</groupId>
6464
<artifactId>cloudevents-json-jackson</artifactId>
65-
<version>2.0.0-milestone4</version>
65+
<version>2.0.0.RC2</version>
6666
</dependency>
6767
<dependency>
6868
<groupId>com.google.code.gson</groupId>

invoker/core/src/main/java/com/google/cloud/functions/invoker/BackgroundFunctionExecutor.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.google.cloud.functions.BackgroundFunction;
2222
import com.google.cloud.functions.Context;
23-
import com.google.cloud.functions.ExperimentalCloudEventsFunction;
2423
import com.google.cloud.functions.RawBackgroundFunction;
2524
import com.google.gson.Gson;
2625
import com.google.gson.GsonBuilder;
@@ -46,6 +45,7 @@
4645
import javax.servlet.http.HttpServlet;
4746
import javax.servlet.http.HttpServletRequest;
4847
import javax.servlet.http.HttpServletResponse;
48+
import com.google.cloud.functions.CloudEventsFunction;
4949

5050
/** Executes the user's background function. */
5151
public final class BackgroundFunctionExecutor extends HttpServlet {
@@ -60,7 +60,7 @@ private BackgroundFunctionExecutor(FunctionExecutor<?> functionExecutor) {
6060
private enum FunctionKind {
6161
BACKGROUND(BackgroundFunction.class),
6262
RAW_BACKGROUND(RawBackgroundFunction.class),
63-
CLOUD_EVENTS(ExperimentalCloudEventsFunction.class);
63+
CLOUD_EVENTS(CloudEventsFunction.class);
6464

6565
static final List<FunctionKind> VALUES = Arrays.asList(values());
6666

@@ -79,7 +79,7 @@ static Optional<FunctionKind> forClass(Class<?> functionClass) {
7979
/**
8080
* Optionally makes a {@link BackgroundFunctionExecutor} for the given class, if it implements one
8181
* of {@link BackgroundFunction}, {@link RawBackgroundFunction}, or
82-
* {@link ExperimentalCloudEventsFunction}. Otherwise returns {@link Optional#empty()}.
82+
* {@link CloudEventsFunction}. Otherwise returns {@link Optional#empty()}.
8383
*
8484
* @param functionClass the class of a possible background function implementation.
8585
* @throws RuntimeException if the given class does implement one of the required interfaces, but we are
@@ -98,7 +98,7 @@ public static Optional<BackgroundFunctionExecutor> maybeForClass(Class<?> functi
9898
*
9999
* @throws RuntimeException if either the class does not implement one of
100100
* {@link BackgroundFunction}, {@link RawBackgroundFunction}, or
101-
* {@link ExperimentalCloudEventsFunction}; or we are unable to construct an instance using its no-arg
101+
* {@link CloudEventsFunction}; or we are unable to construct an instance using its no-arg
102102
* constructor.
103103
*/
104104
public static BackgroundFunctionExecutor forClass(Class<?> functionClass) {
@@ -143,7 +143,7 @@ private static BackgroundFunctionExecutor forClass(Class<?> functionClass, Funct
143143
executor = new TypedFunctionExecutor<>(maybeTargetType.get(), backgroundFunction);
144144
break;
145145
case CLOUD_EVENTS:
146-
executor = new CloudEventFunctionExecutor((ExperimentalCloudEventsFunction) instance);
146+
executor = new CloudEventFunctionExecutor((CloudEventsFunction) instance);
147147
break;
148148
default: // can't happen, we've listed all the FunctionKind values already.
149149
throw new AssertionError(functionKind);
@@ -299,9 +299,9 @@ void serviceCloudEvent(CloudEvent cloudEvent) throws Exception {
299299
}
300300

301301
private static class CloudEventFunctionExecutor extends FunctionExecutor<Void> {
302-
private final ExperimentalCloudEventsFunction function;
302+
private final CloudEventsFunction function;
303303

304-
CloudEventFunctionExecutor(ExperimentalCloudEventsFunction function) {
304+
CloudEventFunctionExecutor(CloudEventsFunction function) {
305305
super(function.getClass());
306306
this.function = function;
307307
}

invoker/core/src/main/java/com/google/cloud/functions/invoker/runner/Invoker.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
import com.beust.jcommander.JCommander;
2020
import com.beust.jcommander.Parameter;
2121
import com.beust.jcommander.ParameterException;
22-
import com.google.cloud.functions.BackgroundFunction;
23-
import com.google.cloud.functions.ExperimentalCloudEventsFunction;
2422
import com.google.cloud.functions.HttpFunction;
25-
import com.google.cloud.functions.RawBackgroundFunction;
2623
import com.google.cloud.functions.invoker.BackgroundFunctionExecutor;
2724
import com.google.cloud.functions.invoker.HttpFunctionExecutor;
2825
import com.google.cloud.functions.invoker.gcf.JsonLogHandler;
@@ -247,17 +244,24 @@ public void startServer() throws Exception {
247244
Class<?> functionClass = loadFunctionClass();
248245

249246
HttpServlet servlet;
250-
if ("http".equals(functionSignatureType)) {
251-
servlet = HttpFunctionExecutor.forClass(functionClass);
252-
} else if ("event".equals(functionSignatureType)) {
253-
servlet = BackgroundFunctionExecutor.forClass(functionClass);
254-
} else if (functionSignatureType == null) {
255-
servlet = servletForDeducedSignatureType(functionClass);
247+
if (functionSignatureType == null) {
248+
servlet = servletForDeducedSignatureType(functionClass);
256249
} else {
257-
String error = String.format(
258-
"Function signature type %s is unknown; should be \"http\" or \"event\"",
259-
functionSignatureType);
260-
throw new RuntimeException(error);
250+
switch (functionSignatureType) {
251+
case "http":
252+
servlet = HttpFunctionExecutor.forClass(functionClass);
253+
break;
254+
case "event":
255+
case "cloudevent":
256+
servlet = BackgroundFunctionExecutor.forClass(functionClass);
257+
break;
258+
default:
259+
String error = String.format(
260+
"Function signature type %s is unknown; should be \"http\", \"event\","
261+
+ " or \"cloudevent\"",
262+
functionSignatureType);
263+
throw new RuntimeException(error);
264+
}
261265
}
262266
ServletHolder servletHolder = new ServletHolder(servlet);
263267
servletHolder.getRegistration().setMultipartConfig(new MultipartConfigElement(""));

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

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ private void backgroundTest(String target) throws Exception {
364364
.build();
365365

366366
backgroundTest(
367+
SignatureType.BACKGROUND,
367368
fullTarget(target),
368369
ImmutableList.of(gcfTestCase, cloudEventsStructuredTestCase, cloudEventsBinaryTestCase));
369370
}
@@ -401,6 +402,7 @@ public void nativeCloudEvent() throws Exception {
401402
.build();
402403

403404
backgroundTest(
405+
SignatureType.CLOUD_EVENT,
404406
fullTarget("CloudEventSnoop"),
405407
ImmutableList.of(cloudEventsStructuredTestCase, cloudEventsBinaryTestCase));
406408
}
@@ -472,7 +474,9 @@ public void classpathOptionHttp() throws Exception {
472474
.setUrl("/?class=" + INTERNAL_CLASS.getName())
473475
.setExpectedResponseText("OK")
474476
.build();
475-
testHttpFunction("com.example.functionjar.Foreground",
477+
testFunction(
478+
SignatureType.HTTP,
479+
"com.example.functionjar.Foreground",
476480
ImmutableList.of("--classpath", functionJarString()),
477481
ImmutableList.of(testCase));
478482
}
@@ -487,7 +491,9 @@ public void classpathOptionBackground() throws Exception {
487491
JsonObject json = gson.fromJson(originalJson, JsonObject.class);
488492
JsonObject jsonData = json.getAsJsonObject("data");
489493
jsonData.addProperty("class", INTERNAL_CLASS.getName());
490-
testBackgroundFunction("com.example.functionjar.Background",
494+
testFunction(
495+
SignatureType.BACKGROUND,
496+
"com.example.functionjar.Background",
491497
ImmutableList.of("--classpath", functionJarString()),
492498
ImmutableList.of(TestCase.builder().setRequestText(json.toString()).build()));
493499
}
@@ -497,11 +503,13 @@ public void classpathOptionBackground() throws Exception {
497503
// event. We start with a fixed body and insert into its JSON an extra property that tells the
498504
// function where to write what it received. We have to do this since background functions, by
499505
// design, don't return a value.
500-
private void backgroundTest(String functionTarget, List<TestCase> testCases) throws Exception {
506+
private void backgroundTest(
507+
SignatureType signatureType, String functionTarget, List<TestCase> testCases)
508+
throws Exception {
501509
for (TestCase testCase : testCases) {
502510
File snoopFile = testCase.snoopFile().get();
503511
snoopFile.delete();
504-
testBackgroundFunction(functionTarget, ImmutableList.of(testCase));
512+
testFunction(signatureType, functionTarget, ImmutableList.of(), ImmutableList.of(testCase));
505513
String snooped = new String(Files.readAllBytes(snoopFile.toPath()), StandardCharsets.UTF_8);
506514
Gson gson = new Gson();
507515
JsonObject snoopedJson = gson.fromJson(snooped, JsonObject.class);
@@ -522,23 +530,7 @@ private void checkSnoopFile(TestCase testCase) throws IOException {
522530
}
523531

524532
private void testHttpFunction(String target, List<TestCase> testCases) throws Exception {
525-
testHttpFunction(target, ImmutableList.of(), testCases);
526-
}
527-
528-
private void testHttpFunction(
529-
String target, ImmutableList<String> extraArgs, List<TestCase> testCases) throws Exception {
530-
testFunction(SignatureType.HTTP, target, extraArgs, testCases);
531-
}
532-
533-
private void testBackgroundFunction(String target, List<TestCase> testCases)
534-
throws Exception {
535-
testBackgroundFunction(target, ImmutableList.of(), testCases);
536-
}
537-
538-
private void testBackgroundFunction(
539-
String target, ImmutableList<String> extraArgs, List<TestCase> testCases)
540-
throws Exception {
541-
testFunction(SignatureType.BACKGROUND, target, extraArgs, testCases);
533+
testFunction(SignatureType.HTTP, target, ImmutableList.of(), testCases);
542534
}
543535

544536
private void testFunction(
@@ -584,7 +576,8 @@ private void testFunction(
584576

585577
private enum SignatureType {
586578
HTTP("http"),
587-
BACKGROUND("event");
579+
BACKGROUND("event"),
580+
CLOUD_EVENT("cloudevent");
588581

589582
private final String name;
590583

invoker/core/src/test/java/com/google/cloud/functions/invoker/testfunctions/CloudEventSnoop.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
import static java.nio.charset.StandardCharsets.UTF_8;
44

5-
import com.google.cloud.functions.ExperimentalCloudEventsFunction;
65
import com.google.gson.Gson;
76
import com.google.gson.JsonObject;
87
import io.cloudevents.CloudEvent;
98
import io.cloudevents.core.format.EventFormat;
109
import io.cloudevents.core.provider.EventFormatProvider;
1110
import io.cloudevents.jackson.JsonFormat;
1211
import java.io.FileOutputStream;
12+
import com.google.cloud.functions.CloudEventsFunction;
1313

14-
public class CloudEventSnoop implements ExperimentalCloudEventsFunction {
14+
public class CloudEventSnoop implements CloudEventsFunction {
1515
@Override
1616
public void accept(CloudEvent event) throws Exception {
1717
String payloadJson = new String(event.getData().toBytes(), UTF_8);

invoker/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>com.google.cloud.functions</groupId>
4444
<artifactId>functions-framework-api</artifactId>
45-
<version>1.0.3</version>
45+
<version>1.0.4-SNAPSHOT</version>
4646
</dependency>
4747
</dependencies>
4848
</dependencyManagement>

0 commit comments

Comments
 (0)