Skip to content

Commit 8371e04

Browse files
author
Ace Nassri
authored
GCF: misc. idiomatic fixes (GoogleCloudPlatform#2630)
* Remove snippets directory * Use CONSTANT_CASE for all p-s-f vars * Remove extraneous <type> param * Add GCF maven plugin to all pom's * Use IODH pattern * Fix test failures * Add top-level pom for testing * Fix lint errors raised by top-level POM * [spotbugs] helloworld: swap if-else for get().orElse() * [spotbugs] parseContentType: if-else --> switch-case * [spotbugs] put back strangely-missing line in imagemagick sample * [spotbugs] misc imagemagick fixes * rename InfiniteRetryPubsub to RetryTimeout + fix spotbugs errors * [spotbugs] fix slack errors * [spotbugs] fix minor errors in logging * [spotbugs] firebase misc fixes * [spotbugs] ocr misc fixes * Make gson declaration consistent + remove extra env-var mocking * [spotbugs] fix http bugs + checkstyle * Disable parent pom testing, take 1: remove shared config * Disable tests, take 2: add kokoro.skipTests POM property * Make eventpojos fields private * [comments] http: remove excess deps * [comments] Remove xml tag from POMs * [comments] update README * [comments] update README * [comments] use more verbose logging for exceptions * Address Ray's comments * [comments 2] add xml lines back to POMs * Remove parent pom + add bash cmd to run all tests
1 parent 8c8fd82 commit 8371e04

File tree

79 files changed

+410
-335
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+410
-335
lines changed

.kokoro/tests/run_tests.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ for file in **/pom.xml; do
158158
continue
159159
fi
160160

161+
# Skip tests with the kokoro.skipTests property defined in pom.xml
162+
SKIP_TESTS=$(mvn -q -DforceStdout help:evaluate -Dexpression=kokoro.skipTests || grep -i true)
163+
if [[ -z $? ]]; then
164+
echo -e "\n Skipping tests: kokoro.skipTests set to 'true' in pom.xml\n"
165+
continue
166+
fi
167+
161168
# Use maven to execute the tests for the project.
162169
mvn -q --batch-mode --fail-at-end clean verify \
163170
-Dfile.encoding="UTF-8" \

SAMPLE_FORMAT.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ Run tests locally with commands:
154154
* Maven: `mvn verify`
155155
* Gradle: `gradle build test`
156156

157+
To run the `functions` tests (or other tests without a parent `pom.xml`), use the following command:
158+
159+
```
160+
cd functions
161+
find */pom.xml | xargs -I {} echo $(pwd)/{} | xargs -I {} dirname {} | xargs -I {} sh -c "cd {} && mvn clean verify"
162+
163+
```
164+
165+
157166
### Gradle Specifics
158167
#### NEEDS WORK
159168
Your `build.gradle` should have the following section:

functions/README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,22 @@ environment.
1212
## Samples
1313

1414
* [Hello World](helloworld/)
15-
* [Background](background/)
16-
* [Callbacks](messages/)
15+
* [Concepts](concepts/)
16+
* [Firebase](firebase/)
1717
* [Cloud Pub/Sub](pubsub/)
1818
* [HTTP](http/)
19-
* [Logging & Monitoring](log/)
19+
* [Logging & Monitoring](logging/)
2020
* [Slack](slack/)
21+
* [OCR tutorial](ocr/)
22+
* [ImageMagick](imagemagick/)
23+
* [CI/CD setup](ci_cd/)
24+
25+
## Running Functions Locally
26+
The [Java Functions Framework](https://github.com/GoogleCloudPlatform/functions-framework-java)
27+
Maven plugin (`com.google.cloud.functions:function-maven-plugin`) allows you to run Java Cloud
28+
Functions code on your local machine for local development and testing purposes. Use the following
29+
Maven command to run a function locally:
30+
31+
```
32+
mvn function:run -Drun.functionTarget=your.package.yourFunction
33+
```

functions/snippets/concepts/pom.xml renamed to functions/concepts/pom.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102
<groupId>com.google.cloud.functions</groupId>
103103
<artifactId>functions-framework-api</artifactId>
104104
<version>1.0.1</version>
105-
<type>jar</type>
106105
</dependency>
107106
</dependencies>
108107

@@ -126,6 +125,20 @@
126125

127126
<build>
128127
<plugins>
128+
<plugin>
129+
<!--
130+
Google Cloud Functions Framework Maven plugin
131+
132+
This plugin allows you to run Cloud Functions Java code
133+
locally. Use the following terminal command to run a
134+
given function locally:
135+
136+
mvn function:run -Drun.functionTarget=your.package.yourFunction
137+
-->
138+
<groupId>com.google.cloud.functions</groupId>
139+
<artifactId>function-maven-plugin</artifactId>
140+
<version>0.9.1</version>
141+
</plugin>
129142
<plugin>
130143
<groupId>org.apache.maven.plugins</groupId>
131144
<artifactId>maven-surefire-plugin</artifactId>

functions/snippets/concepts/src/main/java/com/example/functions/concepts/FileSystem.java renamed to functions/concepts/src/main/java/com/example/functions/concepts/FileSystem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public void service(HttpRequest request, HttpResponse response)
3434
File currentDirectory = new File(".");
3535
File[] files = currentDirectory.listFiles();
3636
BufferedWriter writer = response.getWriter();
37-
writer.write("Files: \n");
37+
writer.write("Files: %n");
3838
for (File f : files) {
39-
writer.write(String.format("\t%s\n", f.getName()));
39+
writer.write(String.format("\t%s%n", f.getName()));
4040
}
4141
}
4242
}

