From 30542e60f9ea72d8bde5c16772a0acc728bda231 Mon Sep 17 00:00:00 2001 From: Mike Prieto Date: Tue, 8 Oct 2024 12:21:38 -0400 Subject: [PATCH] test: Fix flaky tests (#2221) * test: Add logs to flaky OpenTelemetry test * test: Ensure publisher is shutdown before checking spans in test * test: Remove logs from test * test: Check that subscription is created in subscriber samples test * test: Use err to log for testing * test: Only setup and teardown before and after the entire test suite for SubscriberIT * test: Remove logs from test * test: Import AfterClass class * test: Fix errors in test * test: Add small change to GCS ingestion test --- .../google/cloud/pubsub/v1/PublisherImplTest.java | 1 + samples/snippets/src/test/java/pubsub/AdminIT.java | 4 ++-- .../snippets/src/test/java/pubsub/SubscriberIT.java | 13 +++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/PublisherImplTest.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/PublisherImplTest.java index fedd17436..219326db5 100644 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/PublisherImplTest.java +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/v1/PublisherImplTest.java @@ -1342,6 +1342,7 @@ public void testPublishOpenTelemetryTracing() throws Exception { fakeExecutor.advanceTime(Duration.ofSeconds(5)); assertEquals("1", publishFuture.get()); fakeExecutor.advanceTime(Duration.ofSeconds(5)); + shutdownTestPublisher(publisher); List allSpans = openTelemetryTesting.getSpans(); assertEquals(4, allSpans.size()); diff --git a/samples/snippets/src/test/java/pubsub/AdminIT.java b/samples/snippets/src/test/java/pubsub/AdminIT.java index fcfa74981..e0c45e8e1 100644 --- a/samples/snippets/src/test/java/pubsub/AdminIT.java +++ b/samples/snippets/src/test/java/pubsub/AdminIT.java @@ -81,8 +81,8 @@ public class AdminIT { private static final String cloudStorageInputFormat = "text"; private static final String cloudStorageTextDelimiter = ","; private static final String cloudStorageMatchGlob = "**.txt"; - private static final String cloudStorageMinimumObjectCreateTime = "1970-01-01T00:00:00Z"; - private static final String cloudStorageMinimumObjectCreateTimeSeconds = "0"; + private static final String cloudStorageMinimumObjectCreateTime = "1970-01-01T00:00:01Z"; + private static final String cloudStorageMinimumObjectCreateTimeSeconds = "seconds: 1"; private static final TopicName topicName = TopicName.of(projectId, topicId); private static final TopicName kinesisIngestionTopicName = diff --git a/samples/snippets/src/test/java/pubsub/SubscriberIT.java b/samples/snippets/src/test/java/pubsub/SubscriberIT.java index 3f91a2a31..b1c827d50 100644 --- a/samples/snippets/src/test/java/pubsub/SubscriberIT.java +++ b/samples/snippets/src/test/java/pubsub/SubscriberIT.java @@ -39,6 +39,7 @@ import java.util.List; import java.util.UUID; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Rule; @@ -46,8 +47,8 @@ import org.junit.rules.Timeout; public class SubscriberIT { - private ByteArrayOutputStream bout; - private PrintStream out; + private static ByteArrayOutputStream bout; + private static PrintStream out; private static final String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String _suffix = UUID.randomUUID().toString(); @@ -129,8 +130,8 @@ public static void checkRequirements() { requireEnvVar("GOOGLE_CLOUD_PROJECT"); } - @Before - public void setUp() throws Exception { + @BeforeClass + public static void setUp() throws Exception { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); System.setOut(out); @@ -162,8 +163,8 @@ public void setUp() throws Exception { } } - @After - public void tearDown() throws Exception { + @AfterClass + public static void tearDown() throws Exception { try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { subscriptionAdminClient.deleteSubscription(subscriptionName.toString()); subscriptionAdminClient.deleteSubscription(subscriptionEodName.toString());