Search in sources :

Example 1 with Torrent

use of com.turn.ttorrent.common.Torrent in project teamcity-torrent-plugin by JetBrains.

the class ServerTorrentsSeederTest method new_file_seedeed_old_removed.

public void new_file_seedeed_old_removed() throws IOException, InterruptedException, NoSuchAlgorithmException {
    System.setProperty(SeedSettings.MAX_NUMBER_OF_SEEDED_TORRENTS, "3");
    System.setProperty(TorrentConfiguration.ANNOUNCE_URL, "http://localhost:6969/announce");
    System.setProperty(TorrentConfiguration.FILE_SIZE_THRESHOLD, "1");
    System.setProperty(TorrentConfiguration.TRACKER_ENABLED, "true");
    System.setProperty(TorrentConfiguration.USER_DOWNLOAD_ENABLED, "true");
    myConfigurator.getConfigurationWatcher().checkForModifications();
    myDispatcher.getMulticaster().serverStartup();
    final File artifactsDir = createTempDir();
    final File torrentsDir = createTempDir();
    final int fileSize = 1 * 1024 * 1024;
    final Queue<String> filesQueue = new ArrayDeque<String>();
    final Queue<String> hashesQueue = new ArrayDeque<String>();
    final List<File> allArtifacts = new ArrayList<File>();
    final List<File> allTorrents = new ArrayList<File>();
    for (int i = 0; i < 5; i++) {
        // move to artifacts dir;
        final File srcFile = createTmpFileWithTS(artifactsDir, fileSize);
        allArtifacts.add(srcFile);
        File torrentFile = new File(torrentsDir, srcFile.getName() + ".torrent");
        assertFalse(torrentFile.exists());
        Torrent torrentMetaInfo = TorrentCreator.create(srcFile, URI.create(""), "");
        TorrentUtil.saveTorrentToFile(torrentMetaInfo, torrentFile);
        BuildArtifact buildArtifact = new DummyBuildArtifactAdapter() {

            @Override
            public boolean isFile() {
                return true;
            }

            @Override
            public long getSize() {
                return fileSize;
            }

            @NotNull
            @Override
            public String getName() {
                return srcFile.getName();
            }

            @NotNull
            @Override
            public String getRelativePath() {
                return srcFile.getName();
            }
        };
        new ArtifactProcessorImpl(torrentsDir.toPath(), artifactsDir.toPath(), myTorrentsSeeder.getTorrentsSeeder(), myConfigurator).processArtifacts(Collections.singletonList(buildArtifact));
        allTorrents.add(torrentFile);
        filesQueue.add(srcFile.getName());
        hashesQueue.add(torrentMetaInfo.getHexInfoHash());
        if (filesQueue.size() > 3) {
            filesQueue.poll();
        }
        if (hashesQueue.size() > 3) {
            hashesQueue.poll();
        }
        new WaitFor(5 * 1000) {

            @Override
            protected boolean condition() {
                final Collection<AnnounceableTorrent> torrents = myTorrentsSeeder.getAnnounceableTorrents();
                if (torrents.size() <= 3) {
                    for (AnnounceableTorrent torrent : torrents) {
                        if (torrent.getHexInfoHash().equals(torrentMetaInfo.getHexInfoHash())) {
                            return true;
                        }
                    }
                }
                return false;
            }
        }.assertCompleted("should have completed in 5 sec");
        assertTrue(myTorrentsSeeder.getSharedTorrents().size() <= 3);
        Collection<String> torrentsHashes = new ArrayList<String>();
        for (AnnounceableTorrent torrent : myTorrentsSeeder.getAnnounceableTorrents()) {
            torrentsHashes.add(torrent.getHexInfoHash());
        }
        // checking currently seeded torrents
        assertEquals(filesQueue.size(), torrentsHashes.size());
        assertContains(hashesQueue, torrentsHashes.toArray(new String[torrentsHashes.size()]));
        // checking removed ones;
        assertThat(allArtifacts, new Constraint() {

            public boolean eval(Object o) {
                for (File artifact : (List<File>) o) {
                    if (!artifact.exists()) {
                        return false;
                    }
                }
                return true;
            }

            public StringBuffer describeTo(StringBuffer buffer) {
                return null;
            }
        });
        assertThat(allTorrents, new Constraint() {

            public boolean eval(Object o) {
                for (File link : (List<File>) o) {
                    if (link.exists() != filesQueue.contains(link.getName().replace(".torrent", ""))) {
                        return false;
                    }
                }
                return true;
            }

            public StringBuffer describeTo(StringBuffer buffer) {
                return null;
            }
        });
    }
    assertEquals(3, myTorrentsSeeder.getNumberOfSeededTorrents());
}
Also used : Torrent(com.turn.ttorrent.common.Torrent) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent) Constraint(org.jmock.core.Constraint) Constraint(org.jmock.core.Constraint) WaitFor(com.intellij.util.WaitFor) BuildArtifact(jetbrains.buildServer.serverSide.artifacts.BuildArtifact) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent)

