Skip to content

Commit

Permalink
clean up old background directories on new uniqueId created
Browse files Browse the repository at this point in the history
  • Loading branch information
rezastallone committed Oct 11, 2023
1 parent 57122a5 commit d83f19b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ Stream<File> listFilesRecursively(File directory) {
return Stream.concat(plainFiles, directories.flatMap(this::listFilesRecursively));
}

Stream<File> listDirectories(File directory){
File[] files = directory.listFiles();
if (files == null){
return Stream.empty();
}
return Stream.of(files).filter(File::isDirectory);
}

Stream<File> listSpanFiles(File dir) {
return listFiles(dir)
.filter(this::isRegularFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class StartTypeAwareSpanStorage implements SpanStorage {

private final VisibleScreenTracker visibleScreenTracker;
private final FileUtils fileUtils;
private final String uniqueId = UUID.randomUUID().toString();
private final String uniqueId;
private final File rootDir;
private final File spanDir;

Expand All @@ -43,6 +43,9 @@ public StartTypeAwareSpanStorage(
this.fileUtils = fileUtils;
this.rootDir = rootDir;
this.spanDir = fileUtils.getSpansDirectory(rootDir);
this.uniqueId = UUID.randomUUID().toString();
//if new id then background span directory with old ids can be deleted
cleanUpUnsentBackgroundSpan();
}

@Override
Expand Down Expand Up @@ -115,4 +118,18 @@ private File ensureDirExist(File pathToReturn) {
"Error creating path " + pathToReturn + " for span buffer, defaulting to parent");
return rootDir;
}

private void cleanUpUnsentBackgroundSpan(){
fileUtils.listDirectories(new File(spanDir, "background/"))
.filter(file -> {
String path = file.getPath();
String pathId = path.substring(path.lastIndexOf("/") + 1);
return !pathId.equals(uniqueId);
})
.forEach(file -> {
Log.d(SplunkRum.LOG_TAG, "Cleaning up " + file.getPath());
fileUtils.listFilesRecursively(file).forEach(fileUtils::safeDelete);
fileUtils.safeDelete(file);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -29,6 +30,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
Expand All @@ -48,6 +51,19 @@ void setup() {
fileProvider = new StartTypeAwareSpanStorage(visibleScreenTracker, fileUtils, rootDir);
}

@Test
void constructor_onNewId_shouldCleanOldBackgroundFiles(){
File file = mock();
when(file.getPath()).thenReturn("files/spans/background/123");
when(fileUtils.listDirectories(any())).thenReturn(Stream.of(file));
ArgumentCaptor<File> fileArgumentCaptor = ArgumentCaptor.forClass(File.class);

fileProvider = new StartTypeAwareSpanStorage(visibleScreenTracker, fileUtils, rootDir);

verify(fileUtils).safeDelete(fileArgumentCaptor.capture());
assertEquals(file.getPath(), fileArgumentCaptor.getValue().getPath());
}

@Test
void getPendingFiles_givenInBackground_shouldReturnForegoundOnlySpan() {
when(visibleScreenTracker.getPreviouslyVisibleScreen()).thenReturn(null);
Expand Down

0 comments on commit d83f19b

Please sign in to comment.