Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

Commit a9c8fc0

Browse files
committed
chore: Fix log cleanup
1 parent d1f1e91 commit a9c8fc0

6 files changed

Lines changed: 33 additions & 24 deletions

File tree

google-cloud-logging/src/main/java/com/google/cloud/logging/testing/RemoteLoggingHelper.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.io.IOException;
2424
import java.io.InputStream;
2525
import java.time.Duration;
26+
import java.time.Instant;
27+
import java.time.format.DateTimeFormatter;
2628
import java.util.UUID;
2729
import java.util.logging.Level;
2830
import java.util.logging.Logger;
@@ -93,10 +95,14 @@ public static RemoteLoggingHelper create() {
9395

9496
/**
9597
* Formats a resource name for testing purpose. This method appends a random UUID to the provided
96-
* name.
98+
* name. Prepends the Log name with the current instant time to allow for stale logs to be easily
99+
* found and removed.
100+
*
101+
* <p>Format: {Instant}_{name}_{UUID}
97102
*/
98103
public static String formatForTest(String name) {
99-
return name + "-" + UUID.randomUUID();
104+
String now = DateTimeFormatter.ISO_INSTANT.format(Instant.now());
105+
return now + "_" + name + "_" + UUID.randomUUID().toString().substring(0, 8);
100106
}
101107

102108
private static RetrySettings retrySettings() {

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.google.cloud.logging.testing.RemoteLoggingHelper;
2222
import com.google.common.collect.Iterables;
2323
import com.google.logging.v2.LogName;
24-
2524
import java.time.Instant;
2625
import java.time.format.DateTimeFormatter;
2726
import java.time.temporal.ChronoUnit;
@@ -51,6 +50,19 @@ static void beforeClass() {
5150

5251
@AfterAll
5352
static void afterClass() throws Exception {
53+
// Use a cutoff of ~6 hours based on the log name. Any logs that were created before the cutoff
54+
// is from a previous invocation of the test and was unable to be properly deleted.
55+
Page<String> logPage = logging.listLogs();
56+
for (String logName : logPage.iterateAll()) {
57+
Instant cutoff = Instant.now().minus(6, ChronoUnit.HOURS);
58+
String logCreateTimeString = logName.split("_")[0];
59+
Instant logCreateTimeInstant =
60+
(Instant) DateTimeFormatter.ISO_INSTANT.parse(logCreateTimeString);
61+
if (logCreateTimeInstant.isBefore(cutoff)) {
62+
logging.deleteLog(logName);
63+
}
64+
}
65+
5466
logging.close();
5567
}
5668

@@ -64,18 +76,6 @@ protected static <V> String createEqualityFilter(String name, V value) {
6476
return name + "=" + "\"" + value + "\"";
6577
}
6678

67-
protected static boolean cleanupLog(String logName) throws InterruptedException {
68-
int deleteAttempts = 0;
69-
int allowedDeleteAttempts = 5;
70-
boolean deleted = false;
71-
while (!deleted && deleteAttempts < allowedDeleteAttempts) {
72-
Thread.sleep(5000);
73-
deleted = logging.deleteLog(logName);
74-
deleteAttempts++;
75-
}
76-
return deleted;
77-
}
78-
7979
/**
8080
* Creates an equality expression for logging filter.
8181
*

google-cloud-logging/src/test/java/com/google/cloud/logging/it/ITJulLoggerTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ class ITJulLoggerTest extends BaseSystemTest {
4646
private static final String LOG_ID = formatForTest("test-jul-log-handler-log");
4747

4848
@AfterEach
49-
void tearDown() throws InterruptedException {
50-
assertTrue(cleanupLog(LOG_ID));
49+
void tearDown() {
50+
// best-effort to try and delete the log
51+
logging.deleteLog(LOG_ID);
5152
}
5253

5354
@Disabled

google-cloud-logging/src/test/java/com/google/cloud/logging/it/ITLoggingTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ static void insertLogs() {
8181
}
8282

8383
@AfterAll
84-
static void cleanUpLogs() throws InterruptedException {
85-
assertTrue(cleanupLog(LOG_ID));
84+
static void cleanUpLogs() {
85+
// best-effort to try and delete the log
86+
logging.deleteLog(LOG_ID);
8687
}
8788

8889
@Disabled

google-cloud-logging/src/test/java/com/google/cloud/logging/it/ITTailLogsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ class ITTailLogsTest extends BaseSystemTest {
3838
MonitoredResource.newBuilder("global").build();
3939

4040
@AfterAll
41-
static void cleanUpLogs() throws InterruptedException {
42-
assertTrue(cleanupLog(LOG_ID));
41+
static void cleanUpLogs() {
42+
// best-effort to try and delete the log
43+
logging.deleteLog(LOG_ID);
4344
}
4445

4546
@Disabled

google-cloud-logging/src/test/java/com/google/cloud/logging/it/ITTracingLogsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static com.google.common.truth.Truth.assertThat;
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
2222
import static org.junit.jupiter.api.Assertions.assertNull;
23-
import static org.junit.jupiter.api.Assertions.assertTrue;
2423

2524
import com.google.cloud.MonitoredResource;
2625
import com.google.cloud.logging.*;
@@ -118,8 +117,9 @@ static void prepareLogs() throws InterruptedException {
118117
}
119118

120119
@AfterEach
121-
void cleanUpLogs() throws InterruptedException {
122-
assertTrue(cleanupLog(LOG_ID));
120+
void cleanUpLogs() {
121+
// best-effort to try and delete the log
122+
logging.deleteLog(LOG_ID);
123123
}
124124

125125
@Test

0 commit comments

Comments
 (0)