Example 2 with Torrent

use of com.turn.ttorrent.common.Torrent in project teamcity-torrent-plugin by JetBrains.

the class TorrentFilesFactoryImpl method createTorrentFile.

@Nullable
@Override
public File createTorrentFile(@NotNull File srcFile) {
    final String announceUrl = myConfiguration.getAnnounceUrl();
    if (announceUrl == null)
        return null;
    try {
        File torrentFile = getTorrentFile();
        Torrent torrent = TorrentCreator.create(srcFile, URI.create(announceUrl), "TeamCity Torrent Plugin");
        TorrentUtil.saveTorrentToFile(torrent, torrentFile);
        return torrentFile;
    } catch (Exception e) {
        LOG.warnAndDebugDetails("Failed to create torrent for source file: " + srcFile.getAbsolutePath(), e);
    }
    return null;
}
Also used : Torrent(com.turn.ttorrent.common.Torrent) File(java.io.File) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Torrent

use of com.turn.ttorrent.common.Torrent in project teamcity-torrent-plugin by JetBrains.

the class AgentTorrentsManagerTest method testAnnounceAllOnAgentStarted.

public void testAnnounceAllOnAgentStarted() throws IOException, URISyntaxException, InterruptedException, NoSuchAlgorithmException {
    Tracker tracker = new Tracker(6969);
    try {
        final List<String> torrentHashes = new ArrayList<String>();
        final List<File> createdFiles = new ArrayList<File>();
        final int torrentsCount = 10;
        tracker.start(true);
        for (int i = 0; i < torrentsCount; i++) {
            final File artifactFile = createTempFile(65535);
            createdFiles.add(artifactFile);
            File torrentDir = createTempDir();
            final Torrent torrent = TorrentCreator.create(artifactFile, tracker.getAnnounceURI(), "tc-plugin-test");
            final File torrentFile = new File(torrentDir, artifactFile.getName() + ".torrent");
            torrentHashes.add(torrent.getHexInfoHash());
            TorrentUtil.saveTorrentToFile(torrent, torrentFile);
            myTorrentsManager.getTorrentsSeeder().registerSrcAndTorrentFile(artifactFile, torrentFile, false);
        }
        Mock buildAgentMock = mock(BuildAgent.class);
        myTorrentsManager.agentStarted((BuildAgent) buildAgentMock.proxy());
        new WaitFor(3 * 1000) {

            @Override
            protected boolean condition() {
                return myTorrentsManager.getTorrentsSeeder().getNumberOfSeededTorrents() == torrentsCount;
            }
        };
        List<String> seededHashes = new ArrayList<String>();
        List<File> seededFiles = new ArrayList<File>();
        for (AnnounceableTorrent st : myTorrentsManager.getTorrentsSeeder().getClient().getAnnounceableTorrents()) {
            seededHashes.add(st.getHexInfoHash());
            AnnounceableFileTorrent torrent = myTorrentsManager.getTorrentsSeeder().getClient().getAnnounceableFileTorrent(st.getHexInfoHash());
            Torrent metadata = Torrent.load(new File(torrent.getDotTorrentFilePath()));
            seededFiles.add(new File(torrent.getDownloadDirPath(), metadata.getName()));
        }
        assertSameElements(torrentHashes, seededHashes);
        assertSameElements(createdFiles, seededFiles);
    } finally {
        myTorrentsManager.agentShutdown();
        tracker.stop();
    }
}
Also used : Tracker(com.turn.ttorrent.tracker.Tracker) Torrent(com.turn.ttorrent.common.Torrent) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent) AnnounceableFileTorrent(com.turn.ttorrent.common.AnnounceableFileTorrent) AnnounceableFileTorrent(com.turn.ttorrent.common.AnnounceableFileTorrent) WaitFor(jetbrains.buildServer.util.WaitFor) ArrayList(java.util.ArrayList) File(java.io.File) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent) Mock(org.jmock.Mock)

