From 8f79ced2aa01dc91edbe609766fc493e7db7e4d2 Mon Sep 17 00:00:00 2001 From: alyokaz Date: Tue, 5 Sep 2023 13:37:08 +0100 Subject: [PATCH] Refactor obtaining downloaded file logic to own method --- .../java/intergration/IntegrationTest.java | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/test/java/intergration/IntegrationTest.java b/src/test/java/intergration/IntegrationTest.java index acaaa9d..ea049b0 100644 --- a/src/test/java/intergration/IntegrationTest.java +++ b/src/test/java/intergration/IntegrationTest.java @@ -34,11 +34,7 @@ public void canSeedAndReceiveFile() throws IOException { AKTorrent client = AKTorrent.createAndInitializeNoBeacon(); client.addPeer(server.getAddress()); client.downloadFile(FileService.getFileInfo(file)); - Optional completedFile; - //TODO add some form of timeout - do { - completedFile = client.getFile(FILENAME); - } while(completedFile.isEmpty()); + Optional completedFile = getDownloadedFile(client, FILENAME); assertEquals(-1, Files.mismatch(file.toPath(), completedFile.get().toPath())); server.shutDown(); @@ -63,10 +59,7 @@ public void canDownloadFromTwoPeers() throws IOException { nodeD.downloadFile(FileService.getFileInfo(file)); - Optional downloadedFile; - do { - downloadedFile = nodeD.getFile(FILENAME); - } while (downloadedFile.isEmpty()); + Optional downloadedFile = getDownloadedFile(nodeD, FILENAME); assertEquals(-1, Files.mismatch(file.toPath(), downloadedFile.get().toPath())); @@ -94,10 +87,7 @@ public void canDownloadFromMultiplePeers() throws IOException { client.downloadFile(FileService.getFileInfo(file)); - Optional downloadedFile; - do { - downloadedFile = client.getFile(FILENAME); - } while (downloadedFile.isEmpty()); + Optional downloadedFile = getDownloadedFile(client, FILENAME); assertEquals(-1, Files.mismatch(file.toPath(), downloadedFile.get().toPath())); nodes.forEach(AKTorrent::shutDown); @@ -149,10 +139,7 @@ public void testDiscoverTransientPeers() throws IOException { client.downloadFile(FileService.getFileInfo(file)); - Optional downloadedFile; - do{ - downloadedFile = client.getFile(file.getName()); - } while (downloadedFile.isEmpty()); + Optional downloadedFile = getDownloadedFile(client, file.getName()); assertEquals(-1, Files.mismatch(file.toPath(), downloadedFile.get().toPath())); nodeA.shutDown(); @@ -222,6 +209,15 @@ public void deadNodeWillNotBeAddedToLivePeers() throws InterruptedException { clientNode.shutDown(); } + private static Optional getDownloadedFile(AKTorrent node, String filename) { + Optional 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()); }