Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Nov 6, 2024
1 parent 8e18c51 commit 91a2ece
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected void onBundleExchangeEvent(BundleExchangeEvent bundleExchangeEvent) {
@Override
protected Path pathProducer(BundleExchangeName bundleExchangeName, BundleSender bundleSender) {
return bundleExchangeName.isDownload() ? toClientPath.resolve(bundleExchangeName.encryptedBundleId()) :
toServerPath.resolve(bundleExchangeName.encryptedBundleId());
toServerPath.resolve(bundleExchangeName.encryptedBundleId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.SubmissionPublisher;
import java.util.logging.Logger;

import net.discdd.transport.TransportToBundleServerManager;

/**
Expand Down Expand Up @@ -173,7 +174,6 @@ private Void connectToServerError(Exception e, String transportTarget) {
return null;
}


private void appendToActivityLog(String message) {
requireActivity().runOnUiThread(() -> {
if (serverConnnectedStatus.getLineCount() > 20) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.discdd.bundletransport;

import android.content.Context;

import static java.util.logging.Level.INFO;
import static java.util.logging.Level.SEVERE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Logger;

import static java.util.logging.Level.SEVERE;

// BundleTransport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ public void run() {

private void processRecencyBlob(BundleExchangeServiceGrpc.BundleExchangeServiceBlockingStub blockingExchangeStub) {
var recencyBlobReq = GetRecencyBlobRequest.newBuilder().setSender(transportSenderId).build();
var recencyBlob =
blockingExchangeStub.withDeadlineAfter(Constants.GRPC_SHORT_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.getRecencyBlob(recencyBlobReq);
var recencyBlob = blockingExchangeStub.withDeadlineAfter(Constants.GRPC_SHORT_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.getRecencyBlob(recencyBlobReq);
Path blobPath = fromServerPath.resolve(RECENCY_BLOB_BIN);
try (var os = Files.newOutputStream(blobPath, StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING)) {
Expand All @@ -114,15 +113,16 @@ private void processRecencyBlob(BundleExchangeServiceGrpc.BundleExchangeServiceB
}
}

private void processDownloadBundles(List<EncryptedBundleId> bundlesToDownloadList, BundleExchangeServiceGrpc.BundleExchangeServiceStub exchangeStub) {
private void processDownloadBundles(List<EncryptedBundleId> bundlesToDownloadList,
BundleExchangeServiceGrpc.BundleExchangeServiceStub exchangeStub) {
for (var toReceive : bundlesToDownloadList) {
var path = fromServerPath.resolve(toReceive.getEncryptedId());
try (OutputStream os = Files.newOutputStream(path, StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING)) {
var completion = new CompletableFuture<Boolean>();
exchangeStub.withDeadlineAfter(Constants.GRPC_LONG_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.downloadBundle(BundleDownloadRequest.newBuilder().setBundleId(toReceive)
.setSender(transportSenderId).build(), new StreamObserver<>() {
exchangeStub.withDeadlineAfter(Constants.GRPC_LONG_TIMEOUT_MS, TimeUnit.MILLISECONDS).downloadBundle(
BundleDownloadRequest.newBuilder().setBundleId(toReceive).setSender(transportSenderId).build(),
new StreamObserver<>() {
@Override
public void onNext(BundleDownloadResponse value) {
try {
Expand Down Expand Up @@ -153,7 +153,8 @@ public void onCompleted() {
}
}

private void processUploadBundles(List<EncryptedBundleId> bundlesToUploadList, BundleExchangeServiceGrpc.BundleExchangeServiceStub exchangeStub) {
private void processUploadBundles(List<EncryptedBundleId> bundlesToUploadList,
BundleExchangeServiceGrpc.BundleExchangeServiceStub exchangeStub) {
for (var toSend : bundlesToUploadList) {
var path = fromClientPath.resolve(toSend.getEncryptedId());
StreamObserver<BundleUploadResponse> responseObserver = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest(classes = {BundleServerApplication.class, End2EndTest.End2EndTestInitializer.class})

@SpringBootTest(classes = { BundleServerApplication.class, End2EndTest.End2EndTestInitializer.class })
@TestMethodOrder(MethodOrderer.MethodName.class)
public class BundleTransportToBundleServerTest extends End2EndTest {
private static final Logger logger = Logger.getLogger(BundleTransportToBundleServerTest.class.getName());
Expand All @@ -47,13 +48,14 @@ void setUp() {
toClientPath = transportPaths.toClientPath;
toServerPath = transportPaths.toServerPath;

manager = new TransportToBundleServerManager(
transportPaths,
"localhost",
Integer.toString(BUNDLESERVER_GRPC_PORT),
(Void) -> {System.out.println("connectComplete"); return null;},
(Exception e) -> {System.out.println("connectError"); return null;}
);
manager = new TransportToBundleServerManager(transportPaths, "localhost",
Integer.toString(BUNDLESERVER_GRPC_PORT), (Void) -> {
System.out.println("connectComplete");
return null;
}, (Exception e) -> {
System.out.println("connectError");
return null;
});
}

@BeforeEach
Expand All @@ -79,8 +81,7 @@ void tearDownEach() throws IOException {

private void recursiveDelete(Path path) throws IOException {
if (Files.exists(path)) {
Files.walk(path)
.sorted(Comparator.reverseOrder()) // Delete files before directories
Files.walk(path).sorted(Comparator.reverseOrder()) // Delete files before directories
.forEach(file -> {
try {
Files.deleteIfExists(file);
Expand Down Expand Up @@ -117,9 +118,11 @@ void testRun() throws IOException {
}

@Test
void testRecencyBlob() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException {
void testRecencyBlob() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException,
IOException {
// Prepare to process recency blob
Method processRecencyBlob = TransportToBundleServerManager.class.getDeclaredMethod("processRecencyBlob", BundleExchangeServiceGrpc.BundleExchangeServiceBlockingStub.class);
Method processRecencyBlob = TransportToBundleServerManager.class.getDeclaredMethod("processRecencyBlob",
BundleExchangeServiceGrpc.BundleExchangeServiceBlockingStub.class);
processRecencyBlob.setAccessible(true);
processRecencyBlob.invoke(manager, blockingStub);

Expand All @@ -139,26 +142,32 @@ void testRecencyBlob() throws NoSuchMethodException, InvocationTargetException,
logger.info("Recency Blob last modified time: " + lastModifiedTime);
long currentTime = System.currentTimeMillis();
long lastModifiedMillis = lastModifiedTime.toMillis();
assertTrue((currentTime - lastModifiedMillis) < 50000, "Recency Blob should have been modified within the last 5 seconds.");
assertTrue((currentTime - lastModifiedMillis) < 50000,
"Recency Blob should have been modified within the last 5 seconds.");
}

@Test
void testUploadBundles() throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
void testUploadBundles() throws IOException, NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
// Create files on client side
Files.createFile(toServerPath.resolve("bundle1"));
Files.createFile(toServerPath.resolve("bundle2"));
Files.createFile(toServerPath.resolve("bundle3"));
Method populateListFromPath = TransportToBundleServerManager.class.getDeclaredMethod("populateListFromPath", Path.class);
Method populateListFromPath =
TransportToBundleServerManager.class.getDeclaredMethod("populateListFromPath", Path.class);
populateListFromPath.setAccessible(true);

// Retrieve the bundles from the client
List<EncryptedBundleId> bundlesToUpload = (List<EncryptedBundleId>) populateListFromPath.invoke(manager, toServerPath);
List<EncryptedBundleId> bundlesToUpload =
(List<EncryptedBundleId>) populateListFromPath.invoke(manager, toServerPath);

// Assert that bundles are successfully added to /server path
assertEquals(3, bundlesToUpload.size(), "The number of bundles should be 3.");

// Prepare to upload the bundles
Method processUploadBundles = TransportToBundleServerManager.class.getDeclaredMethod("processUploadBundles", List.class, BundleExchangeServiceGrpc.BundleExchangeServiceStub.class);
Method processUploadBundles =
TransportToBundleServerManager.class.getDeclaredMethod("processUploadBundles", List.class,
BundleExchangeServiceGrpc.BundleExchangeServiceStub.class);
processUploadBundles.setAccessible(true);

// Upload all bundles
Expand All @@ -167,29 +176,35 @@ void testUploadBundles() throws IOException, NoSuchMethodException, InvocationTa
// Check that the bundles were uploaded to server
for (EncryptedBundleId toUpload : bundlesToUpload) {
Path uploadPath = toServerPath.resolve(toUpload.getEncryptedId());
assertFalse(Files.exists(uploadPath), toUpload.getEncryptedId() + " should have been deleted after upload.");
assertFalse(Files.exists(uploadPath),
toUpload.getEncryptedId() + " should have been deleted after upload.");
}

assertEquals(0, Files.list(toServerPath).count());
}

@Test
void testDownloadBundles() throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
void testDownloadBundles() throws IOException, NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
// Create files on server side
Files.createFile(toClientPath.resolve("bundle1"));
Files.createFile(toClientPath.resolve("bundle2"));
Files.createFile(toClientPath.resolve("bundle3"));
Method populateListFromPath = TransportToBundleServerManager.class.getDeclaredMethod("populateListFromPath", Path.class);
Method populateListFromPath =
TransportToBundleServerManager.class.getDeclaredMethod("populateListFromPath", Path.class);
populateListFromPath.setAccessible(true);

// Retrieve the bundles from the server
List<EncryptedBundleId> bundlesToDownload = (List<EncryptedBundleId>) populateListFromPath.invoke(manager, toClientPath);
List<EncryptedBundleId> bundlesToDownload =
(List<EncryptedBundleId>) populateListFromPath.invoke(manager, toClientPath);

// Assert that bundles are successfully created
assertEquals(3, bundlesToDownload.size(), "The number of bundles should be 3.");

// Prepare to download the bundles
Method processDownloadBundles = TransportToBundleServerManager.class.getDeclaredMethod("processDownloadBundles", List.class, BundleExchangeServiceGrpc.BundleExchangeServiceStub.class);
Method processDownloadBundles =
TransportToBundleServerManager.class.getDeclaredMethod("processDownloadBundles", List.class,
BundleExchangeServiceGrpc.BundleExchangeServiceStub.class);
processDownloadBundles.setAccessible(true);

// Download all bundles from the server
Expand All @@ -211,19 +226,24 @@ void testDownloadWithSendingBundles() throws Exception {
Files.createFile(toSendDir.resolve("bundle2"));
Files.createFile(toSendDir.resolve("bundle3"));

Method populateListFromPath = TransportToBundleServerManager.class.getDeclaredMethod("populateListFromPath", Path.class);
Method populateListFromPath =
TransportToBundleServerManager.class.getDeclaredMethod("populateListFromPath", Path.class);
populateListFromPath.setAccessible(true);
List<EncryptedBundleId> bundlesToDownload = (List<EncryptedBundleId>) populateListFromPath.invoke(manager, toSendDir);
List<EncryptedBundleId> bundlesToDownload =
(List<EncryptedBundleId>) populateListFromPath.invoke(manager, toSendDir);

Method processDownloadBundles = TransportToBundleServerManager.class.getDeclaredMethod("processDownloadBundles", List.class, BundleExchangeServiceGrpc.BundleExchangeServiceStub.class);
Method processDownloadBundles =
TransportToBundleServerManager.class.getDeclaredMethod("processDownloadBundles", List.class,
BundleExchangeServiceGrpc.BundleExchangeServiceStub.class);
processDownloadBundles.setAccessible(true);

processDownloadBundles.invoke(manager, bundlesToDownload, stub);

// check if /client contains those EncryptedBundleIds
for (EncryptedBundleId toDownload : bundlesToDownload) {
Path downloadPath = toClientPath.resolve(toDownload.getEncryptedId());
assertTrue(Files.exists(downloadPath), toDownload.getEncryptedId() + " should have been sent and downloaded.");
assertTrue(Files.exists(downloadPath),
toDownload.getEncryptedId() + " should have been sent and downloaded.");
}
assertEquals(3, Files.list(toClientPath).count());
}
Expand All @@ -234,17 +254,20 @@ void testDeleteBundles() throws Exception {
Files.createFile(toClientPath.resolve("bundle1"));
Files.createFile(toClientPath.resolve("bundle2"));
Files.createFile(toClientPath.resolve("bundle3"));
Method populateListFromPath = TransportToBundleServerManager.class.getDeclaredMethod("populateListFromPath", Path.class);
Method populateListFromPath =
TransportToBundleServerManager.class.getDeclaredMethod("populateListFromPath", Path.class);
populateListFromPath.setAccessible(true);

// Retrieve the bundles from the server
List<EncryptedBundleId> bundlesToDelete = (List<EncryptedBundleId>) populateListFromPath.invoke(manager, toClientPath);
List<EncryptedBundleId> bundlesToDelete =
(List<EncryptedBundleId>) populateListFromPath.invoke(manager, toClientPath);

// Assert that bundles are successfully added to /client path
assertEquals(3, bundlesToDelete.size(), "The number of bundles should be 3.");

// Prepare to delete bundles
Method processDeleteBundles = TransportToBundleServerManager.class.getDeclaredMethod("processDeleteBundles", List.class);
Method processDeleteBundles =
TransportToBundleServerManager.class.getDeclaredMethod("processDeleteBundles", List.class);
processDeleteBundles.setAccessible(true);

// Delete all bundles
Expand All @@ -253,21 +276,26 @@ void testDeleteBundles() throws Exception {
// Assert that the bundles have been deleted on client side
for (EncryptedBundleId toDelete : bundlesToDelete) {
Path deletePath = toClientPath.resolve(toDelete.getEncryptedId());
assertFalse(Files.exists(deletePath), toDelete.getEncryptedId() + " should be deleted: " + deletePath);
assertFalse(Files.exists(deletePath), toDelete.getEncryptedId() + " should be deleted: " + deletePath);
}
}

@Test
void testUploadNonExistentBundle() throws InvocationTargetException, IllegalAccessException, IOException, NoSuchMethodException {
Method populateListFromPath = TransportToBundleServerManager.class.getDeclaredMethod("populateListFromPath", Path.class);
void testUploadNonExistentBundle() throws InvocationTargetException, IllegalAccessException, IOException,
NoSuchMethodException {
Method populateListFromPath =
TransportToBundleServerManager.class.getDeclaredMethod("populateListFromPath", Path.class);
populateListFromPath.setAccessible(true);

// Retrieve the bundles from the empty directory
List<EncryptedBundleId> bundlesToUpload = (List<EncryptedBundleId>) populateListFromPath.invoke(manager, toServerPath);
List<EncryptedBundleId> bundlesToUpload =
(List<EncryptedBundleId>) populateListFromPath.invoke(manager, toServerPath);
assertTrue(bundlesToUpload.isEmpty(), "The list of bundles should be empty.");

// Attempt to upload bundles
Method processUploadBundles = TransportToBundleServerManager.class.getDeclaredMethod("processUploadBundles", List.class, BundleExchangeServiceGrpc.BundleExchangeServiceStub.class);
Method processUploadBundles =
TransportToBundleServerManager.class.getDeclaredMethod("processUploadBundles", List.class,
BundleExchangeServiceGrpc.BundleExchangeServiceStub.class);
processUploadBundles.setAccessible(true);
processUploadBundles.invoke(manager, bundlesToUpload, stub);

Expand All @@ -276,16 +304,21 @@ void testUploadNonExistentBundle() throws InvocationTargetException, IllegalAcce
}

@Test
void testDownloadNonExistentBundle() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException {
Method populateListFromPath = TransportToBundleServerManager.class.getDeclaredMethod("populateListFromPath", Path.class);
void testDownloadNonExistentBundle() throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException, IOException {
Method populateListFromPath =
TransportToBundleServerManager.class.getDeclaredMethod("populateListFromPath", Path.class);
populateListFromPath.setAccessible(true);

// Retrieve the bundles from the empty directory
List<EncryptedBundleId> bundlesToDownload = (List<EncryptedBundleId>) populateListFromPath.invoke(manager, toClientPath);
List<EncryptedBundleId> bundlesToDownload =
(List<EncryptedBundleId>) populateListFromPath.invoke(manager, toClientPath);
assertTrue(bundlesToDownload.isEmpty(), "The list of bundles should be empty.");

// Attempt to download bundles
Method processDownloadBundles = TransportToBundleServerManager.class.getDeclaredMethod("processDownloadBundles", List.class, BundleExchangeServiceGrpc.BundleExchangeServiceStub.class);
Method processDownloadBundles =
TransportToBundleServerManager.class.getDeclaredMethod("processDownloadBundles", List.class,
BundleExchangeServiceGrpc.BundleExchangeServiceStub.class);
processDownloadBundles.setAccessible(true);
processDownloadBundles.invoke(manager, bundlesToDownload, stub);

Expand Down

0 comments on commit 91a2ece

Please sign in to comment.