Search in sources :

Example 1 with Tracker

use of com.turn.ttorrent.tracker.Tracker 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 Torrent 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>();
        for (AnnounceableTorrent st : myTorrentsManager.getTorrentsSeeder().getClient().getAnnounceableTorrents()) {
            seededHashes.add(st.getHexInfoHash());
            AnnounceableFileTorrent torrent = myTorrentsManager.getTorrentsSeeder().getClient().getAnnounceableFileTorrent(st.getHexInfoHash());
            Torrent metadata = Torrent.load(new File(torrent.getDotTorrentFilePath()));
            seededFiles.add(new File(torrent.getDownloadDirPath(), metadata.getName()));
        }
        assertSameElements(torrentHashes, seededHashes);
        assertSameElements(createdFiles, seededFiles);
    } finally {
        myTorrentsManager.agentShutdown();
        tracker.stop();
    }
}
Also used : Tracker(com.turn.ttorrent.tracker.Tracker) Torrent(com.turn.ttorrent.common.Torrent) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent) AnnounceableFileTorrent(com.turn.ttorrent.common.AnnounceableFileTorrent) AnnounceableFileTorrent(com.turn.ttorrent.common.AnnounceableFileTorrent) WaitFor(jetbrains.buildServer.util.WaitFor) ArrayList(java.util.ArrayList) File(java.io.File) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent) Mock(org.jmock.Mock)

Example 2 with Tracker

use of com.turn.ttorrent.tracker.Tracker in project teamcity-torrent-plugin by JetBrains.

the class TorrentArtifactCacheListenerTest method setUp.

@BeforeMethod
@Override
protected void setUp() throws Exception {
    super.setUp();
    myTracker = new Tracker(6969);
    myTracker.start(false);
    myAgentConfiguration = myAgentConfigurationFixture.setUp();
    Mockery m = new Mockery();
    final AgentRunningBuild build = m.mock(AgentRunningBuild.class);
    final BuildProgressLogger logger = new FakeBuildProgressLogger();
    final CurrentBuildTracker buildTracker = m.mock(CurrentBuildTracker.class);
    final ArtifactCacheProvider cacheProvider = m.mock(ArtifactCacheProvider.class);
    final LeechSettings leechSettings = m.mock(LeechSettings.class);
    final SeedSettings seedingSettings = m.mock(SeedSettings.class);
    m.checking(new Expectations() {

        {
            allowing(buildTracker).getCurrentBuild();
            will(returnValue(build));
            allowing(build).getBuildLogger();
            will(returnValue(logger));
            allowing(build).getAgentConfiguration();
            will(returnValue(myAgentConfiguration));
            allowing(cacheProvider).addListener(with(any(ArtifactsCacheListener.class)));
            allowing(build).getBuildTypeExternalId();
            will(returnValue("1"));
            allowing(build).getBuildId();
            will(returnValue(1L));
            allowing(leechSettings).isDownloadEnabled();
            will(returnValue(true));
            allowing(seedingSettings).isSeedingEnabled();
            will(returnValue(true));
        }
    });
    final TorrentConfiguration configuration = new FakeTorrentConfiguration();
    final ArtifactsWatcher artifactsWatcher = m.mock(ArtifactsWatcher.class);
    mySeeder = new AgentTorrentsSeeder(myAgentConfiguration, configuration);
    TorrentFilesFactoryImpl torrentsFactory = new TorrentFilesFactoryImpl(myAgentConfiguration, configuration, new FakeAgentIdleTasks(), mySeeder);
    final EventDispatcher<AgentLifeCycleListener> eventDispatcher = EventDispatcher.create(AgentLifeCycleListener.class);
    AgentTorrentsManager manager = new AgentTorrentsManager(eventDispatcher, cacheProvider, buildTracker, configuration, mySeeder, torrentsFactory, artifactsWatcher, new TorrentsDownloadStatistic(), leechSettings, myAgentConfiguration, seedingSettings);
    myCacheListener = new TorrentArtifactCacheListener(mySeeder.getTorrentsSeeder(), buildTracker, configuration, manager, torrentsFactory, artifactsWatcher, myAgentConfiguration);
    myCacheListener.onCacheInitialized(new DirectoryCacheProviderImpl(getTorrentsDirectory(), new SimpleDigestCalculator()));
    manager.checkReady();
}
Also used : Expectations(org.jmock.Expectations) ArtifactsCacheListener(jetbrains.buildServer.artifacts.ArtifactsCacheListener) Tracker(com.turn.ttorrent.tracker.Tracker) ArtifactsWatcher(jetbrains.buildServer.agent.artifacts.ArtifactsWatcher) Mockery(org.jmock.Mockery) SeedSettings(jetbrains.buildServer.torrent.settings.SeedSettings) SimpleDigestCalculator(jetbrains.buildServer.artifacts.impl.SimpleDigestCalculator) LeechSettings(jetbrains.buildServer.torrent.settings.LeechSettings) DirectoryCacheProviderImpl(jetbrains.buildServer.artifacts.impl.DirectoryCacheProviderImpl) TorrentsDownloadStatistic(jetbrains.buildServer.torrent.util.TorrentsDownloadStatistic) ArtifactCacheProvider(jetbrains.buildServer.artifacts.ArtifactCacheProvider) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with Tracker

