From a724f3b302a483dbfe8ef0cc5aca6c200aed8ebe Mon Sep 17 00:00:00 2001 From: Armin Isenring Date: Mon, 3 Jul 2023 11:08:28 +0200 Subject: [PATCH] fixed cleanup endlessly retrying to delete --- pom.xml | 2 +- .../service/CleanupScheduler.java | 2 ++ .../service/CleanupSchedulerTest.java | 27 ++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 9c51198f..a1aa6ab0 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ ch.admin.bag.covidcertificate cc-management-service - 4.12.2 + 4.12.3 cc-management-service Service for generating Covid Certificates diff --git a/src/main/java/ch/admin/bag/covidcertificate/service/CleanupScheduler.java b/src/main/java/ch/admin/bag/covidcertificate/service/CleanupScheduler.java index 172fa9be..1e7b640c 100644 --- a/src/main/java/ch/admin/bag/covidcertificate/service/CleanupScheduler.java +++ b/src/main/java/ch/admin/bag/covidcertificate/service/CleanupScheduler.java @@ -75,6 +75,8 @@ public void processCleanup() { log.error("CLEANUP | {} - cleaning failed, retry #{}/{}; throwed '{}'", effort.getName(), ++retry, MAX_RETRIES, e); if (retry < MAX_RETRIES) { deleted = -1; + } else { + break; } } } diff --git a/src/test/java/ch/admin/bag/covidcertificate/service/CleanupSchedulerTest.java b/src/test/java/ch/admin/bag/covidcertificate/service/CleanupSchedulerTest.java index aa9f161e..89479157 100644 --- a/src/test/java/ch/admin/bag/covidcertificate/service/CleanupSchedulerTest.java +++ b/src/test/java/ch/admin/bag/covidcertificate/service/CleanupSchedulerTest.java @@ -7,7 +7,10 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.postgresql.util.PSQLException; +import org.postgresql.util.PSQLState; +import java.io.EOFException; import java.time.LocalDate; import java.util.Collections; import java.util.Map; @@ -59,12 +62,34 @@ void processCleanup_oneSpotConfigured() { when(cleanupService.create(eq("name2"), any(Cleanup.class))).thenReturn(effort2); when(cleanupService.getCount(eq(effort2), any(LocalDate.class))).thenReturn(5L); - when(cleanupService.delete(eq(effort2), any(LocalDate.class), eq(false))).thenReturn(0L); when(cleanupService.delete(eq(effort2), any(LocalDate.class), eq(true))).thenReturn(0L); + when(cleanupService.delete(eq(effort2), any(LocalDate.class), eq(false))).thenReturn(0L); doNothing().when(cleanupService).destroy(eq(effort2)); cleanupScheduler.processCleanup(); verify(cleanupConfig).getSpots(); } + + @Test + void processCleanup_deleteThrowsException() { + Map map = new TreeMap<>(); + map.put("name1", new Cleanup()); + map.put("name2", new Cleanup()); + when(cleanupConfig.getSpots()).thenReturn(map); + + CleanupService.CleaningEffort effort1 = new CleanupService.CleaningEffort("name1", + "countQuery", "deleteUntilQuery", "deleteUntilBatchQuery", 5, + null, null); + + when(cleanupService.create(eq("name1"), any(Cleanup.class))).thenReturn(effort1); + when(cleanupService.getCount(eq(effort1), any(LocalDate.class))).thenReturn(5L); + when(cleanupService.delete(eq(effort1), any(LocalDate.class), eq(true))) + .thenAnswer(invocation -> { throw new EOFException(null); }); + doNothing().when(cleanupService).destroy(eq(effort1)); + + cleanupScheduler.processCleanup(); + + verify(cleanupConfig).getSpots(); + } } \ No newline at end of file