Skip to content

Commit 1abdd68

Browse files
feat(Pubsub): Add samples for ordering keys (GoogleCloudPlatform#1811)
1 parent 2fe847d commit 1abdd68

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Google Inc.
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+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\PubSub;
25+
26+
# [START pubsub_enable_subscription_ordering]
27+
use Google\Cloud\PubSub\PubSubClient;
28+
29+
/**
30+
* Creates a Pub/Sub subscription.
31+
*
32+
* @param string $projectId The Google project ID.
33+
* @param string $topicName The Pub/Sub topic name.
34+
* @param string $subscriptionName The Pub/Sub subscription name.
35+
*/
36+
function enable_subscription_ordering($projectId, $topicName, $subscriptionName)
37+
{
38+
$pubsub = new PubSubClient([
39+
'projectId' => $projectId,
40+
]);
41+
$topic = $pubsub->topic($topicName);
42+
$subscription = $topic->subscription($subscriptionName);
43+
44+
$subscription->create(['enableMessageOrdering' => true]);
45+
46+
printf('Created subscription with ordering: %s' . PHP_EOL, $subscription->name());
47+
printf('Subscription info: %s' . PHP_EOL, json_encode($subscription->info()));
48+
}
49+
# [END pubsub_enable_subscription_ordering]
50+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
51+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Google Inc.
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+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\PubSub;
25+
26+
# [START pubsub_publish_with_ordering_keys]
27+
use Google\Cloud\PubSub\MessageBuilder;
28+
use Google\Cloud\PubSub\PubSubClient;
29+
30+
/**
31+
* Publishes a message for a Pub/Sub topic.
32+
*
33+
* @param string $projectId The Google project ID.
34+
* @param string $topicName The Pub/Sub topic name.
35+
*/
36+
function publish_with_ordering_keys($projectId, $topicName)
37+
{
38+
$pubsub = new PubSubClient([
39+
'projectId' => $projectId,
40+
]);
41+
42+
$topic = $pubsub->topic($topicName);
43+
foreach (range(1, 5) as $i) {
44+
$topic->publish((new MessageBuilder(['orderingKey' => 'foo']))
45+
->setData('message' . $i)->build(), ['enableMessageOrdering' => true]);
46+
}
47+
48+
print('Message published' . PHP_EOL);
49+
}
50+
# [END pubsub_publish_with_ordering_keys]
51+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
52+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

pubsub/api/test/pubsubTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,23 @@ public function testSubscribeExactlyOnceDelivery()
424424
$this->assertMatchesRegularExpression('/Acknowledged message:/', $output);
425425
});
426426
}
427+
428+
public function testPublishAndSubscribeWithOrderingKeys()
429+
{
430+
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');
431+
432+
$output = $this->runFunctionSnippet('publish_with_ordering_keys', [
433+
self::$projectId,
434+
$topic,
435+
]);
436+
$this->assertRegExp('/Message published/', $output);
437+
438+
$output = $this->runFunctionSnippet('enable_subscription_ordering', [
439+
self::$projectId,
440+
$topic,
441+
'subscriberWithOrdering' . rand(),
442+
]);
443+
$this->assertRegExp('/Created subscription with ordering/', $output);
444+
$this->assertRegExp('/\"enableMessageOrdering\":true/', $output);
445+
}
427446
}

0 commit comments

Comments
 (0)