Search in sources :

Example 1 with LoadedTorrent

use of com.turn.ttorrent.client.LoadedTorrent 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 TorrentMetadata 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>();
        List<LoadedTorrent> loadedTorrents = myTorrentsManager.getTorrentsSeeder().getClient().getLoadedTorrents();
        for (int i = 0; i < loadedTorrents.size(); i++) {
            LoadedTorrent st = loadedTorrents.get(i);
            File artifact = createdFiles.get(i);
            seededHashes.add(st.getTorrentHash().getHexInfoHash());
            TorrentMetadata metadata = st.getMetadata();
            seededFiles.add(new File(artifact.getParentFile(), metadata.getFiles().get(0).getRelativePathAsString()));
        }
        assertSameElements(torrentHashes, seededHashes);
        assertSameElements(createdFiles, seededFiles);
    } finally {
        myTorrentsManager.agentShutdown();
        tracker.stop();
    }
}
Also used : LoadedTorrent(com.turn.ttorrent.client.LoadedTorrent) Tracker(com.turn.ttorrent.tracker.Tracker) WaitFor(jetbrains.buildServer.util.WaitFor) ArrayList(java.util.ArrayList) File(java.io.File) TorrentMetadata(com.turn.ttorrent.common.TorrentMetadata) Mock(org.jmock.Mock)

Example 2 with LoadedTorrent

use of com.turn.ttorrent.client.LoadedTorrent 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 LoadedTorrent loadedTorrent = mySeeder.getTorrentsSeeder().getClient().getLoadedTorrents().iterator().next();
    TorrentMetadata metadata = loadedTorrent.getMetadata();
    assertEquals(file.getAbsolutePath(), file.getParent() + File.separatorChar + metadata.getFiles().get(0).getRelativePathAsString());
}
Also used : LoadedTorrent(com.turn.ttorrent.client.LoadedTorrent) File(java.io.File) TorrentMetadata(com.turn.ttorrent.common.TorrentMetadata)

Example 3 with LoadedTorrent

use of com.turn.ttorrent.client.LoadedTorrent 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 LoadedTorrent loadedTorrentTorrent = mySeeder.getTorrentsSeeder().getClient().getLoadedTorrents().iterator().next();
    TorrentMetadata metadata = loadedTorrentTorrent.getMetadata();
    assertEquals(file.getAbsolutePath(), file.getParent() + File.separatorChar + metadata.getFiles().get(0).getRelativePathAsString());
    myCacheListener.onBeforeDelete(file);
    assertEquals(0, mySeeder.getNumberOfSeededTorrents());
}
Also used : LoadedTorrent(com.turn.ttorrent.client.LoadedTorrent) File(java.io.File) TorrentMetadata(com.turn.ttorrent.common.TorrentMetadata)

Example 4 with LoadedTorrent

use of com.turn.ttorrent.client.LoadedTorrent 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());
        TorrentMetadata 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<LoadedTorrent> torrents = myTorrentsSeeder.getLoadedTorrents();
                if (torrents.size() <= 3) {
                    for (LoadedTorrent torrent : torrents) {
                        if (torrent.getTorrentHash().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 (LoadedTorrent torrent : myTorrentsSeeder.getLoadedTorrents()) {
            torrentsHashes.add(torrent.getTorrentHash().getHexInfoHash());
        }
        // checking currently seeded torrents
        assertEquals(filesQueue.size(), torrentsHashes.size());
        assertContains(hashesQueue, torrentsHashes.toArray(new String[0]));
        // 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 : LoadedTorrent(com.turn.ttorrent.client.LoadedTorrent) Constraint(org.jmock.core.Constraint) Constraint(org.jmock.core.Constraint) TorrentMetadata(com.turn.ttorrent.common.TorrentMetadata) WaitFor(com.intellij.util.WaitFor) BuildArtifact(jetbrains.buildServer.serverSide.artifacts.BuildArtifact)

Example 5 with LoadedTorrent

use of com.turn.ttorrent.client.LoadedTorrent in project teamcity-torrent-plugin by JetBrains.

the class TorrentsSeederTest method stop_seeding_broken_file.

public void stop_seeding_broken_file() throws IOException, InterruptedException {
    final File srcFile = createTempFile(65535);
    final File torrentFile = createTorrentFromFile(srcFile, srcFile.getParentFile());
    myDirectorySeeder.registerSrcAndTorrentFile(srcFile, torrentFile, true);
    assertTrue(myDirectorySeeder.isSeeding(torrentFile));
    LoadedTorrent next = myDirectorySeeder.getClient().getLoadedTorrents().iterator().next();
    FileUtil.delete(srcFile);
    myDirectorySeeder.checkForBrokenFiles();
    assertFalse(myDirectorySeeder.isSeeding(torrentFile));
}
Also used : LoadedTorrent(com.turn.ttorrent.client.LoadedTorrent) File(java.io.File)

Aggregations

LoadedTorrent (com.turn.ttorrent.client.LoadedTorrent)5 TorrentMetadata (com.turn.ttorrent.common.TorrentMetadata)4 File (java.io.File)4 WaitFor (com.intellij.util.WaitFor)1 Tracker (com.turn.ttorrent.tracker.Tracker)1 ArrayList (java.util.ArrayList)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