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();
}
}
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();
}
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();
}
}
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();
}
}
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);
}
Aggregations