Search in sources :

Example 1 with CommunicationManager

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

the class TorrentTransportTest method createClientWithClosingExecutorServiceOnStop.

private CommunicationManager createClientWithClosingExecutorServiceOnStop() {
    final ExecutorService es = Executors.newFixedThreadPool(2);
    final ExecutorService validatorES = Executors.newFixedThreadPool(2);
    return new CommunicationManager(es, validatorES) {

        @Override
        public void stop() {
            super.stop();
            es.shutdown();
            validatorES.shutdown();
        }
    };
}
Also used : CommunicationManager(com.turn.ttorrent.client.CommunicationManager) ExecutorService(java.util.concurrent.ExecutorService)

Example 2 with CommunicationManager

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

the class TorrentTransportTest method testInterrupt.

public void testInterrupt() throws IOException, InterruptedException, NoSuchAlgorithmException {
    setTorrentTransportEnabled();
    setDownloadHonestly(true);
    final File storageDir = new File(myTempDir, "storageDir");
    storageDir.mkdir();
    final File downloadDir = new File(myTempDir, "downloadDir");
    downloadDir.mkdir();
    final File torrentsDir = new File(myTempDir, "torrentsDir");
    torrentsDir.mkdir();
    final String fileName = "MyBuild.31.zip";
    final File artifactFile = new File(storageDir, fileName);
    createTempFile(25 * 1024 * 1025).renameTo(artifactFile);
    final File teamcityIvyFile = new File("agent/tests/resources/" + Constants.TEAMCITY_IVY);
    myDownloadMap.put("/" + Constants.TEAMCITY_IVY, teamcityIvyFile);
    final String ivyUrl = SERVER_PATH + Constants.TEAMCITY_IVY;
    final File ivyFile = new File(myTempDir, Constants.TEAMCITY_IVY);
    myTorrentTransport.downloadUrlTo(ivyUrl, ivyFile);
    Tracker tracker = new Tracker(6969);
    List<CommunicationManager> communicationManagers = new ArrayList<CommunicationManager>();
    for (int i = 0; i < myLeechSettings.getMinSeedersForDownload(); i++) {
        final ExecutorService es = Executors.newFixedThreadPool(2);
        communicationManagers.add(createClientWithClosingExecutorServiceOnStop());
    }
    try {
        tracker.start(true);
        mySeeder.start(new InetAddress[] { InetAddress.getLocalHost() }, tracker.getAnnounceURI(), 5);
        final TorrentMetadata torrent = TorrentCreator.create(artifactFile, tracker.getAnnounceURI(), "testplugin");
        final File torrentFile = new File(torrentsDir, fileName + ".torrent");
        TorrentUtil.saveTorrentToFile(torrent, torrentFile);
        myDownloadMap.put("/.teamcity/torrents/" + fileName + ".torrent", torrentFile);
        for (CommunicationManager communicationManager : communicationManagers) {
            communicationManager.start(InetAddress.getLocalHost());
            communicationManager.addTorrent(torrentFile.getAbsolutePath(), storageDir.getAbsolutePath());
        }
        final File targetFile = new File(downloadDir, fileName);
        new Thread() {

            @Override
            public void run() {
                try {
                    sleep(200);
                    myTorrentTransport.interrupt();
                } catch (InterruptedException e) {
                    fail("Must not fail here: " + e);
                }
            }
        }.start();
        String digest = null;
        try {
            digest = myTorrentTransport.downloadUrlTo(SERVER_PATH + fileName, targetFile);
        } catch (IOException ex) {
            assertNull(digest);
            assertTrue(ex.getCause() instanceof InterruptedException);
        }
        assertFalse(targetFile.exists());
    } finally {
        for (CommunicationManager communicationManager : communicationManagers) {
            communicationManager.stop();
        }
        tracker.stop();
    }
}
Also used : Tracker(com.turn.ttorrent.tracker.Tracker) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TorrentMetadata(com.turn.ttorrent.common.TorrentMetadata) CommunicationManager(com.turn.ttorrent.client.CommunicationManager) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File)

Example 3 with CommunicationManager

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

the class TorrentTransportTest method testDownloadAndSeed.

