|
| 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\TipsInfiniteRetries\Test; |
| 21 | + |
| 22 | +use Google\Cloud\Logging\LoggingClient; |
| 23 | +use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait; |
| 24 | +use Google\Cloud\TestUtils\EventuallyConsistentTestTrait; |
| 25 | +use Google\Cloud\TestUtils\GcloudWrapper\CloudFunction; |
| 26 | +use Google\Cloud\PubSub\PubSubClient; |
| 27 | +use PHPUnit\Framework\TestCase; |
| 28 | +use PHPUnit\Framework\ExpectationFailedException; |
| 29 | + |
| 30 | +/** |
| 31 | + * Class DeployTest. |
| 32 | + * |
| 33 | + * This test is not run by the CI system. |
| 34 | + * |
| 35 | + * To skip deployment of a new function, run with "GOOGLE_SKIP_DEPLOYMENT=true". |
| 36 | + * To skip deletion of the tested function, run with "GOOGLE_KEEP_DEPLOYMENT=true". |
| 37 | + */ |
| 38 | +class DeployTest extends TestCase |
| 39 | +{ |
| 40 | + use CloudFunctionDeploymentTrait; |
| 41 | + use EventuallyConsistentTestTrait; |
| 42 | + |
| 43 | + private static $entryPoint = 'avoidInfiniteRetries'; |
| 44 | + |
| 45 | + /* var string */ |
| 46 | + private static $projectId; |
| 47 | + |
| 48 | + /* var string */ |
| 49 | + private static $topicName; |
| 50 | + |
| 51 | + /** @var LoggingClient */ |
| 52 | + private static $loggingClient; |
| 53 | + |
| 54 | + |
| 55 | + public function testTipsRetry(): void |
| 56 | + { |
| 57 | + // Send Pub/Sub message. |
| 58 | + $this->publishMessage(); |
| 59 | + |
| 60 | + // Give event and log systems a head start. |
| 61 | + // If log retrieval fails to find logs for our function within retry limit, increase sleep time. |
| 62 | + sleep(30); |
| 63 | + |
| 64 | + $fiveMinAgo = date(\DateTime::RFC3339, strtotime('-5 minutes')); |
| 65 | + $this->processFunctionLogs(self::$fn, $fiveMinAgo, function (\Iterator $logs) { |
| 66 | + // Concatenate all relevant log messages. |
| 67 | + $actual = ''; |
| 68 | + foreach ($logs as $log) { |
| 69 | + $info = $log->info(); |
| 70 | + $actual .= $info['textPayload']; |
| 71 | + } |
| 72 | + |
| 73 | + // Check that multiple invocations of the function have occurred. |
| 74 | + $retryCount = substr_count($actual, 'retrying...'); |
| 75 | + $this->assertGreaterThan(1, $retryCount); |
| 76 | + |
| 77 | + // Check that the function has stopped retrying |
| 78 | + $this->assertContains('Dropping event', $actual); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + private function publishMessage(): void |
| 83 | + { |
| 84 | + // Construct Pub/Sub message |
| 85 | + $message = json_encode(['retry' => true]); |
| 86 | + |
| 87 | + // Publish a message to the function. |
| 88 | + $pubsub = new PubSubClient([ |
| 89 | + 'projectId' => self::$projectId, |
| 90 | + ]); |
| 91 | + $topic = $pubsub->topic(self::$topicName); |
| 92 | + $topic->publish(['data' => $message]); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Retrieve and process logs for the defined function. |
| 97 | + * |
| 98 | + * @param CloudFunction $fn function whose logs should be checked. |
| 99 | + * @param string $startTime RFC3339 timestamp marking start of time range to retrieve. |
| 100 | + * @param callable $process callback function to run on the logs. |
| 101 | + */ |
| 102 | + private function processFunctionLogs(CloudFunction $fn, string $startTime, callable $process) |
| 103 | + { |
| 104 | + $projectId = self::requireEnv('GOOGLE_PROJECT_ID'); |
| 105 | + |
| 106 | + if (empty(self::$loggingClient)) { |
| 107 | + self::$loggingClient = new LoggingClient([ |
| 108 | + 'projectId' => $projectId |
| 109 | + ]); |
| 110 | + } |
| 111 | + |
| 112 | + // Define the log search criteria. |
| 113 | + $logFullName = 'projects/' . $projectId . '/logs/cloudfunctions.googleapis.com%2Fcloud-functions'; |
| 114 | + $filter = sprintf( |
| 115 | + 'logName="%s" resource.labels.function_name="%s" timestamp>="%s"', |
| 116 | + $logFullName, |
| 117 | + $fn->getFunctionName(), |
| 118 | + $startTime |
| 119 | + ); |
| 120 | + |
| 121 | + echo "\nRetrieving logs [$filter]... (this may take a minute or two)\n"; |
| 122 | + |
| 123 | + // Check for new logs for the function. |
| 124 | + $attempt = 1; |
| 125 | + $this->runEventuallyConsistentTest(function () use ($filter, $process, &$attempt) { |
| 126 | + $entries = self::$loggingClient->entries(['filter' => $filter]); |
| 127 | + |
| 128 | + // If no logs came in try again. |
| 129 | + if (empty($entries->current())) { |
| 130 | + echo 'Logs not found, attempting retry #' . $attempt++ . PHP_EOL; |
| 131 | + throw new ExpectationFailedException('Log Entries not available'); |
| 132 | + } |
| 133 | + echo 'Processing logs...' . PHP_EOL; |
| 134 | + |
| 135 | + $process($entries); |
| 136 | + }, $retries = 10); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Deploy the Cloud Function, called from DeploymentTrait::deployApp(). |
| 141 | + * |
| 142 | + * Overrides CloudFunctionDeploymentTrait::doDeploy(). |
| 143 | + */ |
| 144 | + private static function doDeploy() |
| 145 | + { |
| 146 | + self::$projectId = self::requireEnv('GOOGLE_CLOUD_PROJECT'); |
| 147 | + self::$topicName = self::requireEnv('FUNCTIONS_TOPIC'); |
| 148 | + return self::$fn->deploy(['--retry' => ''], '--trigger-topic=' . self::$topicName); |
| 149 | + } |
| 150 | +} |
0 commit comments