diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/BaseSystemTest.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/BaseSystemTest.java index 8b277b5aa900..98fda1550ee3 100644 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/BaseSystemTest.java +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/BaseSystemTest.java @@ -27,6 +27,7 @@ import com.google.cloud.pubsub.PubSub.MessageConsumer; import com.google.cloud.pubsub.PubSub.MessageProcessor; import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterators; import com.google.common.collect.Lists; import com.google.common.collect.Sets; @@ -465,7 +466,18 @@ public void testPullMessagesAndAutoRenewDeadline() throws InterruptedException { pubsub().publish(topic, message1); pubsub().publish(topic, message2); Iterator iterator = pubsub().pull(subscription, 2); + while (!iterator.hasNext()) { + Thread.sleep(500); + iterator = pubsub().pull(subscription, 2); + } ReceivedMessage consumedMessage = iterator.next(); + if (!iterator.hasNext()) { + iterator = pubsub().pull(subscription, 1); + while (!iterator.hasNext()) { + Thread.sleep(500); + iterator = pubsub().pull(subscription, 1); + } + } Thread.sleep(15000); // first message was consumed while second message is still being renewed Iterator nextIterator = pubsub().pull(subscription, 2); @@ -494,6 +506,10 @@ public void testPullMessagesAndModifyAckDeadline() throws InterruptedException { pubsub().publish(topic, message2); // Consume all messages and stop ack renewal List receivedMessages = Lists.newArrayList(pubsub().pull(subscription, 2)); + while (receivedMessages.size() < 2) { + Thread.sleep(500); + Iterators.addAll(receivedMessages, pubsub().pull(subscription, 2)); + } receivedMessages.get(0).modifyAckDeadline(60, TimeUnit.SECONDS); Thread.sleep(15000); // first message was renewed while second message should still be sent on pull requests