diff --git a/server/src/main/java/com/chutneytesting/SchedulingConfiguration.java b/server/src/main/java/com/chutneytesting/SchedulingConfiguration.java index 534919627..d73d76d0b 100644 --- a/server/src/main/java/com/chutneytesting/SchedulingConfiguration.java +++ b/server/src/main/java/com/chutneytesting/SchedulingConfiguration.java @@ -35,7 +35,7 @@ import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.ApplicationRunner; -import org.springframework.boot.task.TaskExecutorBuilder; +import org.springframework.boot.task.ThreadPoolTaskExecutorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.task.SimpleAsyncTaskExecutor; @@ -75,13 +75,13 @@ public TaskScheduler taskScheduler() { * With a default with default configuration: org.springframework.boot.autoconfigure.task.TaskExecutionProperties.Pool */ @Bean - public TaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) { + public TaskExecutor applicationTaskExecutor(ThreadPoolTaskExecutorBuilder builder) { return builder.threadNamePrefix("app-task-exec").build(); } @Bean public ApplicationRunner scheduledMissedCampaignToExecute(CampaignScheduler campaignScheduler) { - return arg -> campaignScheduler.scheduledMissedCampaignToExecute(); + return arg -> campaignScheduler.scheduledMissedCampaignIds(); } /** diff --git a/server/src/main/java/com/chutneytesting/campaign/infra/SchedulingCampaignFileRepository.java b/server/src/main/java/com/chutneytesting/campaign/infra/SchedulingCampaignFileRepository.java index 60346e3bc..8a452943d 100644 --- a/server/src/main/java/com/chutneytesting/campaign/infra/SchedulingCampaignFileRepository.java +++ b/server/src/main/java/com/chutneytesting/campaign/infra/SchedulingCampaignFileRepository.java @@ -64,7 +64,7 @@ public class SchedulingCampaignFileRepository implements PeriodicScheduledCampai .setSerializationInclusion(JsonInclude.Include.NON_EMPTY); private final ReadWriteLock rwLock; - public SchedulingCampaignFileRepository(@Value(CONFIGURATION_FOLDER_SPRING_VALUE) String storeFolderPath) throws UncheckedIOException { + SchedulingCampaignFileRepository(@Value(CONFIGURATION_FOLDER_SPRING_VALUE) String storeFolderPath) throws UncheckedIOException { this.rwLock = new ReentrantReadWriteLock(true); this.storeFolderPath = Paths.get(storeFolderPath).resolve(ROOT_DIRECTORY_NAME); this.resolvedFilePath = this.storeFolderPath.resolve(SCHEDULING_CAMPAIGNS_FILE); diff --git a/server/src/main/java/com/chutneytesting/execution/domain/schedule/CampaignScheduler.java b/server/src/main/java/com/chutneytesting/execution/domain/schedule/CampaignScheduler.java index adca520b6..3235f62c2 100644 --- a/server/src/main/java/com/chutneytesting/execution/domain/schedule/CampaignScheduler.java +++ b/server/src/main/java/com/chutneytesting/execution/domain/schedule/CampaignScheduler.java @@ -83,13 +83,14 @@ private Callable executeScheduledCampaignById(List campaignsId) { }; } - public void scheduledMissedCampaignToExecute() { - scheduledCampaignIdsToExecute().toList(); + public void scheduledMissedCampaignIds() { + scheduledCampaignIdsToExecute().forEach(campaignId -> LOGGER.info("Reschedule missed campaign with id {}", campaignId)); } synchronized private Stream> scheduledCampaignIdsToExecute() { try { - return periodicScheduledCampaignRepository.getALl().stream() + List all = periodicScheduledCampaignRepository.getALl(); + return all.stream() .filter(sc -> sc.nextExecutionDate != null) .filter(sc -> sc.nextExecutionDate.isBefore(LocalDateTime.now(clock))) .peek(this::prepareScheduledCampaignForNextExecution) diff --git a/server/src/test/java/com/chutneytesting/execution/domain/schedule/CampaignSchedulerTest.java b/server/src/test/java/com/chutneytesting/execution/domain/schedule/CampaignSchedulerTest.java index bb599e8c9..f7f21b67e 100644 --- a/server/src/test/java/com/chutneytesting/execution/domain/schedule/CampaignSchedulerTest.java +++ b/server/src/test/java/com/chutneytesting/execution/domain/schedule/CampaignSchedulerTest.java @@ -22,8 +22,8 @@ import static java.util.Arrays.asList; import static java.util.Collections.singletonList; import static java.util.stream.Collectors.toList; -import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; @@ -34,11 +34,7 @@ import com.chutneytesting.campaign.domain.Frequency; import com.chutneytesting.campaign.domain.PeriodicScheduledCampaign; import com.chutneytesting.campaign.domain.PeriodicScheduledCampaignRepository; -import com.chutneytesting.campaign.infra.SchedulingCampaignFileRepository; import com.chutneytesting.execution.domain.campaign.CampaignExecutionEngine; -import com.chutneytesting.tools.file.FileUtils; -import java.nio.file.Path; -import java.nio.file.Paths; import java.time.Clock; import java.time.LocalDateTime; import java.time.ZoneId; @@ -61,10 +57,6 @@ public class CampaignSchedulerTest { private final CampaignExecutionEngine campaignExecutionEngine = mock(CampaignExecutionEngine.class); private final PeriodicScheduledCampaignRepository periodicScheduledCampaignRepository = mock(PeriodicScheduledCampaignRepository.class); - - private static Path SCHEDULING_CAMPAIGN_FILE; - @TempDir - private static Path temporaryFolder; private final Clock clock = mock(Clock.class); @BeforeEach @@ -125,65 +117,32 @@ void should_add_next_execution_when_executing_periodic_scheduled_campaign_except @Test void should_reschedule_missed_campaign() { - String tmpConfDir = temporaryFolder.toFile().getAbsolutePath(); - SCHEDULING_CAMPAIGN_FILE = Paths.get(tmpConfDir + "/scheduling/schedulingCampaigns.json"); - CampaignScheduler campaignScheduler = new CampaignScheduler(campaignExecutionEngine, clock, new SchedulingCampaignFileRepository(tmpConfDir), Executors.newFixedThreadPool(2)); - - String initialSchedules = """ - { - "1" : { - "id" : "1", - "campaignsId" : [ 11 ], - "campaignsTitle" : [ "campaign title 1" ], - "schedulingDate" : [ 2024, 1, 1, 14, 0 ], - "frequency" : "Weekly" - }, - "2" : { - "id" : "2", - "campaignsId" : [ 22 ], - "campaignsTitle" : [ "campaign title 2" ], - "schedulingDate" : [ 2023, 3, 4, 7, 10 ], - "frequency" : "Hourly" - }, - "3" : { - "id" : "3", - "campaignsId" : [ 33 ], - "campaignsTitle" : [ "campaign title 3" ], - "schedulingDate" : [ 2024, 2, 2, 14, 0 ] - } - } - """; - FileUtils.initFolder(SCHEDULING_CAMPAIGN_FILE.getParent()); - FileUtils.writeContent(SCHEDULING_CAMPAIGN_FILE, initialSchedules); - - - String expectedSchedules = - """ - { - "4" : { - "id" : "4", - "campaignsId" : [ 11 ], - "campaignsTitle" : [ "campaign title 1" ], - "schedulingDate" : [ 2024, 3, 18, 14, 0 ], - "frequency" : "Weekly" - }, - "5" : { - "id" : "5", - "campaignsId" : [ 22 ], - "campaignsTitle" : [ "campaign title 2" ], - "schedulingDate" : [ 2024, 3, 15, 16, 10 ], - "frequency" : "Hourly" - } - } - """; + PeriodicScheduledCampaign periodicScheduledCampaign1 = new PeriodicScheduledCampaign(1L, List.of(11L), List.of("campaign title 1"), LocalDateTime.of(2024, 1, 1, 14, 0), Frequency.WEEKLY); + PeriodicScheduledCampaign periodicScheduledCampaign2 = new PeriodicScheduledCampaign(2L, List.of(22L), List.of("campaign title 2"), LocalDateTime.of(2023, 3, 4, 7, 10), Frequency.HOURLY); + PeriodicScheduledCampaign periodicScheduledCampaign3 = new PeriodicScheduledCampaign(3L, List.of(33L), List.of("campaign title 3"), LocalDateTime.of(2024, 2, 2, 14, 0)); + when(periodicScheduledCampaignRepository.getALl()) + .thenReturn( + List.of(periodicScheduledCampaign1, periodicScheduledCampaign2, periodicScheduledCampaign3) + ); + when(periodicScheduledCampaignRepository.add(any())) + .thenReturn( + null + ); + doNothing().when(periodicScheduledCampaignRepository).removeById(any()); + + PeriodicScheduledCampaign expected1 = new PeriodicScheduledCampaign(1L, List.of(11L), List.of("campaign title 1"), LocalDateTime.of(2024, 3, 18, 14, 0), Frequency.WEEKLY); + PeriodicScheduledCampaign expected2 = new PeriodicScheduledCampaign(2L, List.of(22L), List.of("campaign title 2"), LocalDateTime.of(2024, 3, 15, 16, 10), Frequency.HOURLY); + // WHEN - campaignScheduler.scheduledMissedCampaignToExecute(); + sut.scheduledMissedCampaignIds(); // THEN - String result = FileUtils.readContent(SCHEDULING_CAMPAIGN_FILE); - assertThat(result).isEqualToIgnoringNewLines(expectedSchedules); - + verify(periodicScheduledCampaignRepository).add(expected1); + verify(periodicScheduledCampaignRepository).add(expected2); + verify(periodicScheduledCampaignRepository).removeById(1L); + verify(periodicScheduledCampaignRepository).removeById(2L); + verify(periodicScheduledCampaignRepository).removeById(3L); } @Test