Skip to content

Commit 0b00228

Browse files
committed
Using Preconditions in Publisher.
1 parent ec2ecc6 commit 0b00228

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

google-cloud-clients/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/Publisher.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,12 @@ public String getTopicNameString() {
216216
* @return the message ID wrapped in a future.
217217
*/
218218
public ApiFuture<String> publish(PubsubMessage message) {
219-
if (shutdown.get()) {
220-
throw new IllegalStateException("Cannot publish on a shut-down publisher.");
221-
}
219+
Preconditions.checkState(!shutdown.get(), "Cannot publish on a shut-down publisher.");
222220

223221
final String orderingKey = message.getOrderingKey();
224-
if (orderingKey != null && !orderingKey.isEmpty() && !enableMessageOrdering) {
225-
throw new IllegalStateException(
226-
"Cannot publish a message with an ordering key when message ordering is not enabled.");
227-
}
222+
Preconditions.checkState(
223+
orderingKey != null && !orderingKey.isEmpty() && !enableMessageOrdering,
224+
"Cannot publish a message with an ordering key when message ordering is not enabled.");
228225

229226
final OutstandingPublish outstandingPublish =
230227
new OutstandingPublish(messageTransform.apply(message));
@@ -361,9 +358,13 @@ private void publishOutstandingBatch(final OutstandingBatch outstandingBatch) {
361358
public void onSuccess(PublishResponse result) {
362359
try {
363360
if (result.getMessageIdsCount() != outstandingBatch.size()) {
364-
Throwable t = new IllegalStateException(String.format(
365-
"The publish result count %s does not match " + "the expected %s results. Please contact Cloud Pub/Sub support "
366-
+ "if this frequently occurs", result.getMessageIdsCount(), outstandingBatch.size()));
361+
Throwable t =
362+
new IllegalStateException(
363+
String.format(
364+
"The publish result count %s does not match "
365+
+ "the expected %s results. Please contact Cloud Pub/Sub support "
366+
+ "if this frequently occurs",
367+
result.getMessageIdsCount(), outstandingBatch.size()));
367368
for (OutstandingPublish oustandingMessage : outstandingBatch.outstandingPublishes) {
368369
oustandingMessage.publishResult.setException(t);
369370
}

google-cloud-clients/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/PublisherImplTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import java.util.concurrent.TimeUnit;
4848
import org.easymock.EasyMock;
4949
import org.junit.After;
50-
import org.junit.Assert;
5150
import org.junit.Before;
5251
import org.junit.Test;
5352
import org.junit.runner.RunWith;
@@ -413,7 +412,6 @@ public void testEnableMessageOrdering_overwritesMaxAttempts() throws Exception {
413412
publisher.shutdown();
414413
}
415414

416-
417415
@Test
418416
public void testEnableMessageOrdering_dontSendWhileInflight() throws Exception {
419417
// Set maxAttempts to 1 and enableMessageOrdering to true at the same time.

0 commit comments

Comments
 (0)