3838import com .google .common .collect .ImmutableMap ;
3939import com .google .common .collect .ImmutableSet ;
4040import com .google .common .collect .Iterators ;
41+ import com .google .common .collect .Sets ;
4142import com .google .protobuf .Any ;
4243import com .google .protobuf .StringValue ;
4344
4445import org .junit .Rule ;
4546import org .junit .Test ;
4647import org .junit .rules .ExpectedException ;
48+ import org .junit .rules .Timeout ;
4749
4850import java .util .HashSet ;
4951import java .util .Iterator ;
@@ -69,6 +71,9 @@ public abstract class BaseSystemTest {
6971 @ Rule
7072 public ExpectedException thrown = ExpectedException .none ();
7173
74+ @ Rule
75+ public Timeout globalTimeout = Timeout .seconds (300 );
76+
7277 /**
7378 * Returns the Logging service used to issue requests. This service can be such that it interacts
7479 * with the remote Logging service (for integration tests) or with an emulator (for local
@@ -99,12 +104,11 @@ public void testCreateGetUpdateAndDeleteSink() {
99104 assertEquals ("dataset" , datasetDestination .dataset ());
100105 assertEquals (sink , logging ().getSink (name ));
101106 sink = sink .update (sink .toBuilder ()
102- .versionFormat (SinkInfo .VersionFormat .V1 )
103- .filter ("metadata.serviceName=appengine.googleapis.com" )
107+ .filter ("severity<=ERROR" )
104108 .build ());
105109 assertEquals (name , sink .name ());
106- assertEquals (SinkInfo .VersionFormat .V1 , sink .versionFormat ());
107- assertEquals ("metadata.serviceName=appengine.googleapis.com " , sink .filter ());
110+ assertEquals (SinkInfo .VersionFormat .V2 , sink .versionFormat ());
111+ assertEquals ("severity<=ERROR " , sink .filter ());
108112 assertTrue (sink .delete ());
109113 }
110114
@@ -125,12 +129,11 @@ public void testCreateGetUpdateAndDeleteSinkAsync()
125129 assertEquals ("dataset" , datasetDestination .dataset ());
126130 assertEquals (sink , logging ().getSinkAsync (name ).get ());
127131 sink = sink .updateAsync (sink .toBuilder ()
128- .versionFormat (SinkInfo .VersionFormat .V1 )
129- .filter ("metadata.serviceName=appengine.googleapis.com" )
132+ .filter ("severity<=ERROR" )
130133 .build ()).get ();
131134 assertEquals (name , sink .name ());
132- assertEquals (SinkInfo .VersionFormat .V1 , sink .versionFormat ());
133- assertEquals ("metadata.serviceName=appengine.googleapis.com " , sink .filter ());
135+ assertEquals (SinkInfo .VersionFormat .V2 , sink .versionFormat ());
136+ assertEquals ("severity<=ERROR " , sink .filter ());
134137 assertTrue (sink .deleteAsync ().get ());
135138 }
136139
@@ -171,18 +174,18 @@ public void testUpdateNonExistingSinkAsync() throws ExecutionException, Interrup
171174 }
172175
173176 @ Test
174- public void testListSinks () {
177+ public void testListSinks () throws InterruptedException {
175178 String firstName = formatForTest ("test-list-sinks-1" );
176179 String secondName = formatForTest ("test-list-sinks-2" );
177180 Sink firstSink = logging ().create (SinkInfo .of (firstName , DatasetDestination .of ("dataset" )));
178181 Sink secondSink = logging ().create (SinkInfo .of (secondName , DatasetDestination .of ("dataset" )));
179- Set <String > sinkNames = new HashSet <>();
180- Iterator <Sink > sinkIterator = logging ().listSinks (Logging .ListOption .pageSize (1 )).iterateAll ();
181- while (sinkIterator .hasNext ()) {
182- sinkNames .add (sinkIterator .next ().name ());
182+ Logging .ListOption [] options = {Logging .ListOption .pageSize (1 )};
183+ Page <Sink > sinkPage = logging ().listSinks (options );
184+ Set <Sink > sinks = Sets .newHashSet (sinkPage .iterateAll ());
185+ while (!sinks .contains (firstSink ) || !sinks .contains (secondSink )) {
186+ Thread .sleep (500 );
187+ sinks = Sets .newHashSet (logging ().listSinks (options ).iterateAll ());
183188 }
184- assertTrue (sinkNames .contains (firstName ));
185- assertTrue (sinkNames .contains (secondName ));
186189 firstSink .delete ();
187190 secondSink .delete ();
188191 }
@@ -193,14 +196,13 @@ public void testListSinksAsync() throws ExecutionException, InterruptedException
193196 String secondName = formatForTest ("test-list-sinks-async-2" );
194197 Sink firstSink = logging ().create (SinkInfo .of (firstName , DatasetDestination .of ("dataset" )));
195198 Sink secondSink = logging ().create (SinkInfo .of (secondName , DatasetDestination .of ("dataset" )));
196- Set <String > sinkNames = new HashSet <>();
197- Iterator <Sink > sinkIterator =
198- logging ().listSinksAsync (Logging .ListOption .pageSize (1 )).get ().iterateAll ();
199- while (sinkIterator .hasNext ()) {
200- sinkNames .add (sinkIterator .next ().name ());
199+ Logging .ListOption [] options = {Logging .ListOption .pageSize (1 )};
200+ AsyncPage <Sink > sinkPage = logging ().listSinksAsync (options ).get ();
201+ Set <Sink > sinks = Sets .newHashSet (sinkPage .iterateAll ());
202+ while (!sinks .contains (firstSink ) || !sinks .contains (secondSink )) {
203+ Thread .sleep (500 );
204+ sinks = Sets .newHashSet (logging ().listSinksAsync (options ).get ().iterateAll ());
201205 }
202- assertTrue (sinkNames .contains (firstName ));
203- assertTrue (sinkNames .contains (secondName ));
204206 firstSink .delete ();
205207 secondSink .delete ();
206208 }
@@ -304,37 +306,35 @@ public void testUpdateNonExistingMetricAsync() throws ExecutionException, Interr
304306 }
305307
306308 @ Test
307- public void testListMetrics () {
309+ public void testListMetrics () throws InterruptedException {
308310 String firstName = formatForTest ("test-list-metrics-1" );
309311 String secondName = formatForTest ("test-list-metrics-2" );
310312 Metric firstMetric = logging ().create (MetricInfo .of (firstName , "severity>=ERROR" ));
311313 Metric secondMetric = logging ().create (MetricInfo .of (secondName , "severity>=ERROR" ));
312- Set <String > metricNames = new HashSet <>();
313- Iterator <Metric > metricIterator =
314- logging ().listMetrics (Logging .ListOption .pageSize (1 )).iterateAll ();
315- while (metricIterator .hasNext ()) {
316- metricNames .add (metricIterator .next ().name ());
314+ Logging .ListOption [] options = {Logging .ListOption .pageSize (1 )};
315+ Page <Metric > metricPage = logging ().listMetrics (options );
316+ Set <Metric > metrics = Sets .newHashSet (metricPage .iterateAll ());
317+ while (!metrics .contains (firstMetric ) || !metrics .contains (secondMetric )) {
318+ Thread .sleep (500 );
319+ metrics = Sets .newHashSet (logging ().listMetrics (options ).iterateAll ());
317320 }
318- assertTrue (metricNames .contains (firstName ));
319- assertTrue (metricNames .contains (secondName ));
320321 firstMetric .delete ();
321322 secondMetric .delete ();
322323 }
323324
324325 @ Test
325- public void testListMetricsAsync () {
326+ public void testListMetricsAsync () throws ExecutionException , InterruptedException {
326327 String firstName = formatForTest ("test-list-metrics-async-1" );
327328 String secondName = formatForTest ("test-list-metrics-async-2" );
328329 Metric firstMetric = logging ().create (MetricInfo .of (firstName , "severity>=ERROR" ));
329330 Metric secondMetric = logging ().create (MetricInfo .of (secondName , "severity>=ERROR" ));
330- Set <String > metricNames = new HashSet <>();
331- Iterator <Metric > metricIterator =
332- logging ().listMetrics (Logging .ListOption .pageSize (1 )).iterateAll ();
333- while (metricIterator .hasNext ()) {
334- metricNames .add (metricIterator .next ().name ());
331+ Logging .ListOption [] options = {Logging .ListOption .pageSize (1 )};
332+ AsyncPage <Metric > metricPage = logging ().listMetricsAsync (options ).get ();
333+ Set <Metric > metrics = Sets .newHashSet (metricPage .iterateAll ());
334+ while (!metrics .contains (firstMetric ) || !metrics .contains (secondMetric )) {
335+ Thread .sleep (500 );
336+ metrics = Sets .newHashSet (logging ().listMetricsAsync (options ).get ().iterateAll ());
335337 }
336- assertTrue (metricNames .contains (firstName ));
337- assertTrue (metricNames .contains (secondName ));
338338 firstMetric .delete ();
339339 secondMetric .delete ();
340340 }
0 commit comments