functions/snippets/concepts/src/main/java/com/example/functions/concepts/LazyFields.java renamed to functions/concepts/src/main/java/com/example/functions/concepts/LazyFields.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,31 @@ public class LazyFields implements HttpFunction {
3030
// Always initialized (at cold-start)
3131
// Warning: Class variables used in Servlet classes must be thread-safe,
3232
// or else might introduce race conditions in your code.
33-
private static final int nonLazyGlobal = fileWideComputation();
33+
private static final int NON_LAZY_GLOBAL = fileWideComputation();
34+
3435
// Declared at cold-start, but only initialized if/when the function executes
35-
private static Integer lazyGlobal = null;
36+
// Uses the "initialization-on-demand holder" idiom
37+
// More information: https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
38+
private static class LazyGlobalHolder {
39+
// Making the default constructor private prohibits instantiation of this class
40+
private LazyGlobalHolder() {}
41+
42+
// This value is initialized only if (and when) the getLazyGlobal() function below is called
43+
private static final Integer INSTANCE = functionSpecificComputation();
44+
45+
private static Integer getInstance() {
46+
return LazyGlobalHolder.INSTANCE;
47+
}
48+
}
3649

3750
@Override
3851
public void service(HttpRequest request, HttpResponse response)
3952
throws IOException {
40-
// This value is initialized only if (and when) the function is called
41-
if (lazyGlobal == null) {
42-
lazyGlobal = functionSpecificComputation();
43-
}
53+
Integer lazyGlobal = LazyGlobalHolder.getInstance();
4454

4555
BufferedWriter writer = response.getWriter();
46-
writer.write(String.format("Lazy global: %s; non-lazy global: %s", lazyGlobal, nonLazyGlobal));
56+
writer.write(String.format("Lazy global: %s; non-lazy global: %s",
57+
lazyGlobal, NON_LAZY_GLOBAL));
4758
}
4859

4960
private static int functionSpecificComputation() {

functions/snippets/concepts/src/main/java/com/example/functions/concepts/RetryPubSub.java renamed to functions/concepts/src/main/java/com/example/functions/concepts/RetryPubSub.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@
2323
import com.google.gson.Gson;
2424
import com.google.gson.JsonElement;
2525
import com.google.gson.JsonObject;
26+
import java.nio.charset.StandardCharsets;
2627
import java.util.Base64;
2728
import java.util.logging.Logger;
2829

2930
public class RetryPubSub implements BackgroundFunction<PubSubMessage> {
3031
private static final Logger LOGGER = Logger.getLogger(RetryPubSub.class.getName());
3132

3233
// Use Gson (https://github.com/google/gson) to parse JSON content.
33-
private Gson gsonParser = new Gson();
34+
private static final Gson gson = new Gson();
3435

3536
@Override
3637
public void accept(PubSubMessage message, Context context) {
37-
String bodyJson = new String(Base64.getDecoder().decode(message.getData()));
38-
JsonElement bodyElement = gsonParser.fromJson(bodyJson, JsonElement.class);
38+
String bodyJson = new String(
39+
Base64.getDecoder().decode(message.getData()), StandardCharsets.UTF_8);
40+
JsonElement bodyElement = gson.fromJson(bodyJson, JsonElement.class);
3941

4042
// Get the value of the "retry" JSON parameter, if one exists
4143
boolean retry = false;

0 commit comments

Comments
 (0)