Skip to content

Commit bcc7c71

Browse files
authored
chore(fix): improve exec time of flaky test
implement writing log(s) synchronously. enforce uniqueness of the log name to avoid conflict with parallel executions. rename the test to follow test<SampleClassName> notation. Fix #657
1 parent e749acc commit bcc7c71

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

samples/snippets/src/main/java/com/example/logging/ListLogEntries.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.text.SimpleDateFormat;
2727
import java.util.Calendar;
2828

29-
3029
public class ListLogEntries {
3130

3231
public static void main(String[] args) throws Exception {
@@ -39,13 +38,20 @@ public static void main(String[] args) throws Exception {
3938

4039
// When composing a filter, using indexed fields, such as timestamp, resource.type, logName and
4140
// others can help accelerate the results
42-
// Full list of indexed fields here: https://cloud.google.com/logging/docs/view/advanced-queries#finding-quickly
41+
// Full list of indexed fields here:
42+
// https://cloud.google.com/logging/docs/view/advanced-queries#finding-quickly
4343
// This sample restrict the results to only last hour to minimize number of API calls
4444
Calendar calendar = Calendar.getInstance();
4545
calendar.add(Calendar.HOUR, -1);
4646
DateFormat rfc3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
47-
String logFilter = "logName=projects/" + options.getProjectId() + "/logs/" + logName
48-
+ " AND timestamp>=\"" + rfc3339.format(calendar.getTime()) + "\"";
47+
String logFilter =
48+
"logName=projects/"
49+
+ options.getProjectId()
50+
+ "/logs/"
51+
+ logName
52+
+ " AND timestamp>=\""
53+
+ rfc3339.format(calendar.getTime())
54+
+ "\"";
4955

5056
// List all log entries
5157
Page<LogEntry> entries = logging.listLogEntries(EntryListOption.filter(logFilter));

samples/snippets/src/test/java/com/example/logging/LoggingIT.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.example.logging;
1818

19+
import static com.google.cloud.logging.testing.RemoteLoggingHelper.formatForTest;
1920
import static com.google.common.truth.Truth.assertThat;
2021

2122
import com.google.cloud.MonitoredResource;
@@ -24,6 +25,7 @@
2425
import com.google.cloud.logging.Logging;
2526
import com.google.cloud.logging.LoggingOptions;
2627
import com.google.cloud.logging.Payload.StringPayload;
28+
import com.google.cloud.logging.Synchronicity;
2729
import java.io.ByteArrayOutputStream;
2830
import java.io.PrintStream;
2931
import java.util.Collections;
@@ -38,7 +40,7 @@
3840
@SuppressWarnings("checkstyle:abbreviationaswordinname")
3941
public class LoggingIT {
4042

41-
private static final String TEST_LOG = "test-log";
43+
private static final String TEST_LOG = formatForTest("test-log");
4244
private static final String GOOGLEAPIS_AUDIT_LOGNAME = "cloudaudit.googleapis.com%2Factivity";
4345
private static final String STRING_PAYLOAD = "Hello, world!";
4446
private static final String STRING_PAYLOAD2 = "Hello world again";
@@ -74,13 +76,14 @@ public void testQuickstart() throws Exception {
7476
}
7577

7678
@Test(timeout = 60000)
77-
public void testWriteAndListLogs() throws Exception {
79+
public void testListLogEntries() throws Exception {
7880
// write a log entry
7981
LogEntry entry =
8082
LogEntry.newBuilder(StringPayload.of(STRING_PAYLOAD2))
8183
.setLogName(TEST_LOG)
8284
.setResource(MonitoredResource.newBuilder("global").build())
8385
.build();
86+
logging.setWriteSynchronicity(Synchronicity.SYNC);
8487
logging.write(Collections.singleton(entry));
8588
// flush out log immediately
8689
logging.flush();

0 commit comments

Comments
 (0)