@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 testConfigurationFailureWrongEndTime() { processEngineConfiguration.setHistoryCleanupBatchWindowStartTime("23:00"); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime("wrongValue"); thrown.expect(ProcessEngineException.class); thrown.expectMessage("historyCleanupBatchWindowEndTime"); processEngineConfiguration.initHistoryCleanup(); }
@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())); } }
@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; } }); }
@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); }
@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 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 testLessThanThresholdOutsideBatchWindow() { //given prepareData(5); //we're outside batch window Date twoHoursAgo = new Date(); processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(new SimpleDateFormat("HH:mm").format(twoHoursAgo)); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime(new SimpleDateFormat("HH:mm").format(DateUtils.addHours(twoHoursAgo, 1))); processEngineConfiguration.initHistoryCleanup(); ClockUtil.setCurrentTime(DateUtils.addHours(twoHoursAgo, 2)); //when for (int i = 1; i <= 3; i++) { runHistoryCleanup(); } //then final List<Job> historyCleanupJobs = historyService.findHistoryCleanupJobs(); for (Job job : historyCleanupJobs) { JobEntity jobEntity = (JobEntity) job; HistoryCleanupJobHandlerConfiguration configuration = getConfiguration(jobEntity); //job rescheduled till next batch window start Date nextRun = getNextRunWithinBatchWindow(ClockUtil.getCurrentTime()); assertTrue(jobEntity.getDuedate().equals(nextRun)); //countEmptyRuns canceled assertEquals(0, configuration.getCountEmptyRuns()); } //nothing was removed assertResult(5); }
@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); }
ClockUtil.setCurrentTime(now); processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(new SimpleDateFormat("HH:mm").format(now)); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime(new SimpleDateFormat("HH:mm").format(DateUtils.addMinutes(now, 30))); processEngineConfiguration.initHistoryCleanup();
ClockUtil.setCurrentTime(now); processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(new SimpleDateFormat("HH:mm").format(now)); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime(new SimpleDateFormat("HH:mm").format(DateUtils.addHours(now, 2))); processEngineConfiguration.initHistoryCleanup();
configuration.setHistoryCleanupBatchWindowEndTime("14:00"); configuration.setHistoryCleanupDegreeOfParallelism(3); configuration.initHistoryCleanup();
ClockUtil.setCurrentTime(now); processEngineConfiguration.setHistoryCleanupBatchWindowStartTime(new SimpleDateFormat("HH:mm").format(now)); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime(new SimpleDateFormat("HH:mm").format(DateUtils.addHours(now, 1))); processEngineConfiguration.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())); }
@Test public void testLessThanThresholdWithinBatchWindowBeforeMidnight() { //given prepareData(5); //we're within batch window, but batch window passes midnight Date date = new Date(); ClockUtil.setCurrentTime(DateUtils.setMinutes(DateUtils.setHours(date, 23), 10)); //23:10 processEngineConfiguration.setHistoryCleanupBatchWindowStartTime("23:00"); processEngineConfiguration.setHistoryCleanupBatchWindowEndTime("01:00"); 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 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); }