Skip to content

Commit 1b3f2f9

Browse files
authored
feat(PubSub): Add OptimisticSubscribe sample (#2100)
1 parent 798f66c commit 1b3f2f9

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\PubSub;
26+
27+
# [START pubsub_optimistic_subscribe]
28+
use Google\Cloud\Core\Exception\NotFoundException;
29+
use Google\Cloud\PubSub\PubSubClient;
30+
31+
/**
32+
* Optimistically subscribes to a topic
33+
*
34+
* @param string $projectId The Google project ID.
35+
* @param string $topicName The Pub/Sub topic name.
36+
* @param string $subscriptionId The ID of the subscription.
37+
*/
38+
function optimistic_subscribe(
39+
string $projectId,
40+
string $topicName,
41+
string $subscriptionId
42+
): void {
43+
44+
$pubsub = new PubSubClient([
45+
'projectId' => $projectId,
46+
]);
47+
48+
$subscription = $pubsub->subscription($subscriptionId);
49+
50+
try {
51+
$messages = $subscription->pull();
52+
foreach ($messages as $message) {
53+
printf('PubSub Message: %s' . PHP_EOL, $message->data());
54+
$subscription->acknowledge($message);
55+
}
56+
} catch (NotFoundException $e) { // Subscription is not found
57+
printf('Exception Message: %s' . PHP_EOL, $e->getMessage());
58+
printf('StackTrace: %s' . PHP_EOL, $e->getTraceAsString());
59+
// Create subscription and retry the pull. Any messages published before subscription creation would not be received.
60+
$pubsub->subscribe($subscriptionId, $topicName);
61+
optimistic_subscribe($projectId, $topicName, $subscriptionId);
62+
}
63+
}
64+
# [END pubsub_optimistic_subscribe]
65+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
66+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

pubsub/api/test/pubsubTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,42 @@ public function testSubscriberErrorListener()
562562
$this->assertMatchesRegularExpression('/Exception Message/', $output);
563563
$this->assertMatchesRegularExpression(sprintf('/Resource not found \(resource=%s\)/', $subscription), $output);
564564
}
565+
566+
public function testOptimisticSubscribe()
567+
{
568+
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');
569+
$subcriptionId = 'test-subscription-' . rand();
570+
571+
$output = $this->runFunctionSnippet('optimistic_subscribe', [
572+
self::$projectId,
573+
$topic,
574+
$subcriptionId
575+
]);
576+
$this->assertMatchesRegularExpression('/Exception Message/', $output);
577+
$this->assertMatchesRegularExpression(sprintf('/Resource not found \(resource=%s\)/', $subcriptionId), $output);
578+
579+
$testMessage = 'This is a test message';
580+
$output = $this->runFunctionSnippet('publish_message', [
581+
self::$projectId,
582+
$topic,
583+
$testMessage,
584+
]);
585+
$this->assertMatchesRegularExpression('/Message published/', $output);
586+
$output = $this->runFunctionSnippet('optimistic_subscribe', [
587+
self::$projectId,
588+
$topic,
589+
$subcriptionId
590+
]);
591+
$this->assertMatchesRegularExpression(sprintf('/PubSub Message: %s/', $testMessage), $output);
592+
$this->assertDoesNotMatchRegularExpression('/Exception Message/', $output);
593+
$this->assertDoesNotMatchRegularExpression(sprintf('/Resource not found \(resource=%s\)/', $subcriptionId), $output);
594+
595+
// Delete subscription
596+
$output = $this->runFunctionSnippet('delete_subscription', [
597+
self::$projectId,
598+
$subcriptionId,
599+
]);
600+
$this->assertMatchesRegularExpression('/Subscription deleted:/', $output);
601+
$this->assertMatchesRegularExpression(sprintf('/%s/', $subcriptionId), $output);
602+
}
565603
}

0 commit comments

Comments
 (0)