Skip to content

Commit f76f1c9

Browse files
authored
Fix listMonitoredResourceDescriptors tests flakeyness (#1340)
1 parent bf0fd0c commit f76f1c9

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed

google-cloud-examples/src/test/java/com/google/cloud/examples/logging/snippets/ITLoggingSnippets.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.cloud.logging.Metric;
2626
import com.google.cloud.logging.Sink;
2727
import com.google.cloud.logging.testing.RemoteLoggingHelper;
28-
import com.google.common.collect.ImmutableSet;
2928
import com.google.common.collect.Iterators;
3029
import com.google.common.collect.Sets;
3130

@@ -36,20 +35,13 @@
3635
import org.junit.rules.ExpectedException;
3736
import org.junit.rules.Timeout;
3837

39-
import java.util.HashSet;
4038
import java.util.Iterator;
4139
import java.util.Set;
4240
import java.util.concurrent.ExecutionException;
4341

4442
public class ITLoggingSnippets {
4543

4644
private static final String DATASET = "dataset";
47-
private static final Set<String> DESCRIPTOR_TYPES = ImmutableSet.of("gce_instance", "gae_app",
48-
"cloudsql_database", "api", "gcs_bucket", "global", "dataflow_step", "build",
49-
"app_script_function", "dataproc_cluster", "ml_job", "bigquery_resource", "container",
50-
"gke_cluster", "cloud_debugger_resource", "http_load_balancer", "aws_ec2_instance",
51-
"client_auth_config_brand", "client_auth_config_client", "logging_log", "logging_sink",
52-
"metric", "project", "testservice_matrix", "service_account", "deployment");
5345

5446
private static Logging logging;
5547
private static LoggingSnippets loggingSnippets;
@@ -134,21 +126,19 @@ public void testMetric() throws ExecutionException, InterruptedException {
134126
public void testMonitoredResourceDescriptor() throws ExecutionException, InterruptedException {
135127
Iterator<MonitoredResourceDescriptor> iterator =
136128
loggingSnippets.listMonitoredResourceDescriptors().iterateAll();
137-
Set<String> descriptorTypes = new HashSet<>();
129+
int count = 0;
138130
while (iterator.hasNext()) {
139-
descriptorTypes.add(iterator.next().type());
140-
}
141-
for (String type : DESCRIPTOR_TYPES) {
142-
assertTrue(descriptorTypes.contains(type));
131+
assertNotNull(iterator.next().getType());
132+
count += 1;
143133
}
134+
assertTrue(count > 0);
144135
iterator = loggingSnippets.listMonitoredResourceDescriptorsAsync().iterateAll();
145-
descriptorTypes.clear();
136+
count = 0;
146137
while (iterator.hasNext()) {
147-
descriptorTypes.add(iterator.next().type());
148-
}
149-
for (String type : DESCRIPTOR_TYPES) {
150-
assertTrue(descriptorTypes.contains(type));
138+
assertNotNull(iterator.next().getType());
139+
count += 1;
151140
}
141+
assertTrue(count > 0);
152142
}
153143

154144
@Test

google-cloud-logging/src/test/java/com/google/cloud/logging/BaseSystemTest.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.google.cloud.logging.SinkInfo.Destination.DatasetDestination;
3737
import com.google.common.collect.ImmutableList;
3838
import com.google.common.collect.ImmutableMap;
39-
import com.google.common.collect.ImmutableSet;
4039
import com.google.common.collect.Iterators;
4140
import com.google.common.collect.Sets;
4241
import com.google.protobuf.Any;
@@ -47,7 +46,6 @@
4746
import org.junit.rules.ExpectedException;
4847
import org.junit.rules.Timeout;
4948

50-
import java.util.HashSet;
5149
import java.util.Iterator;
5250
import java.util.Set;
5351
import java.util.concurrent.ExecutionException;
@@ -60,13 +58,6 @@
6058
*/
6159
public abstract class BaseSystemTest {
6260

63-
private static final Set<String> DESCRIPTOR_TYPES = ImmutableSet.of("gce_instance", "gae_app",
64-
"cloudsql_database", "api", "gcs_bucket", "global", "dataflow_step", "build",
65-
"app_script_function", "dataproc_cluster", "ml_job", "bigquery_resource", "container",
66-
"gke_cluster", "cloud_debugger_resource", "http_load_balancer", "aws_ec2_instance",
67-
"client_auth_config_brand", "client_auth_config_client", "logging_log", "logging_sink",
68-
"metric", "project", "testservice_matrix", "service_account", "deployment");
69-
7061
@Rule
7162
public ExpectedException thrown = ExpectedException.none();
7263

@@ -214,27 +205,25 @@ public void testListSinksAsync() throws ExecutionException, InterruptedException
214205
public void testListMonitoredResourceDescriptors() {
215206
Iterator<MonitoredResourceDescriptor> iterator =
216207
logging().listMonitoredResourceDescriptors(Logging.ListOption.pageSize(1)).iterateAll();
217-
Set<String> descriptorTypes = new HashSet<>();
208+
int count = 0;
218209
while (iterator.hasNext()) {
219-
descriptorTypes.add(iterator.next().getType());
220-
}
221-
for (String type : DESCRIPTOR_TYPES) {
222-
assertTrue(descriptorTypes.contains(type));
210+
assertNotNull(iterator.next().getType());
211+
count += 1;
223212
}
213+
assertTrue(count > 0);
224214
}
225215

226216
@Test
227217
public void testListMonitoredResourceDescriptorsAsync()
228218
throws ExecutionException, InterruptedException {
229219
Iterator<MonitoredResourceDescriptor> iterator = logging()
230220
.listMonitoredResourceDescriptorsAsync(Logging.ListOption.pageSize(1)).get().iterateAll();
231-
Set<String> descriptorTypes = new HashSet<>();
221+
int count = 0;
232222
while (iterator.hasNext()) {
233-
descriptorTypes.add(iterator.next().getType());
234-
}
235-
for (String type : DESCRIPTOR_TYPES) {
236-
assertTrue(descriptorTypes.contains(type));
223+
assertNotNull(iterator.next().getType());
224+
count += 1;
237225
}
226+
assertTrue(count > 0);
238227
}
239228

240229
@Test

0 commit comments

Comments
 (0)