Search in sources :

Example 1 with Constraint

use of org.jmock.core.Constraint 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)

Aggregations

WaitFor (com.intellij.util.WaitFor)1 AnnounceableTorrent (com.turn.ttorrent.common.AnnounceableTorrent)1 Torrent (com.turn.ttorrent.common.Torrent)1 BuildArtifact (jetbrains.buildServer.serverSide.artifacts.BuildArtifact)1 Constraint (org.jmock.core.Constraint)1