Search in sources :

Example 1 with Client

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

the class TorrentTransportTest method createClientWithClosingExecutorServiceOnStop.

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

        @Override
        public void stop(int timeout, TimeUnit timeUnit) {
            super.stop(timeout, timeUnit);
            es.shutdown();
            validatorES.shutdown();
        }
    };
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) TimeUnit(java.util.concurrent.TimeUnit) Client(com.turn.ttorrent.client.Client) HttpClient(org.apache.commons.httpclient.HttpClient)

Example 2 with Client

use of com.turn.ttorrent.client.Client 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<Client> clientList = new ArrayList<Client>();
    for (int i = 0; i < myLeechSettings.getMinSeedersForDownload(); i++) {
        final ExecutorService es = Executors.newFixedThreadPool(2);
        clientList.add(createClientWithClosingExecutorServiceOnStop());
    }
    try {
        tracker.start(true);
        mySeeder.start(new InetAddress[] { InetAddress.getLocalHost() }, tracker.getAnnounceURI(), 5);
        final Torrent 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 (Client client : clientList) {
            client.start(InetAddress.getLocalHost());
            client.addTorrent(torrentFile.getAbsolutePath(), storageDir.getAbsolutePath(), true, false);
        }
        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 (Client client : clientList) {
            client.stop();
        }
        tracker.stop();
    }
}
Also used : Tracker(com.turn.ttorrent.tracker.Tracker) Torrent(com.turn.ttorrent.common.Torrent) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ExecutorService(java.util.concurrent.ExecutorService) Client(com.turn.ttorrent.client.Client) HttpClient(org.apache.commons.httpclient.HttpClient) File(java.io.File)

Example 3 with Client

use of com.turn.ttorrent.client.Client 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("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<Client> clientList = new ArrayList<Client>();
    for (int i = 0; i < myLeechSettings.getMinSeedersForDownload(); i++) {
        clientList.add(createClientWithClosingExecutorServiceOnStop());
    }
    try {
        tracker.start(true);
        mySeeder.start(new InetAddress[] { InetAddress.getLocalHost() }, tracker.getAnnounceURI(), 5);
        final Torrent 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 (Client client : clientList) {
            client.start(InetAddress.getLocalHost());
            client.addTorrent(torrentFile.getAbsolutePath(), storageDir.getAbsolutePath(), true, false);
        }
        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 (Client client : clientList) {
            client.stop();
        }
        tracker.stop();
    }
}
Also used : Tracker(com.turn.ttorrent.tracker.Tracker) Torrent(com.turn.ttorrent.common.Torrent) ArrayList(java.util.ArrayList) Client(com.turn.ttorrent.client.Client) HttpClient(org.apache.commons.httpclient.HttpClient) File(java.io.File)

Example 4 with Client

use of com.turn.ttorrent.client.Client 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);
    Client client = new Client(es, validatorES);
    assertFalse(TorrentUtil.isConnectionManagerInitialized(client));
    client.start(InetAddress.getLocalHost());
    assertTrue(TorrentUtil.isConnectionManagerInitialized(client));
    client.stop();
    assertTrue(TorrentUtil.isConnectionManagerInitialized(client));
    es.shutdown();
    validatorES.shutdown();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Client(com.turn.ttorrent.client.Client)

Aggregations

Client (com.turn.ttorrent.client.Client)4 ExecutorService (java.util.concurrent.ExecutorService)3 HttpClient (org.apache.commons.httpclient.HttpClient)3 Torrent (com.turn.ttorrent.common.Torrent)2 Tracker (com.turn.ttorrent.tracker.Tracker)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 TimeUnit (java.util.concurrent.TimeUnit)1