Skip to content

Commit c655f10

Browse files
author
Ace Nassri
authored
Merge pull request GoogleCloudPlatform#1199 from GoogleCloudPlatform/functions-scopes
feat(functions): add scopes sample
2 parents 62b8ad2 + 4778f77 commit c655f10

File tree

6 files changed

+261
-0
lines changed

6 files changed

+261
-0
lines changed

functions/tips_scopes/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"google/cloud-functions-framework": "^0.7.1"
4+
}
5+
}

functions/tips_scopes/index.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
function _heavyComputation(): int
19+
{
20+
return 1 * 2 * 3 * 4 * 5;
21+
}
22+
23+
function _lightComputation(): int
24+
{
25+
return 1 + 2 + 3 + 4 + 5;
26+
}
27+
28+
// [START functions_tips_scopes]
29+
30+
use Psr\Http\Message\ServerRequestInterface;
31+
32+
function scopeDemo(ServerRequestInterface $request): string
33+
{
34+
// Heavy computations should be cached between invocations.
35+
// The PHP runtime does NOT preserve variables between invocations, so we
36+
// must write their values to a file or otherwise cache them.
37+
// (All writable directories in Cloud Functions are in-memory, so
38+
// file-based caching operations are typically fast.)
39+
// You can also use PSR-6 caching libraries for this task:
40+
// https://packagist.org/providers/psr/cache-implementation
41+
$cachePath = sys_get_temp_dir() . '/cached_value.txt';
42+
43+
$response = '';
44+
if (file_exists($cachePath)) {
45+
// Read cached value from file
46+
$response .= "Reading cached value." . PHP_EOL;
47+
$instanceVar = file_get_contents($cachePath);
48+
} else {
49+
// Compute cached value + write to file
50+
$response .= "Cache empty, computing value." . PHP_EOL;
51+
$instanceVar = _heavyComputation();
52+
file_put_contents($cachePath, $instanceVar);
53+
}
54+
55+
// Lighter computations can re-run on each function invocation.
56+
$functionVar = _lightComputation();
57+
58+
$response .= 'Per instance: ' . $instanceVar . PHP_EOL;
59+
$response .= 'Per function: ' . $functionVar . PHP_EOL;
60+
61+
return $response;
62+
}
63+
64+
// [END functions_tips_scopes]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="../../testing/bootstrap.php" convertWarningsToExceptions="false">
18+
<testsuites>
19+
<testsuite name="Cloud Functions Scope Test Suite">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">.directory>
29+
<exclude>
30+
<directory>./vendordirectory>
31+
exclude>
32+
whitelist>
33+
filter>
34+
phpunit>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace Google\Cloud\Samples\Functions\HelloworldGet\Test;
21+
22+
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
23+
use PHPUnit\Framework\TestCase;
24+
25+
/**
26+
* Class DeployTest.
27+
*
28+
* This test is not run by the CI system.
29+
*
30+
* To skip deployment of a new function, run with "GOOGLE_SKIP_DEPLOYMENT=true".
31+
* To skip deletion of the tested function, run with "GOOGLE_KEEP_DEPLOYMENT=true".
32+
*/
33+
class DeployTest extends TestCase
34+
{
35+
use CloudFunctionDeploymentTrait;
36+
37+
private static $entryPoint = 'scopeDemo';
38+
39+
public function testFunction(): void
40+
{
41+
// Send a request to the function.
42+
$firstResp = $this->client->post('', [
43+
// Uncomment and CURLOPT_VERBOSE debug content will be sent to stdout.
44+
// 'debug' => true
45+
]);
46+
$secondResp = $this->client->post('', [
47+
// Uncomment and CURLOPT_VERBOSE debug content will be sent to stdout.
48+
// 'debug' => true
49+
]);
50+
51+
// Assert status codes.
52+
$this->assertEquals('200', $firstResp->getStatusCode());
53+
$this->assertEquals('200', $secondResp->getStatusCode());
54+
55+
$firstOutput = trim((string) $firstResp->getBody());
56+
$secondOutput = trim((string) $secondResp->getBody());
57+
58+
// Assert generic function output.
59+
$this->assertContains('Per instance: 120', $firstOutput);
60+
$this->assertContains('Per function: 15', $firstOutput);
61+
62+
// Assert caching behavior.
63+
$this->assertContains('Cache empty', $firstOutput);
64+
$this->assertContains('Reading cached value', $secondOutput);
65+
}
66+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Google\Cloud\Samples\Functions\HelloworldGet\Test;
20+
21+
use PHPUnit\Framework\TestCase;
22+
use Google\Cloud\TestUtils\CloudFunctionLocalTestTrait;
23+
24+
/**
25+
* Class IntegrationTest.
26+
*/
27+
class IntegrationTest extends TestCase
28+
{
29+
use CloudFunctionLocalTestTrait;
30+
31+
private static $entryPoint = 'scopeDemo';
32+
33+
public function testFunction(): void
34+
{
35+
// Send two requests to the function.
36+
// (This tests cross-request caching behavior.)
37+
$firstResp = $this->client->post('/');
38+
$secondResp = $this->client->post('/');
39+
40+
// Assert status codes.
41+
$this->assertEquals('200', $firstResp->getStatusCode());
42+
$this->assertEquals('200', $secondResp->getStatusCode());
43+
}
44+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Google\Cloud\Samples\Functions\HelloworldGet\Test;
20+
21+
use GuzzleHttp\Psr7\ServerRequest;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* Unit tests for the Cloud Function.
26+
*/
27+
class UnitTest extends TestCase
28+
{
29+
private static $entryPoint = 'scopeDemo';
30+
31+
public static function setUpBeforeClass(): void
32+
{
33+
require_once __DIR__ . '/../index.php';
34+
}
35+
36+
public function testFunction(): void
37+
{
38+
$request = new ServerRequest('POST', '/');
39+
$output = $this->runFunction(self::$entryPoint, [$request]);
40+
$this->assertContains('Per instance: 120', $output);
41+
$this->assertContains('Per function: 15', $output);
42+
}
43+
44+
private static function runFunction($functionName, array $params = []): string
45+
{
46+
return call_user_func_array($functionName, $params);
47+
}
48+
}

0 commit comments

Comments
 (0)