use of com.turn.ttorrent.tracker.Tracker 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 4 with Tracker

use of com.turn.ttorrent.tracker.Tracker 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 5 with Tracker

use of com.turn.ttorrent.tracker.Tracker in project teamcity-torrent-plugin by JetBrains.

the class TorrentsSeederTest method setUp.

@BeforeMethod
@Override
protected void setUp() throws Exception {
    super.setUp();
    myTracker = new Tracker(6969);
    myTracker.start(false);
    Mockery m = new Mockery();
    myExecutorService = m.mock(ScheduledExecutorService.class);
    final TorrentConfiguration torrentConfiguration = m.mock(TorrentConfiguration.class);
    m.checking(new Expectations() {

        {
            allowing(torrentConfiguration).getWorkerPoolSize();
            will(returnValue(10));
            allowing(torrentConfiguration).getPieceHashingPoolSize();
            will(returnValue(4));
            allowing(myExecutorService).submit(with(any(Runnable.class)));
            allowing(myExecutorService).scheduleWithFixedDelay(with(any(Runnable.class)), with(any(Long.class)), with(any(Long.class)), with(any(TimeUnit.class)));
            allowing(myExecutorService).shutdownNow();
        }
    });
    myDirectorySeeder = new TorrentsSeeder(createTempDir(), 100, null, myExecutorService, torrentConfiguration);
    myDirectorySeeder.start(new InetAddress[] { InetAddress.getLocalHost() }, myTracker.getAnnounceURI(), 3);
}
Also used : Expectations(org.jmock.Expectations) Tracker(com.turn.ttorrent.tracker.Tracker) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TimeUnit(java.util.concurrent.TimeUnit) TorrentConfiguration(jetbrains.buildServer.torrent.TorrentConfiguration) Mockery(org.jmock.Mockery) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

Tracker (com.turn.ttorrent.tracker.Tracker)5 Torrent (com.turn.ttorrent.common.Torrent)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Client (com.turn.ttorrent.client.Client)2 HttpClient (org.apache.commons.httpclient.HttpClient)2 Expectations (org.jmock.Expectations)2 Mockery (org.jmock.Mockery)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 AnnounceableFileTorrent (com.turn.ttorrent.common.AnnounceableFileTorrent)1 AnnounceableTorrent (com.turn.ttorrent.common.AnnounceableTorrent)1 IOException (java.io.IOException)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 ArtifactsWatcher (jetbrains.buildServer.agent.artifacts.ArtifactsWatcher)1 ArtifactCacheProvider (jetbrains.buildServer.artifacts.ArtifactCacheProvider)1 ArtifactsCacheListener (jetbrains.buildServer.artifacts.ArtifactsCacheListener)1 DirectoryCacheProviderImpl (jetbrains.buildServer.artifacts.impl.DirectoryCacheProviderImpl)1 SimpleDigestCalculator (jetbrains.buildServer.artifacts.impl.SimpleDigestCalculator)1