Skip to content

Commit 3ea96ab

Browse files
authored
fix: functions retry sample and test (GoogleCloudPlatform#1342)
1 parent 8b62476 commit 3ea96ab

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

functions/tips_retry/index.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121

2222
function tipsRetry(CloudEvent $event): void
2323
{
24-
$data = $event->getData()['data'];
25-
$data = json_decode(base64_decode($data), true);
24+
$cloudEventData = $event->getData();
25+
$pubSubData = $cloudEventData['message']['data'];
26+
27+
$json = json_decode(base64_decode($pubSubData), true);
2628

2729
// Determine whether to retry the invocation based on a parameter
28-
$tryAgain = $data['some_parameter'];
30+
$tryAgain = $json['some_parameter'];
2931

3032
if ($tryAgain) {
3133
/**

functions/tips_retry/puppies.jpg

280 KB
Loading

functions/tips_retry/test/DeployTest.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
namespace Google\Cloud\Samples\Functions\TipsRetry\Test;
2121

22-
use Google\Cloud\Logging\LoggingClient;
2322
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
2423
use Google\Cloud\PubSub\PubSubClient;
2524
use PHPUnit\Framework\TestCase;
@@ -39,24 +38,14 @@ class DeployTest extends TestCase
3938

4039
private static $entryPoint = 'tipsRetry';
4140

42-
/* var string */
43-
private static $projectId;
44-
4541
/* var string */
4642
private static $topicName;
4743

48-
/** @var LoggingClient */
49-
private static $loggingClient;
50-
5144
public function testTipsRetry(): void
5245
{
5346
// Send Pub/Sub message.
5447
$this->publishMessage();
5548

56-
// Give event and log systems a head start.
57-
// If log retrieval fails to find logs for our function within retry limit, increase sleep time.
58-
sleep(60);
59-
6049
$fiveMinAgo = date(\DateTime::RFC3339, strtotime('-5 minutes'));
6150
$this->processFunctionLogs($fiveMinAgo, function (\Iterator $logs) {
6251
// Concatenate all relevant log messages.
@@ -67,7 +56,8 @@ public function testTipsRetry(): void
6756
}
6857

6958
// Check that multiple invocations of the function have occurred.
70-
$retryCount = substr_count($actual, 'Retrying...');
59+
$retryText = 'Intermittent failure occurred; retrying...';
60+
$retryCount = substr_count($actual, $retryText);
7161
$this->assertGreaterThan(1, $retryCount);
7262
});
7363
}
@@ -90,14 +80,16 @@ private function publishMessage(): void
9080
*/
9181
private static function doDeploy()
9282
{
93-
self::$projectId = self::requireEnv('GOOGLE_CLOUD_PROJECT');
9483
self::$topicName = self::requireEnv('FUNCTIONS_TOPIC');
9584

9685
/**
9786
* The --retry flag tells Cloud Functions to automatically retry
9887
* failed function invocations. This is necessary because we're
9988
* the parent sample exists to demonstrate automatic retries.
10089
*/
101-
return self::$fn->deploy(['--retry' => ''], '--trigger-topic=' . self::$topicName);
90+
return self::$fn->deploy(
91+
['--retry' => ''],
92+
'--trigger-topic=' . self::$topicName
93+
);
10294
}
10395
}

functions/tips_retry/test/IntegrationTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ class IntegrationTest extends TestCase
3939
private static $functionSignatureType = 'cloudevent';
4040

4141

42-
private static function makeData($jsonArray)
42+
private static function makeData(array $jsonArray)
4343
{
44-
return ['data' => base64_encode(json_encode($jsonArray))];
44+
return [
45+
'message' => [
46+
'data' => base64_encode(json_encode($jsonArray))
47+
]
48+
];
4549
}
4650

4751
public function dataProvider()
@@ -93,6 +97,6 @@ public function testTipsRetry(array $cloudevent, string $statusCode, string $exp
9397
);
9498

9599
// Verify the function's behavior is correct.
96-
$this->assertContains($expected, $actual, $label . ' contains');
100+
$this->assertStringContainsString($expected, $actual, $label . ' contains');
97101
}
98102
}

0 commit comments

Comments
 (0)