Skip to content

Commit ab7da15

Browse files
authored
Increase Logging Testing stability (GoogleCloudPlatform#903)
* Changed ListLogs to every page of results instead of just the first. * Move deleteLog into the tearDown section to ensure they are removed if the test otherwise fails.
1 parent a2e305f commit ab7da15

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

logging/cloud-client/src/main/java/com/example/logging/ListLogs.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ public static void main(String... args) throws Exception {
3939

4040
String logFilter = "logName=projects/" + options.getProjectId() + "/logs/" + logName;
4141

42-
// List log entries
42+
// List all log entries
4343
Page<LogEntry> entries = logging.listLogEntries(
4444
EntryListOption.filter(logFilter));
45-
for (LogEntry logEntry : entries.iterateAll()) {
46-
System.out.println(logEntry);
47-
}
48-
// Use entries.getNextPage() to paginate
45+
do {
46+
for (LogEntry logEntry : entries.iterateAll()) {
47+
System.out.println(logEntry);
48+
}
49+
entries = entries.getNextPage();
50+
} while(entries != null);
51+
4952
}
5053
// [END listlogs]
5154
}

logging/cloud-client/src/test/java/com/example/logging/LoggingIT.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
@SuppressWarnings("checkstyle:abbreviationaswordinname")
4040
public class LoggingIT {
4141

42+
private final String QUICKSTART_LOG = "my-log";
43+
private final String TEST_WRITE_LOG = "test-log";
44+
4245
private ByteArrayOutputStream bout;
4346
private PrintStream out;
4447
private Logging logging = LoggingOptions.getDefaultInstance().getService();
@@ -56,37 +59,36 @@ public void setUp() {
5659

5760
@After
5861
public void tearDown() {
62+
// Clean up created logs
63+
deleteLog(QUICKSTART_LOG);
64+
deleteLog(TEST_WRITE_LOG);
65+
5966
System.setOut(null);
6067
}
6168

6269
@Test
6370
public void testQuickstart() throws Exception {
64-
String logName = "my-log";
65-
deleteLog(logName);
66-
QuickstartSample.main(logName);
71+
QuickstartSample.main(QUICKSTART_LOG);
6772
String got = bout.toString();
6873
assertThat(got).contains("Logged: Hello, world!");
69-
deleteLog(logName);
7074
}
7175

72-
@Test(timeout = 10000)
76+
@Test(timeout = 20000)
7377
public void testWriteAndListLogs() throws Exception {
74-
String logName = "test-log";
75-
deleteLog(logName);
7678
// write a log entry
7779
LogEntry entry = LogEntry.newBuilder(StringPayload.of("Hello world again"))
78-
.setLogName(logName)
80+
.setLogName(TEST_WRITE_LOG)
7981
.setResource(MonitoredResource.newBuilder("global").build())
8082
.build();
8183
logging.write(Collections.singleton(entry));
8284
// flush out log immediately
8385
logging.flush();
8486
bout.reset();
87+
// Check if the log is listed yet
8588
while (bout.toString().isEmpty()) {
86-
ListLogs.main(logName);
89+
ListLogs.main(TEST_WRITE_LOG);
8790
Thread.sleep(1000);
8891
}
8992
assertThat(bout.toString().contains("Hello world again")).isTrue();
90-
deleteLog(logName);
9193
}
9294
}

0 commit comments

Comments
 (0)