Commits

Anonymous committed 4c1cb30

ability to limit number of seeded torrents + test

Comments (0)

Files changed (2)

common/src/jetbrains/buildServer/artifactsMirror/seeder/TorrentsDirectorySeeder.java

 import java.io.IOException;
 import java.net.InetAddress;
 import java.security.NoSuchAlgorithmException;
-import java.util.Collection;
+import java.util.*;
 
 public class TorrentsDirectorySeeder {
   @NotNull
   private final FilesWatcher myNewLinksWatcher;
   private final TorrentFileFactory myTorrentFactory;
   private volatile boolean myStopped = true;
+  private volatile int myMaxTorrentsToSeed = -1; // no limit by default
 
   public TorrentsDirectorySeeder(@NotNull File torrentStorage, @NotNull TorrentFileFactory torrentFileFactory) {
     myTorrentStorage = torrentStorage;
 
   @NotNull
   private Collection<File> findAllLinks() {
-    return FileUtil.findFiles(new FileFilter() {
+    Collection<File> links = FileUtil.findFiles(new FileFilter() {
       public boolean accept(File file) {
         if (!FileLink.isLink(file)) return false;
 
         }
       }
     }, myTorrentStorage);
+
+    if (myMaxTorrentsToSeed < 0 || links.size() <= myMaxTorrentsToSeed) {
+      return links;
+    }
+
+    List<File> sorted = new ArrayList<File>(links);
+    Collections.sort(sorted, new Comparator<File>() {
+      public int compare(File o1, File o2) {
+        return (int)(o2.lastModified() - o1.lastModified());
+      }
+    });
+
+    return sorted.subList(0, myMaxTorrentsToSeed);
   }
 
   private void processRemovedLink(@NotNull File removedLink) {
   public int getNumberOfSeededTorrents() {
     return myTorrentSeeder.getNumberOfSeededTorrents();
   }
+
+  public int getMaxTorrentsToSeed() {
+    return myMaxTorrentsToSeed;
+  }
+
+  public void setMaxTorrentsToSeed(int maxTorrentsToSeed) {
+    myMaxTorrentsToSeed = maxTorrentsToSeed;
+  }
 }
 

common/tests/jetbrains/buildServer/artifactsMirror/seeder/TorrentsDirectorySeederTest.java

 import org.testng.annotations.Test;
 
 import java.io.File;
+import java.io.FileFilter;
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.URI;
     assertFalse(linkDir.isDirectory());
   }
 
+  public void max_number_of_seeded_torrents() throws IOException {
+    myDirectorySeeder.setMaxTorrentsToSeed(3);
+
+    for (int i=0; i<5; i++) {
+      File srcFile = createTempFile();
+      File linkFile = FileLink.createLink(srcFile, myStorageDir);
+      waitForTorrentFile(linkFile);
+    }
+
+    new WaitFor() {
+      @Override
+      protected boolean condition() {
+        return myStorageDir.list().length == 2 * myDirectorySeeder.getMaxTorrentsToSeed();
+      }
+    };
+
+    assertEquals(myDirectorySeeder.getMaxTorrentsToSeed(), myStorageDir.listFiles(new FileFilter() {
+      public boolean accept(File pathname) {
+        return FileLink.isLink(pathname);
+      }
+    }).length);
+
+    assertEquals(myDirectorySeeder.getMaxTorrentsToSeed(), myStorageDir.listFiles(new FileFilter() {
+      public boolean accept(File pathname) {
+        return pathname.getName().endsWith(TorrentUtil.TORRENT_FILE_SUFFIX);
+      }
+    }).length);
+
+    assertEquals(myDirectorySeeder.getMaxTorrentsToSeed(), myDirectorySeeder.getNumberOfSeededTorrents());
+  }
+
   private File waitForTorrentFile(File linkFile) {
     final File torrentFile = TorrentsDirectorySeeder.getTorrentFileByLinkFile(linkFile);
     new WaitFor() {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.