Skip to content

Commit

Permalink
Refactor obtaining downloaded file logic to own method
Browse files Browse the repository at this point in the history
  • Loading branch information
alyokaz committed Sep 5, 2023
1 parent d6434cd commit 8f79ced
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/test/java/intergration/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ public void canSeedAndReceiveFile() throws IOException {
AKTorrent client = AKTorrent.createAndInitializeNoBeacon();
client.addPeer(server.getAddress());
client.downloadFile(FileService.getFileInfo(file));
Optional<File> completedFile;
//TODO add some form of timeout
do {
completedFile = client.getFile(FILENAME);
} while(completedFile.isEmpty());
Optional<File> completedFile = getDownloadedFile(client, FILENAME);

assertEquals(-1, Files.mismatch(file.toPath(), completedFile.get().toPath()));
server.shutDown();
Expand All @@ -63,10 +59,7 @@ public void canDownloadFromTwoPeers() throws IOException {

nodeD.downloadFile(FileService.getFileInfo(file));

Optional<File> downloadedFile;
do {
downloadedFile = nodeD.getFile(FILENAME);
} while (downloadedFile.isEmpty());
Optional<File> downloadedFile = getDownloadedFile(nodeD, FILENAME);

assertEquals(-1, Files.mismatch(file.toPath(), downloadedFile.get().toPath()));

Expand Down Expand Up @@ -94,10 +87,7 @@ public void canDownloadFromMultiplePeers() throws IOException {

client.downloadFile(FileService.getFileInfo(file));

Optional<File> downloadedFile;
do {
downloadedFile = client.getFile(FILENAME);
} while (downloadedFile.isEmpty());
Optional<File> downloadedFile = getDownloadedFile(client, FILENAME);

assertEquals(-1, Files.mismatch(file.toPath(), downloadedFile.get().toPath()));
nodes.forEach(AKTorrent::shutDown);
Expand Down Expand Up @@ -149,10 +139,7 @@ public void testDiscoverTransientPeers() throws IOException {

client.downloadFile(FileService.getFileInfo(file));

Optional<File> downloadedFile;
do{
downloadedFile = client.getFile(file.getName());
} while (downloadedFile.isEmpty());
Optional<File> downloadedFile = getDownloadedFile(client, file.getName());

assertEquals(-1, Files.mismatch(file.toPath(), downloadedFile.get().toPath()));
nodeA.shutDown();
Expand Down Expand Up @@ -222,6 +209,15 @@ public void deadNodeWillNotBeAddedToLivePeers() throws InterruptedException {
clientNode.shutDown();
}

private static Optional<File> getDownloadedFile(AKTorrent node, String filename) {
Optional<File> downloadedFile;
//TODO add some kind of timeout
do {
downloadedFile = node.getFile(filename);
} while (downloadedFile.isEmpty());
return downloadedFile;
}

private File getFile(String filename) {
return new File(getClass().getResource("/" + filename).getFile());
}
Expand Down

0 comments on commit 8f79ced

Please sign in to comment.