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