Skip to content

Commit 632abc4

Browse files
authored
feat: Add Functions tips scopes (GoogleCloudPlatform#1586)
* feat: Add functions_tips_scopes * Fix: Fix tests by debugging locally * Update functions/snippets/src/main/java/Scopes.java Co-Authored-By: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> * fix: functions_tips_scopes add util methods * Update Scopes.java * Update Scopes.java
1 parent 7f6839f commit 632abc4

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START functions_tips_scopes]
18+
19+
import java.io.IOException;
20+
import java.io.PrintWriter;
21+
import java.util.Arrays;
22+
import javax.servlet.http.HttpServletRequest;
23+
import javax.servlet.http.HttpServletResponse;
24+
25+
public class Scopes {
26+
// Global (instance-wide) scope
27+
// This computation runs at instance cold-start.
28+
// Warning: Class variables used in Servlet classes must be thread-safe,
29+
// or else might introduce race conditions in your code.
30+
private static final int InstanceVar = heavyComputation();
31+
32+
public void scopeDemo(HttpServletRequest request, HttpServletResponse response)
33+
throws IOException {
34+
// Per-function scope
35+
// This computation runs every time this function is called
36+
int functionVar = lightComputation();
37+
38+
PrintWriter writer = response.getWriter();
39+
writer.write(String.format("Instance: %s; function: %s", InstanceVar, functionVar));
40+
}
41+
42+
private static int lightComputation() {
43+
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
44+
return Arrays.stream(numbers).sum();
45+
}
46+
47+
private static int heavyComputation() {
48+
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
49+
return Arrays.stream(numbers).reduce((t, x) -> t * x).getAsInt();
50+
}
51+
}
52+
53+
// [END functions_tips_scopes]

functions/snippets/src/test/java/SnippetsTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ public void parseContentTypeTest_form() throws IOException {
156156
assertThat(responseOut.toString(), containsString("Hello John!"));
157157
}
158158

159+
@Test
160+
public void scopesTest() throws IOException {
161+
new Scopes().scopeDemo(request, response);
162+
163+
assertThat(responseOut.toString(), containsString("Instance:"));
164+
}
165+
159166
@Test
160167
public void retrieveLogsTest() throws IOException {
161168
new RetrieveLogs().retrieveLogs(request, response);

0 commit comments

Comments
 (0)