use of com.turn.ttorrent.common.AnnounceableTorrent 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());
}
use of com.turn.ttorrent.common.AnnounceableTorrent 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.common.AnnounceableTorrent in project teamcity-torrent-plugin by JetBrains.
the class TorrentArtifactCacheListenerTest method test_stop_seed_when_delete.
public void test_stop_seed_when_delete() throws IOException, NoSuchAlgorithmException {
File file = createTempFile(1024 * 1025);
myCacheListener.onAfterAddOrUpdate(file);
waitForSeededTorrents(1);
final AnnounceableTorrent announceableTorrent = mySeeder.getTorrentsSeeder().getClient().getAnnounceableTorrents().iterator().next();
final AnnounceableFileTorrent torrent = mySeeder.getTorrentsSeeder().getClient().getAnnounceableFileTorrent(announceableTorrent.getHexInfoHash());
Torrent metadata = Torrent.load(new File(torrent.getDotTorrentFilePath()));
assertEquals(file.getAbsolutePath(), torrent.getDownloadDirPath() + File.separatorChar + metadata.getFilenames().get(0));
myCacheListener.onBeforeDelete(file);
assertEquals(0, mySeeder.getNumberOfSeededTorrents());
}
use of com.turn.ttorrent.common.AnnounceableTorrent in project teamcity-torrent-plugin by JetBrains.
the class TorrentArtifactCacheListenerTest method test_seed_when_file_appear.
public void test_seed_when_file_appear() throws IOException, NoSuchAlgorithmException {
File file = createTempFile(1024 * 1025);
myCacheListener.onAfterAddOrUpdate(file);
waitForSeededTorrents(1);
assertEquals(1, mySeeder.getNumberOfSeededTorrents());
final AnnounceableTorrent announceableTorrent = mySeeder.getTorrentsSeeder().getClient().getAnnounceableTorrents().iterator().next();
final AnnounceableFileTorrent torrent = mySeeder.getTorrentsSeeder().getClient().getAnnounceableFileTorrent(announceableTorrent.getHexInfoHash());
Torrent metadata = Torrent.load(new File(torrent.getDotTorrentFilePath()));
assertEquals(file.getAbsolutePath(), torrent.getDownloadDirPath() + File.separatorChar + metadata.getFilenames().get(0));
}
Aggregations