Skip to content

Commit

Permalink
SLING-9905 : CleanupTest added to the project
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-egli committed May 18, 2022
1 parent 86a6725 commit 4217c0c
Show file tree
Hide file tree
Showing 3 changed files with 458 additions and 5 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.discovery.commons</artifactId>
<version>1.0.24</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.discovery.base</artifactId>
<version>2.0.10</version>
<scope>test</scope>
</dependency>

<!-- JUnit -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Clock;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -67,6 +69,9 @@ public class CleanUpTask {
/** We count the scheduler runs. */
private volatile long schedulerRuns;

/** specific clock instance that tests can the use to fiddle around with */
private Clock clock = Clock.systemDefaultZone();

/**
* Constructor
*/
Expand All @@ -75,6 +80,22 @@ public CleanUpTask(final JobManagerConfiguration config, final JobSchedulerImpl
this.jobScheduler = jobScheduler;
}

/** test hook to overwrite the default clock and fiddle with it */
void setClock(Clock clock) {
this.clock = clock;
}

private final Calendar getCalendarInstance() {
Calendar calendar = Calendar.getInstance();
// explicitly set the time based on the clock to allow test fiddlings
calendar.setTimeInMillis(clock.millis());
return calendar;
}

private final long currentTimeMillis() {
return clock.millis();
}

/**
* One maintenance run
*/
Expand Down Expand Up @@ -115,7 +136,7 @@ public void run() {

if (this.configuration.getHistoryCleanUpRemovedJobs() > 0 &&
schedulerRuns % 60 == 1) {
Calendar removeDate = Calendar.getInstance();
Calendar removeDate = getCalendarInstance();
removeDate.add(Calendar.MINUTE, - this.configuration.getHistoryCleanUpRemovedJobs());
this.historyCleanUpRemovedJobs(removeDate);
}
Expand Down Expand Up @@ -187,7 +208,7 @@ private void simpleEmptyFolderCleanup(final TopologyCapabilities caps, final Str
this.logger.debug("Cleaning up job resource tree: looking for empty folders");
final ResourceResolver resolver = this.configuration.createResourceResolver();
try {
final Calendar cleanUpDate = Calendar.getInstance();
final Calendar cleanUpDate = getCalendarInstance();
// go back five minutes
cleanUpDate.add(Calendar.MINUTE, -5);

Expand Down Expand Up @@ -255,7 +276,7 @@ private void fullEmptyFolderCleanup(final TopologyCapabilities caps, final Strin
final Resource baseResource = resolver.getResource(basePath);
// sanity check - should never be null
if ( baseResource != null ) {
final Calendar now = Calendar.getInstance();
final Calendar now = getCalendarInstance();
final int removeYear = now.get(Calendar.YEAR);
final int removeMonth = now.get(Calendar.MONTH) + 1;
final int removeDay = now.get(Calendar.DAY_OF_MONTH);
Expand Down Expand Up @@ -385,15 +406,16 @@ private void cleanUpInstanceIdFolders(final TopologyCapabilities caps, final Str
if ( !hasJobs(caps, r) ) {
// check for timestamp
final long timestamp = r.getValueMap().get(PROPERTY_LAST_CHECKED, -1L);
if ( timestamp > 0 && (timestamp + KEEP_DURATION <= System.currentTimeMillis()) ) {
final long now = currentTimeMillis();
if ( timestamp > 0 && (timestamp + KEEP_DURATION <= now) ) {
toDelete.add(r);
if ( toDelete.size() == MAX_REMOVE_ID_FOLDERS ) {
break;
}
} else if ( timestamp == -1 ) {
final ModifiableValueMap mvm = r.adaptTo(ModifiableValueMap.class);
if ( mvm != null ) {
mvm.put(PROPERTY_LAST_CHECKED, System.currentTimeMillis());
mvm.put(PROPERTY_LAST_CHECKED, now);
resolver.commit();
}
}
Expand Down
Loading

0 comments on commit 4217c0c

Please sign in to comment.