Skip to content

Commit

Permalink
docs: update batch publish sample (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
anguillanneuf authored and bcoe committed Aug 23, 2019
1 parent b0ceb5e commit f8203ef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
31 changes: 15 additions & 16 deletions samples/system-test/topics.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
/**
* Copyright 2019, Google, LLC.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

Expand Down Expand Up @@ -156,7 +155,7 @@ describe('topics', () => {
});

it('should publish with specific batch settings', async () => {
const waitTime = 5000;
const waitTime = 1000;
const [subscription] = await pubsub
.topic(topicNameOne)
.subscription(subscriptionNameThree)
Expand All @@ -170,7 +169,7 @@ describe('topics', () => {
const actualWait = publishTime.getTime() - startTime;

assert.strictEqual(data.toString(), expectedMessage.data);
assert(actualWait >= waitTime);
assert(actualWait <= waitTime);
});

it('should publish with retry settings', async () => {
Expand Down
48 changes: 25 additions & 23 deletions samples/topics.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* This application demonstrates how to perform basic operations on topics with
Expand Down Expand Up @@ -157,16 +156,19 @@ async function publishBatchedMessages(
// Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
const dataBuffer = Buffer.from(data);

const [messageId] = await pubsub
.topic(topicName, {
batching: {
maxMessages: maxMessages,
maxMilliseconds: maxWaitTime,
},
})
.publish(dataBuffer);
console.log(`Message ${messageId} published.`);
const batchPublisher = pubsub.topic(topicName, {
batching: {
maxMessages: maxMessages,
maxMilliseconds: maxWaitTime,
},
});

for (let i = 0; i < 10; i++) {
(async () => {
const messageId = await batchPublisher.publish(dataBuffer);
console.log(`Message ${messageId} published.`);
})();
}
// [END pubsub_publisher_batch_settings]
}

Expand Down

0 comments on commit f8203ef

Please sign in to comment.