Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Make Pub/Sub integration tests less flaky (#2546) (#2547)
Browse files Browse the repository at this point in the history
* Make PubTemplateIntegrationTests less flaky
  • Loading branch information
meltsufin authored Oct 8, 2020
1 parent e60b015 commit f8eadc1
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -62,9 +66,12 @@
* @author Chengyuan Zhao
* @author Dmitry Solomakha
* @author Daniel Zou
* @author Mike Eltsufin
*/
public class PubSubTemplateIntegrationTests {

private static final Log LOGGER = LogFactory.getLog(PubSubTemplateIntegrationTests.class);

private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.cloud.gcp.pubsub.subscriber.max-ack-extension-period=0")
.withConfiguration(AutoConfigurations.of(GcpContextAutoConfiguration.class,
Expand Down Expand Up @@ -94,7 +101,15 @@ public void testCreatePublishPullNextAndDelete() {
headers.put("cactuar", "tonberry");
headers.put("fujin", "raijin");
pubSubTemplate.publish(topicName, "tatatatata", headers).get();
PubsubMessage pubsubMessage = pubSubTemplate.pullNext(subscriptionName);

// get message
AtomicReference<PubsubMessage> pubsubMessageRef = new AtomicReference<>();
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(
() -> {
pubsubMessageRef.set(pubSubTemplate.pullNext(subscriptionName));
return pubsubMessageRef.get() != null;
});
PubsubMessage pubsubMessage = pubsubMessageRef.get();

assertThat(pubsubMessage.getData()).isEqualTo(ByteString.copyFromUtf8("tatatatata"));
assertThat(pubsubMessage.getAttributesCount()).isEqualTo(2);
Expand Down Expand Up @@ -143,7 +158,8 @@ public void testPullAndAck() {
f.get(5, TimeUnit.SECONDS);
}
catch (InterruptedException | ExecutionException | TimeoutException ex) {
ex.printStackTrace();
LOGGER.error(ex);
Thread.currentThread().interrupt();
}
});

Expand Down

0 comments on commit f8eadc1

Please sign in to comment.