Skip to content

Commit

Permalink
Increase Logging Testing stability (#903)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
kurtisvg authored Nov 1, 2017
1 parent a2e305f commit ab7da15
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ public static void main(String... args) throws Exception {

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

// List log entries
// List all log entries
Page<LogEntry> entries = logging.listLogEntries(
EntryListOption.filter(logFilter));
for (LogEntry logEntry : entries.iterateAll()) {
System.out.println(logEntry);
}
// Use entries.getNextPage() to paginate
do {
for (LogEntry logEntry : entries.iterateAll()) {
System.out.println(logEntry);
}
entries = entries.getNextPage();
} while(entries != null);

}
// [END listlogs]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class LoggingIT {

private final String QUICKSTART_LOG = "my-log";
private final String TEST_WRITE_LOG = "test-log";

private ByteArrayOutputStream bout;
private PrintStream out;
private Logging logging = LoggingOptions.getDefaultInstance().getService();
Expand All @@ -56,37 +59,36 @@ public void setUp() {

@After
public void tearDown() {
// Clean up created logs
deleteLog(QUICKSTART_LOG);
deleteLog(TEST_WRITE_LOG);

System.setOut(null);
}

@Test
public void testQuickstart() throws Exception {
String logName = "my-log";
deleteLog(logName);
QuickstartSample.main(logName);
QuickstartSample.main(QUICKSTART_LOG);
String got = bout.toString();
assertThat(got).contains("Logged: Hello, world!");
deleteLog(logName);
}

@Test(timeout = 10000)
@Test(timeout = 20000)
public void testWriteAndListLogs() throws Exception {
String logName = "test-log";
deleteLog(logName);
// write a log entry
LogEntry entry = LogEntry.newBuilder(StringPayload.of("Hello world again"))
.setLogName(logName)
.setLogName(TEST_WRITE_LOG)
.setResource(MonitoredResource.newBuilder("global").build())
.build();
logging.write(Collections.singleton(entry));
// flush out log immediately
logging.flush();
bout.reset();
// Check if the log is listed yet
while (bout.toString().isEmpty()) {
ListLogs.main(logName);
ListLogs.main(TEST_WRITE_LOG);
Thread.sleep(1000);
}
assertThat(bout.toString().contains("Hello world again")).isTrue();
deleteLog(logName);
}
}

0 comments on commit ab7da15

Please sign in to comment.