public void testDownloadAndSeed() throws IOException, NoSuchAlgorithmException, InterruptedException {
    setTorrentTransportEnabled();
    setDownloadHonestly(true);
    final File storageDir = new File(myTempDir, "storageDir");
    storageDir.mkdir();
    final File downloadDir = new File(myTempDir, "downloadDir");
    downloadDir.mkdir();
    final File torrentsDir = new File(myTempDir, "torrentsDir");
    torrentsDir.mkdir();
    final String fileName = "MyBuild.31.zip";
    final File artifactFile = new File(storageDir, fileName);
    createTempFile(20250).renameTo(artifactFile);
    final File teamcityIvyFile = new File("src/test/resources/" + Constants.TEAMCITY_IVY);
    myDownloadMap.put("/" + Constants.TEAMCITY_IVY, teamcityIvyFile);
    final String ivyUrl = SERVER_PATH + Constants.TEAMCITY_IVY;
    final File ivyFile = new File(myTempDir, Constants.TEAMCITY_IVY);
    myTorrentTransport.downloadUrlTo(ivyUrl, ivyFile);
    Tracker tracker = new Tracker(6969);
    List<CommunicationManager> communicationManagers = new ArrayList<CommunicationManager>();
    for (int i = 0; i < myLeechSettings.getMinSeedersForDownload(); i++) {
        communicationManagers.add(createClientWithClosingExecutorServiceOnStop());
    }
    try {
        tracker.start(true);
        mySeeder.start(new InetAddress[] { InetAddress.getLocalHost() }, tracker.getAnnounceURI(), 5);
        final TorrentMetadata torrent = TorrentCreator.create(artifactFile, tracker.getAnnounceURI(), "testplugin");
        final File torrentFile = new File(torrentsDir, fileName + ".torrent");
        TorrentUtil.saveTorrentToFile(torrent, torrentFile);
        myDownloadMap.put("/.teamcity/torrents/" + fileName + ".torrent", torrentFile);
        for (CommunicationManager communicationManager : communicationManagers) {
            communicationManager.start(InetAddress.getLocalHost());
            communicationManager.addTorrent(torrentFile.getAbsolutePath(), storageDir.getAbsolutePath());
        }
        final File targetFile = new File(downloadDir, fileName);
        final String digest = myTorrentTransport.downloadUrlTo(SERVER_PATH + fileName, targetFile);
        assertNotNull(digest);
        assertTrue(FileUtils.contentEquals(artifactFile, targetFile));
    } finally {
        for (CommunicationManager communicationManager : communicationManagers) {
            communicationManager.stop();
        }
        tracker.stop();
    }
}
Also used : Tracker(com.turn.ttorrent.tracker.Tracker) CommunicationManager(com.turn.ttorrent.client.CommunicationManager) ArrayList(java.util.ArrayList) File(java.io.File) TorrentMetadata(com.turn.ttorrent.common.TorrentMetadata)

Example 4 with CommunicationManager

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

the class TorrentUtilTest method isConnectionManagerInitializedTest.

public void isConnectionManagerInitializedTest() throws Exception {
    ExecutorService es = Executors.newFixedThreadPool(2);
    ExecutorService validatorES = Executors.newFixedThreadPool(2);
    CommunicationManager communicationManager = new CommunicationManager(es, validatorES);
    assertFalse(TorrentUtil.isConnectionManagerInitialized(communicationManager));
    communicationManager.start(InetAddress.getLocalHost());
    assertTrue(TorrentUtil.isConnectionManagerInitialized(communicationManager));
    communicationManager.stop();
    assertTrue(TorrentUtil.isConnectionManagerInitialized(communicationManager));
    es.shutdown();
    validatorES.shutdown();
}
Also used : CommunicationManager(com.turn.ttorrent.client.CommunicationManager) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

CommunicationManager (com.turn.ttorrent.client.CommunicationManager)4 ExecutorService (java.util.concurrent.ExecutorService)3 TorrentMetadata (com.turn.ttorrent.common.TorrentMetadata)2 Tracker (com.turn.ttorrent.tracker.Tracker)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1