@Test public void testHistoryCleanupHelper() throws ParseException { processEngineConfiguration.setHistoryCleanupBatchWindowStartTime("22:00+0100"); processEngineConfiguration.initHistoryCleanup(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); Date date = sdf.parse("2017-09-06T22:15:00+0100"); assertTrue(HistoryCleanupHelper.isWithinBatchWindow(date, processEngineConfiguration)); date = sdf.parse("2017-09-06T22:15:00+0200"); assertFalse(HistoryCleanupHelper.isWithinBatchWindow(date, processEngineConfiguration)); }
@Test public void testHistoryCleanupWithinBatchWindow() { //given prepareData(15); //we're within batch window Date now = new Date(); ClockUtil.setCurrentTime(now); processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(new SimpleDateFormat("HH:mm").format(now)); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime(new SimpleDateFormat("HH:mm").format(DateUtils.addHours(now, HISTORY_TIME_TO_LIVE))); processEngineConfiguration.initHistoryCleanup(); //when runHistoryCleanup(); //then assertResult(0); }
@Test public void testConfigurationFailureWrongStartTime() { processEngineConfiguration.setHistoryCleanupBatchWindowStartTime("23"); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime("01:00"); thrown.expect(ProcessEngineException.class); thrown.expectMessage("historyCleanupBatchWindowStartTime"); processEngineConfiguration.initHistoryCleanup(); }
@Test public void testScheduleJobForBatchWindow() throws ParseException { ClockUtil.setCurrentTime(currentDate); processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(startTime); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime(endTime); processEngineConfiguration.initHistoryCleanup(); Job job = historyService.cleanUpHistoryAsync(); assertFalse(startDateForCheck.after(job.getDuedate())); // job due date is not before start date assertTrue(endDateForCheck.after(job.getDuedate())); } }
@Test public void testConfigurationFailureWrongEndTime() { processEngineConfiguration.setHistoryCleanupBatchWindowStartTime("23:00"); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime("wrongValue"); thrown.expect(ProcessEngineException.class); thrown.expectMessage("historyCleanupBatchWindowEndTime"); processEngineConfiguration.initHistoryCleanup(); }
@After public void clearDatabase() { //reset configuration changes processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(defaultStartTime); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime(defaultEndTime); processEngineConfiguration.setHistoryCleanupBatchSize(defaultBatchSize); processEngineConfiguration.getCommandExecutorTxRequired().execute(new Command<Void>() { public Void execute(CommandContext commandContext) { List<Job> jobs = managementService.createJobQuery().list(); if (jobs.size() > 0) { assertEquals(1, jobs.size()); String jobId = jobs.get(0).getId(); commandContext.getJobManager().deleteJob((JobEntity) jobs.get(0)); commandContext.getHistoricJobLogManager().deleteHistoricJobLogByJobId(jobId); } return null; } }); }
@Test public void testNotEnoughTimeToDeleteEverything() { //given //we have something to cleanup prepareData(80); //we call history cleanup within batch window Date now = new Date(); ClockUtil.setCurrentTime(now); processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(new SimpleDateFormat("HH:mm").format(now)); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime(new SimpleDateFormat("HH:mm").format(DateUtils.addHours(now, HISTORY_TIME_TO_LIVE))); processEngineConfiguration.initHistoryCleanup(); //job is executed once within batch window //we run the job in 3 threads, so not more than 60 instances can be removed in one run runHistoryCleanup(); //when //time passed -> outside batch window ClockUtil.setCurrentTime(DateUtils.addHours(now, 6)); //the job is called for the second time for (Job job : historyService.findHistoryCleanupJobs()) { managementService.executeJob(job.getId()); } //then //second execution was not able to delete rest data assertResultNotLess(20); }
@After public void clearDatabase() { //reset configuration changes processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(defaultStartTime); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime(defaultEndTime); processEngineConfiguration.setHistoryCleanupBatchSize(defaultBatchSize); processEngineConfiguration.getCommandExecutorTxRequired().execute(new Command<Void>() { public Void execute(CommandContext commandContext) { List<Job> jobs = managementService.createJobQuery().list(); if (jobs.size() > 0) { assertEquals(1, jobs.size()); String jobId = jobs.get(0).getId(); commandContext.getJobManager().deleteJob((JobEntity) jobs.get(0)); commandContext.getHistoricJobLogManager().deleteHistoricJobLogByJobId(jobId); } return null; } }); }
@Test public void testManualRunDoesNotRespectBatchWindow() { //given //we have something to cleanup int processInstanceCount = 40; prepareData(processInstanceCount); //we call history cleanup outside batch window Date now = new Date(); ClockUtil.setCurrentTime(now); processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(new SimpleDateFormat("HH:mm").format(DateUtils.addHours(now, 1))); //now + 1 hour processEngineConfiguration.setHistoryCleanupBatchWindowEndTime(new SimpleDateFormat("HH:mm").format(DateUtils.addHours(now, HISTORY_TIME_TO_LIVE))); //now + 5 hours processEngineConfiguration.initHistoryCleanup(); //when //job is executed before batch window start runHistoryCleanup(true); //the job is called for the second time after batch window end ClockUtil.setCurrentTime(DateUtils.addHours(now, 6)); //now + 6 hours for (Job job : historyService.findHistoryCleanupJobs()) { managementService.executeJob(job.getId()); } //then assertResult(0); }
@Test public void testRescheduleForNever() { //given //force creation of job historyService.cleanUpHistoryAsync(true); List<Job> historyCleanupJobs = historyService.findHistoryCleanupJobs(); assertFalse(historyCleanupJobs.isEmpty()); for (Job job : historyCleanupJobs) { assertNotNull(job.getDuedate()); } processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(null); processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(null); processEngineConfiguration.initHistoryCleanup(); ClockUtil.setCurrentTime(new Date()); //when historyService.cleanUpHistoryAsync(false); //then historyCleanupJobs = historyService.findHistoryCleanupJobs(); for (Job job : historyCleanupJobs) { assertTrue(job.isSuspended()); assertNull(job.getDuedate()); } }
@Test public void testLessThanThresholdWithinBatchWindow() { //given prepareData(5); //we're within batch window Date now = new Date(); ClockUtil.setCurrentTime(now); processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(new SimpleDateFormat("HH:mm").format(now)); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime(new SimpleDateFormat("HH:mm").format(DateUtils.addHours(now, HISTORY_TIME_TO_LIVE))); processEngineConfiguration.initHistoryCleanup(); //when runHistoryCleanup(); //then final List<Job> historyCleanupJobs = historyService.findHistoryCleanupJobs(); for (Job job : historyCleanupJobs) { JobEntity jobEntity = (JobEntity) job; HistoryCleanupJobHandlerConfiguration configuration = getConfiguration(jobEntity); //job rescheduled till current time + delay Date nextRun = getNextRunWithDelay(ClockUtil.getCurrentTime(), 0); assertTrue(jobEntity.getDuedate().equals(nextRun) || jobEntity.getDuedate().after(nextRun)); Date nextRunMax = DateUtils.addSeconds(ClockUtil.getCurrentTime(), HistoryCleanupJobHandlerConfiguration.MAX_DELAY); assertTrue(jobEntity.getDuedate().before(nextRunMax)); //countEmptyRuns incremented assertEquals(1, configuration.getCountEmptyRuns()); } //data is still removed assertResult(0); }
@Test public void testHistoryCleanupJobScheduled() throws ParseException { final ProcessEngineConfigurationImpl standaloneInMemProcessEngineConfiguration = (ProcessEngineConfigurationImpl)ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration(); standaloneInMemProcessEngineConfiguration.setHistoryCleanupBatchWindowStartTime("23:00"); standaloneInMemProcessEngineConfiguration.setHistoryCleanupBatchWindowEndTime("01:00"); standaloneInMemProcessEngineConfiguration.setJdbcUrl("jdbc:h2:mem:camunda" + getClass().getSimpleName() + "testHistoryCleanupJobScheduled"); ProcessEngine engine = standaloneInMemProcessEngineConfiguration .buildProcessEngine(); final List<Job> historyCleanupJobs = engine.getHistoryService().findHistoryCleanupJobs(); assertFalse(historyCleanupJobs.isEmpty()); final ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) engine.getProcessEngineConfiguration(); for (Job historyCleanupJob : historyCleanupJobs) { assertEquals(processEngineConfiguration.getBatchWindowManager().getCurrentOrNextBatchWindow(ClockUtil.getCurrentTime(), processEngineConfiguration).getStart(), historyCleanupJob.getDuedate()); } closeProcessEngine(engine); }
public void initEngineConfiguration(ProcessEngineConfigurationImpl engineConfiguration) { engineConfiguration .setHistoryRemovalTimeStrategy(HISTORY_REMOVAL_TIME_STRATEGY_END) .setHistoryRemovalTimeProvider(new DefaultHistoryRemovalTimeProvider()) .initHistoryRemovalTime(); engineConfiguration.setHistoryCleanupStrategy(HISTORY_CLEANUP_STRATEGY_REMOVAL_TIME_BASED); engineConfiguration.setHistoryCleanupBatchSize(MAX_BATCH_SIZE); engineConfiguration.setHistoryCleanupBatchWindowStartTime("13:00"); engineConfiguration.setHistoryCleanupDegreeOfParallelism(1); engineConfiguration.initHistoryCleanup(); }
@Test public void testScheduleJobForBatchWindowWithDefaultWindowConfigured() throws ParseException { ClockUtil.setCurrentTime(currentDate); processEngineConfiguration.setHistoryCleanupBatchWindowStartTime("23:00"); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime("00:00"); processEngineConfiguration.initHistoryCleanup(); Job job = historyService.cleanUpHistoryAsync(); if (startDateForCheckWithDefaultValues == null) { startDateForCheckWithDefaultValues = startDateForCheck; } if (endDateForCheckWithDefaultValues == null) { endDateForCheckWithDefaultValues = endDateForCheck; } assertFalse(startDateForCheckWithDefaultValues.after(job.getDuedate())); // job due date is not before start date assertTrue(endDateForCheckWithDefaultValues.after(job.getDuedate())); ClockUtil.setCurrentTime(DateUtils.addMinutes(endDateForCheckWithDefaultValues, -1)); job = historyService.cleanUpHistoryAsync(); assertFalse(startDateForCheckWithDefaultValues.after(job.getDuedate())); assertTrue(endDateForCheckWithDefaultValues.after(job.getDuedate())); ClockUtil.setCurrentTime(DateUtils.addMinutes(endDateForCheckWithDefaultValues, 1)); job = historyService.cleanUpHistoryAsync(); assertTrue(endDateForCheckWithDefaultValues.before(job.getDuedate())); }
@AfterClass public static void tearDownAfterAll() { if (engineConfiguration != null) { engineConfiguration .setHistoryRemovalTimeProvider(null) .setHistoryRemovalTimeStrategy(null) .initHistoryRemovalTime(); engineConfiguration.setHistoryCleanupStrategy(HISTORY_CLEANUP_STRATEGY_END_TIME_BASED); engineConfiguration.setHistoryCleanupBatchSize(MAX_BATCH_SIZE); engineConfiguration.setHistoryCleanupBatchWindowStartTime(null); engineConfiguration.setHistoryCleanupDegreeOfParallelism(1); engineConfiguration.initHistoryCleanup(); } ClockUtil.reset(); }
@Test public void testLessThanThresholdWithinBatchWindowAfterMidnight() { //given prepareData(5); //we're within batch window, but batch window passes midnight Date date = new Date(); ClockUtil.setCurrentTime(DateUtils.setMinutes(DateUtils.setHours(date, 0), 10)); //00:10 processEngineConfiguration.setHistoryCleanupBatchWindowStartTime("23:00"); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime("01:00"); processEngineConfiguration.initHistoryCleanup(); //when runHistoryCleanup(false); //then final List<Job> historyCleanupJobs = historyService.findHistoryCleanupJobs(); for (Job job : historyCleanupJobs) { JobEntity jobEntity = (JobEntity) job; HistoryCleanupJobHandlerConfiguration configuration = getConfiguration(jobEntity); //job rescheduled till current time + delay Date nextRun = getNextRunWithDelay(ClockUtil.getCurrentTime(), 0); assertTrue(jobEntity.getDuedate().equals(nextRun) || jobEntity.getDuedate().after(nextRun)); Date nextRunMax = DateUtils.addSeconds(ClockUtil.getCurrentTime(), HistoryCleanupJobHandlerConfiguration.MAX_DELAY); assertTrue(jobEntity.getDuedate().before(nextRunMax)); //countEmptyRuns incremented assertEquals(1, configuration.getCountEmptyRuns()); } //data is still removed assertResult(0); }
@Test public void testLessThanThresholdOutsideBatchWindowAfterMidnight() { //given prepareData(5); //we're outside batch window, batch window passes midnight Date date = new Date(); ClockUtil.setCurrentTime(DateUtils.setMinutes(DateUtils.setHours(date, 1), 10)); //01:10 processEngineConfiguration.setHistoryCleanupBatchWindowStartTime("23:00"); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime("01:00"); processEngineConfiguration.initHistoryCleanup(); //when String jobId = historyService.cleanUpHistoryAsync().getId(); managementService.executeJob(jobId); //then JobEntity jobEntity = getJobEntity(jobId); HistoryCleanupJobHandlerConfiguration configuration = getConfiguration(jobEntity); //job rescheduled till next batch window start Date nextRun = getNextRunWithinBatchWindow(ClockUtil.getCurrentTime()); assertTrue(jobEntity.getDuedate().equals(nextRun)); assertTrue(nextRun.after(ClockUtil.getCurrentTime())); //countEmptyRuns canceled assertEquals(0, configuration.getCountEmptyRuns()); //nothing was removed assertResult(5); }
@Test public void testLessThanThresholdOutsideBatchWindowBeforeMidnight() { //given prepareData(5); //we're outside batch window, batch window passes midnight Date date = new Date(); ClockUtil.setCurrentTime(DateUtils.setMinutes(DateUtils.setHours(date, 22), 10)); //22:10 processEngineConfiguration.setHistoryCleanupBatchWindowStartTime("23:00"); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime("01:00"); processEngineConfiguration.initHistoryCleanup(); //when String jobId = historyService.cleanUpHistoryAsync().getId(); managementService.executeJob(jobId); //then JobEntity jobEntity = getJobEntity(jobId); HistoryCleanupJobHandlerConfiguration configuration = getConfiguration(jobEntity); //job rescheduled till next batch window start Date nextRun = getNextRunWithinBatchWindow(ClockUtil.getCurrentTime()); assertTrue(jobEntity.getDuedate().equals(nextRun)); assertTrue(nextRun.after(ClockUtil.getCurrentTime())); //countEmptyRuns cancelled assertEquals(0, configuration.getCountEmptyRuns()); //nothing was removed assertResult(5); }
@AfterClass public static void tearDownAfterAll() { if (engineConfiguration != null) { engineConfiguration .setHistoryRemovalTimeProvider(null) .setHistoryRemovalTimeStrategy(null) .initHistoryRemovalTime(); engineConfiguration.setHistoryCleanupStrategy(HISTORY_CLEANUP_STRATEGY_REMOVAL_TIME_BASED); engineConfiguration.setHistoryCleanupBatchSize(MAX_BATCH_SIZE); engineConfiguration.setHistoryCleanupBatchWindowStartTime(null); engineConfiguration.setHistoryCleanupDegreeOfParallelism(1); engineConfiguration.setBatchOperationHistoryTimeToLive(null); engineConfiguration.setBatchOperationsForHistoryCleanup(null); engineConfiguration.initHistoryCleanup(); } ClockUtil.reset(); }
@Before public void init() { runtimeService = engineRule.getRuntimeService(); formService = engineRule.getFormService(); historyService = engineRule.getHistoryService(); taskService = engineRule.getTaskService(); managementService = engineRule.getManagementService(); repositoryService = engineRule.getRepositoryService(); identityService = engineRule.getIdentityService(); externalTaskService = engineRule.getExternalTaskService(); decisionService = engineRule.getDecisionService(); engineConfiguration = engineRule.getProcessEngineConfiguration(); engineConfiguration .setHistoryRemovalTimeStrategy(HISTORY_REMOVAL_TIME_STRATEGY_END) .setHistoryRemovalTimeProvider(new DefaultHistoryRemovalTimeProvider()) .initHistoryRemovalTime(); engineConfiguration.setHistoryCleanupStrategy(HISTORY_CLEANUP_STRATEGY_REMOVAL_TIME_BASED); engineConfiguration.setHistoryCleanupBatchSize(MAX_BATCH_SIZE); engineConfiguration.setHistoryCleanupBatchWindowStartTime(null); engineConfiguration.setHistoryCleanupDegreeOfParallelism(1); engineConfiguration.setBatchOperationHistoryTimeToLive(null); engineConfiguration.setBatchOperationsForHistoryCleanup(null); engineConfiguration.initHistoryCleanup(); jobIds = new HashSet<>(); }