Example 4 with Torrent

use of com.turn.ttorrent.common.Torrent in project teamcity-torrent-plugin by JetBrains.

the class TorrentArtifactCacheListenerTest method test_stop_seed_when_delete.

public void test_stop_seed_when_delete() throws IOException, NoSuchAlgorithmException {
    File file = createTempFile(1024 * 1025);
    myCacheListener.onAfterAddOrUpdate(file);
    waitForSeededTorrents(1);
    final AnnounceableTorrent announceableTorrent = mySeeder.getTorrentsSeeder().getClient().getAnnounceableTorrents().iterator().next();
    final AnnounceableFileTorrent torrent = mySeeder.getTorrentsSeeder().getClient().getAnnounceableFileTorrent(announceableTorrent.getHexInfoHash());
    Torrent metadata = Torrent.load(new File(torrent.getDotTorrentFilePath()));
    assertEquals(file.getAbsolutePath(), torrent.getDownloadDirPath() + File.separatorChar + metadata.getFilenames().get(0));
    myCacheListener.onBeforeDelete(file);
    assertEquals(0, mySeeder.getNumberOfSeededTorrents());
}
Also used : Torrent(com.turn.ttorrent.common.Torrent) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent) AnnounceableFileTorrent(com.turn.ttorrent.common.AnnounceableFileTorrent) AnnounceableFileTorrent(com.turn.ttorrent.common.AnnounceableFileTorrent) File(java.io.File) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent)

Example 5 with Torrent

use of com.turn.ttorrent.common.Torrent in project teamcity-torrent-plugin by JetBrains.

the class TorrentArtifactCacheListenerTest method test_seed_when_file_appear.

public void test_seed_when_file_appear() throws IOException, NoSuchAlgorithmException {
    File file = createTempFile(1024 * 1025);
    myCacheListener.onAfterAddOrUpdate(file);
    waitForSeededTorrents(1);
    assertEquals(1, mySeeder.getNumberOfSeededTorrents());
    final AnnounceableTorrent announceableTorrent = mySeeder.getTorrentsSeeder().getClient().getAnnounceableTorrents().iterator().next();
    final AnnounceableFileTorrent torrent = mySeeder.getTorrentsSeeder().getClient().getAnnounceableFileTorrent(announceableTorrent.getHexInfoHash());
    Torrent metadata = Torrent.load(new File(torrent.getDotTorrentFilePath()));
    assertEquals(file.getAbsolutePath(), torrent.getDownloadDirPath() + File.separatorChar + metadata.getFilenames().get(0));
}
Also used : Torrent(com.turn.ttorrent.common.Torrent) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent) AnnounceableFileTorrent(com.turn.ttorrent.common.AnnounceableFileTorrent) AnnounceableFileTorrent(com.turn.ttorrent.common.AnnounceableFileTorrent) File(java.io.File) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent)

Aggregations

Torrent (com.turn.ttorrent.common.Torrent)11 File (java.io.File)8 AnnounceableTorrent (com.turn.ttorrent.common.AnnounceableTorrent)5 IOException (java.io.IOException)5 AnnounceableFileTorrent (com.turn.ttorrent.common.AnnounceableFileTorrent)4 Tracker (com.turn.ttorrent.tracker.Tracker)3 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 Client (com.turn.ttorrent.client.Client)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 HttpClient (org.apache.commons.httpclient.HttpClient)2 WaitFor (com.intellij.util.WaitFor)1 SharedTorrent (com.turn.ttorrent.client.SharedTorrent)1 ExecutorService (java.util.concurrent.ExecutorService)1 BuildArtifact (jetbrains.buildServer.serverSide.artifacts.BuildArtifact)1 WaitFor (jetbrains.buildServer.util.WaitFor)1 Mock (org.jmock.Mock)1 Constraint (org.jmock.core.Constraint)1