Search in sources :

Example 1 with BuildArtifact

use of jetbrains.buildServer.serverSide.artifacts.BuildArtifact 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 BuildArtifact

use of jetbrains.buildServer.serverSide.artifacts.BuildArtifact in project teamcity-torrent-plugin by JetBrains.

the class ServerTorrentsSeederTest method announceBuildArtifactsTest.

public void announceBuildArtifactsTest() {
    Path path = Paths.get("tmp");
    Mockery m = new Mockery();
    BuildArtifacts buildArtifacts = m.mock(BuildArtifacts.class);
    ArtifactsCollector artifactsCollector = m.mock(ArtifactsCollector.class);
    ArtifactProcessor artifactProcessor = m.mock(ArtifactProcessor.class);
    UnusedTorrentFilesRemover unusedTorrentFilesRemover = m.mock(UnusedTorrentFilesRemover.class);
    List<BuildArtifact> artifactsCollectorResult = Collections.emptyList();
    m.checking(new Expectations() {

        {
            one(artifactProcessor).processArtifacts(with(artifactsCollectorResult));
            one(artifactsCollector).collectArtifacts(with(buildArtifacts));
            will(returnValue(artifactsCollectorResult));
            one(unusedTorrentFilesRemover).removeUnusedTorrents(with(artifactsCollectorResult), with(path));
        }
    });
    myTorrentsSeeder.announceBuildArtifacts(path, buildArtifacts, artifactsCollector, artifactProcessor, unusedTorrentFilesRemover);
    m.assertIsSatisfied();
}
Also used : Path(java.nio.file.Path) Expectations(org.jmock.Expectations) BuildArtifact(jetbrains.buildServer.serverSide.artifacts.BuildArtifact) BuildArtifacts(jetbrains.buildServer.serverSide.artifacts.BuildArtifacts) Mockery(org.jmock.Mockery)

Example 3 with BuildArtifact

use of jetbrains.buildServer.serverSide.artifacts.BuildArtifact in project teamcity-torrent-plugin by JetBrains.

the class UnusedTorrentFilesRemoverImplTest method removeUnusedTorrentsTest.

public void removeUnusedTorrentsTest() throws IOException {
    Set<String> actual = new HashSet<>();
    UnusedTorrentFilesRemoverImpl unusedTorrentFilesRemover = new UnusedTorrentFilesRemoverImpl((path) -> {
        actual.add(path.toString());
    }, (path, visitor) -> {
        visitor.visitFile(Paths.get(".teamcity", "torrents", "exist.torrent"), null);
        visitor.visitFile(Paths.get(".teamcity", "torrents", "notExist.torrent"), null);
        visitor.visitFile(Paths.get(".teamcity", "torrents", "dir", "exist.torrent"), null);
        visitor.visitFile(Paths.get(".teamcity", "torrents", "dir", "notExist.torrent"), null);
    });
    Path torrentsDir = Paths.get(".teamcity", "torrents");
    List<BuildArtifact> buildArtifacts = Arrays.asList(createBuildArtifact("exist", "exist"), createBuildArtifact("exist", "dir/exist"));
    unusedTorrentFilesRemover.removeUnusedTorrents(buildArtifacts, torrentsDir);
    Set<String> expected = new HashSet<>();
    expected.add(Paths.get(".teamcity", "torrents", "notExist.torrent").toString());
    expected.add(Paths.get(".teamcity", "torrents", "dir", "notExist.torrent").toString());
    assertEquals(actual, expected);
}
Also used : Path(java.nio.file.Path) BuildArtifact(jetbrains.buildServer.serverSide.artifacts.BuildArtifact) HashSet(java.util.HashSet)

Aggregations

BuildArtifact (jetbrains.buildServer.serverSide.artifacts.BuildArtifact)3 Path (java.nio.file.Path)2 WaitFor (com.intellij.util.WaitFor)1 AnnounceableTorrent (com.turn.ttorrent.common.AnnounceableTorrent)1 Torrent (com.turn.ttorrent.common.Torrent)1 HashSet (java.util.HashSet)1 BuildArtifacts (jetbrains.buildServer.serverSide.artifacts.BuildArtifacts)1 Expectations (org.jmock.Expectations)1 Mockery (org.jmock.Mockery)1 Constraint (org.jmock.core.Constraint)1