Skip to content

Commit

Permalink
Add intergration tests for Beacon
Browse files Browse the repository at this point in the history
Add more comprehensive integration tests for usage of Beacon
  • Loading branch information
alyokaz committed Sep 11, 2023
1 parent e76604c commit 9460dd3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/alyokaz/aktorrent/AKTorrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
import java.net.*;
import java.util.*;


//TODO This class should be refactored to get rid of the null argument for
// beacon address. It seems a lot of the logic that requires communication between
// the services could be simplified by pulling it up into this class.
public class AKTorrent {
private final NodeServer server;
private final PingServer udpServer;
private final PeerService peerService;
private final FileService fileService;
private final InetSocketAddress beaconAddress;

public AKTorrent(Server server, PingServer udpServer, PeerService peerService, FileService fileService, InetSocketAddress beaconAddress) {
public AKTorrent(NodeServer server, PingServer udpServer, PeerService peerService, FileService fileService, InetSocketAddress beaconAddress) {
this.server = server;
this.udpServer = udpServer;
this.peerService = peerService;
Expand Down Expand Up @@ -65,6 +67,8 @@ public Set<InetSocketAddress> getLivePeers() {
}

public Set<FileInfo> getAvailableFiles() {
if(beaconAddress != null)
peerService.contactBeacon(server.getServerAddress(), beaconAddress);
return fileService.updateAndGetAvailableFiles();
}

Expand Down
78 changes: 78 additions & 0 deletions src/test/java/intergration/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,83 @@ public void canDownloadPeersFromBeacon() throws IOException {
nodeB.shutDown();
}

@Test
public void canDownloadFileWithBeacon() throws IOException,
ExecutionException, InterruptedException, TimeoutException {
Beacon beacon = Beacon.createAndInitialise();

AKTorrent nodeA = AKTorrent.createAndInitialize(beacon.getAddress());
AKTorrent nodeB = AKTorrent.createAndInitialize(beacon.getAddress());

File file = getFile(FILENAME);
nodeA.seedFile(file);

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

File downloadedFile = getDownloadedFile(nodeB, FILENAME)
.get(3000, TimeUnit.MILLISECONDS);

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

beacon.shutDown();
nodeA.shutDown();
nodeB.shutDown();
}

@Test
public void canDownloadFileWithBeaconBtoA() throws IOException,
ExecutionException, InterruptedException, TimeoutException {
Beacon beacon = Beacon.createAndInitialise();

AKTorrent nodeA = AKTorrent.createAndInitialize(beacon.getAddress());
AKTorrent nodeB = AKTorrent.createAndInitialize(beacon.getAddress());

File file = getFile(FILENAME);
nodeB.seedFile(file);

nodeA.getAvailableFiles();
nodeA.downloadFile(FileService.getFileInfo(file));

File downloadedFile = getDownloadedFile(nodeA, FILENAME)
.get(30000, TimeUnit.MILLISECONDS);

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

beacon.shutDown();
nodeA.shutDown();
nodeB.shutDown();
}

@Test
public void canDownloadFromTransientPeerWithBeacon() throws ExecutionException, InterruptedException, TimeoutException, IOException {
Beacon beacon = Beacon.createAndInitialise();
InetSocketAddress beaconAddress = beacon.getAddress();

AKTorrent nodeA = AKTorrent.createAndInitialize(beaconAddress);
AKTorrent nodeB = AKTorrent.createAndInitialize(beaconAddress);
nodeA.getAvailableFiles();
AKTorrent nodeC = AKTorrent.createAndInitializeNoBeacon();

nodeC.addPeer(nodeB.getAddress());
File file = getFile(FILENAME);
nodeC.seedFile(file);
nodeC.getAvailableFiles();
nodeB.getAvailableFiles();

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

File downloadedFile = getDownloadedFile(nodeA, FILENAME)
.get(10000, TimeUnit.MILLISECONDS);

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

nodeA.shutDown();
nodeB.shutDown();
nodeC.shutDown();
}

@Test
public void deadNodeWillNotBeAddedToLivePeers() throws InterruptedException {
AKTorrent deadNode = AKTorrent.createAndInitializeNoBeacon();
Expand Down Expand Up @@ -292,6 +369,7 @@ public void preventDuplicatePeer() {
assertEquals(1, nodeA.getLivePeers().size());
}

//TODO move timeout to this method
private static Future<File> getDownloadedFile(AKTorrent node, String filename) {
return executor.submit(() -> {
Optional<File> downloadedFile;
Expand Down

0 comments on commit 9460dd3

Please sign in to comment.