Address review feedback.

This commit is contained in:
akwizgran
2022-06-20 10:41:13 +01:00
parent 91d5698fe9
commit 4d7a3bca62
2 changed files with 20 additions and 46 deletions

View File

@@ -176,11 +176,7 @@ class MailboxFileManagerImpl implements MailboxFileManager, EventListener {
File uploadDir = createDirectoryIfNeeded(UPLOAD_DIR_NAME); File uploadDir = createDirectoryIfNeeded(UPLOAD_DIR_NAME);
File[] orphanedUploads = uploadDir.listFiles(); File[] orphanedUploads = uploadDir.listFiles();
if (orphanedUploads != null) { if (orphanedUploads != null) {
for (File f : orphanedUploads) { for (File f : orphanedUploads) delete(f);
if (!f.delete()) {
LOG.warning("Failed to delete orphaned upload");
}
}
} }
File downloadDir = createDirectoryIfNeeded(DOWNLOAD_DIR_NAME); File downloadDir = createDirectoryIfNeeded(DOWNLOAD_DIR_NAME);
File[] orphanedDownloads = downloadDir.listFiles(); File[] orphanedDownloads = downloadDir.listFiles();
@@ -217,9 +213,7 @@ class MailboxFileManagerImpl implements MailboxFileManager, EventListener {
delegate.dispose(exception, recognised); delegate.dispose(exception, recognised);
if (isHandlingComplete(exception, recognised)) { if (isHandlingComplete(exception, recognised)) {
LOG.info("Deleting downloaded file"); LOG.info("Deleting downloaded file");
if (!file.delete()) { delete(file);
LOG.warning("Failed to delete downloaded file");
}
} }
} }
} }

View File

@@ -94,10 +94,7 @@ public class MailboxUploadWorkerTest extends BrambleMockTestCase {
public void testChecksForDataWhenStartedAndRemovesObserverWhenDestroyed() public void testChecksForDataWhenStartedAndRemovesObserverWhenDestroyed()
throws Exception { throws Exception {
// When the worker is started it should check for data to send // When the worker is started it should check for data to send
context.checking(new DbExpectations() {{ expectRunTaskOnIoExecutor();
oneOf(ioExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
}});
expectCheckForDataToSendNoDataWaiting(); expectCheckForDataToSendNoDataWaiting();
worker.start(); worker.start();
@@ -117,10 +114,7 @@ public class MailboxUploadWorkerTest extends BrambleMockTestCase {
// When the worker is started it should check for data to send. As // When the worker is started it should check for data to send. As
// there's data ready to send immediately, the worker should start a // there's data ready to send immediately, the worker should start a
// connectivity check // connectivity check
context.checking(new DbExpectations() {{ expectRunTaskOnIoExecutor();
oneOf(ioExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
}});
expectCheckForDataToSendAndStartConnectivityCheck(); expectCheckForDataToSendAndStartConnectivityCheck();
worker.start(); worker.start();
@@ -131,10 +125,9 @@ public class MailboxUploadWorkerTest extends BrambleMockTestCase {
// When the connectivity check succeeds, the worker should write a file // When the connectivity check succeeds, the worker should write a file
// and start an upload task // and start an upload task
expectRunTaskOnIoExecutor();
AtomicReference<ApiCall> upload = new AtomicReference<>(); AtomicReference<ApiCall> upload = new AtomicReference<>();
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(ioExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
oneOf(mailboxFileManager).createAndWriteTempFileForUpload( oneOf(mailboxFileManager).createAndWriteTempFileForUpload(
with(contactId), with(any(OutgoingSessionRecord.class))); with(contactId), with(any(OutgoingSessionRecord.class)));
will(new DoAllAction( will(new DoAllAction(
@@ -183,10 +176,7 @@ public class MailboxUploadWorkerTest extends BrambleMockTestCase {
// When the worker is started it should check for data to send. As // When the worker is started it should check for data to send. As
// there's data ready to send immediately, the worker should start a // there's data ready to send immediately, the worker should start a
// connectivity check // connectivity check
context.checking(new DbExpectations() {{ expectRunTaskOnIoExecutor();
oneOf(ioExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
}});
expectCheckForDataToSendAndStartConnectivityCheck(); expectCheckForDataToSendAndStartConnectivityCheck();
worker.start(); worker.start();
@@ -197,10 +187,9 @@ public class MailboxUploadWorkerTest extends BrambleMockTestCase {
// When the connectivity check succeeds, the worker should write a file // When the connectivity check succeeds, the worker should write a file
// and start an upload task // and start an upload task
expectRunTaskOnIoExecutor();
AtomicReference<ApiCall> upload = new AtomicReference<>(); AtomicReference<ApiCall> upload = new AtomicReference<>();
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(ioExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
oneOf(mailboxFileManager).createAndWriteTempFileForUpload( oneOf(mailboxFileManager).createAndWriteTempFileForUpload(
with(contactId), with(any(OutgoingSessionRecord.class))); with(contactId), with(any(OutgoingSessionRecord.class)));
will(new DoAllAction( will(new DoAllAction(
@@ -244,11 +233,8 @@ public class MailboxUploadWorkerTest extends BrambleMockTestCase {
// When the worker is started it should check for data to send. As // When the worker is started it should check for data to send. As
// the data isn't ready to send immediately, the worker should // the data isn't ready to send immediately, the worker should
// schedule a wakeup // schedule a wakeup
expectRunTaskOnIoExecutor();
AtomicReference<Runnable> wakeup = new AtomicReference<>(); AtomicReference<Runnable> wakeup = new AtomicReference<>();
context.checking(new DbExpectations() {{
oneOf(ioExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
}});
expectCheckForDataToSendAndScheduleWakeup(wakeup); expectCheckForDataToSendAndScheduleWakeup(wakeup);
worker.start(); worker.start();
@@ -270,11 +256,8 @@ public class MailboxUploadWorkerTest extends BrambleMockTestCase {
// When the worker is started it should check for data to send. As // When the worker is started it should check for data to send. As
// the data isn't ready to send immediately, the worker should // the data isn't ready to send immediately, the worker should
// schedule a wakeup // schedule a wakeup
expectRunTaskOnIoExecutor();
AtomicReference<Runnable> wakeup = new AtomicReference<>(); AtomicReference<Runnable> wakeup = new AtomicReference<>();
context.checking(new DbExpectations() {{
oneOf(ioExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
}});
expectCheckForDataToSendAndScheduleWakeup(wakeup); expectCheckForDataToSendAndScheduleWakeup(wakeup);
worker.start(); worker.start();
@@ -299,11 +282,8 @@ public class MailboxUploadWorkerTest extends BrambleMockTestCase {
// When the worker is started it should check for data to send. As // When the worker is started it should check for data to send. As
// the data isn't ready to send immediately, the worker should // the data isn't ready to send immediately, the worker should
// schedule a wakeup // schedule a wakeup
expectRunTaskOnIoExecutor();
AtomicReference<Runnable> wakeup = new AtomicReference<>(); AtomicReference<Runnable> wakeup = new AtomicReference<>();
context.checking(new DbExpectations() {{
oneOf(ioExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
}});
expectCheckForDataToSendAndScheduleWakeup(wakeup); expectCheckForDataToSendAndScheduleWakeup(wakeup);
worker.start(); worker.start();
@@ -343,10 +323,7 @@ public class MailboxUploadWorkerTest extends BrambleMockTestCase {
@Test @Test
public void testCancelsCheckWhenDestroyed() throws Exception { public void testCancelsCheckWhenDestroyed() throws Exception {
// When the worker is started it should check for data to send // When the worker is started it should check for data to send
context.checking(new DbExpectations() {{ expectRunTaskOnIoExecutor();
oneOf(ioExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
}});
expectCheckForDataToSendNoDataWaiting(); expectCheckForDataToSendNoDataWaiting();
worker.start(); worker.start();
@@ -379,10 +356,7 @@ public class MailboxUploadWorkerTest extends BrambleMockTestCase {
// When the worker is started it should check for data to send. As // When the worker is started it should check for data to send. As
// there's data ready to send immediately, the worker should start a // there's data ready to send immediately, the worker should start a
// connectivity check // connectivity check
context.checking(new DbExpectations() {{ expectRunTaskOnIoExecutor();
oneOf(ioExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
}});
expectCheckForDataToSendAndStartConnectivityCheck(); expectCheckForDataToSendAndStartConnectivityCheck();
worker.start(); worker.start();
@@ -390,10 +364,9 @@ public class MailboxUploadWorkerTest extends BrambleMockTestCase {
// When the connectivity check succeeds, the worker should try to // When the connectivity check succeeds, the worker should try to
// write a file. This fails with an exception, so the worker should // write a file. This fails with an exception, so the worker should
// retry by scheduling a check for new data after a short delay // retry by scheduling a check for new data after a short delay
expectRunTaskOnIoExecutor();
AtomicReference<Runnable> check = new AtomicReference<>(); AtomicReference<Runnable> check = new AtomicReference<>();
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(ioExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
oneOf(mailboxFileManager).createAndWriteTempFileForUpload( oneOf(mailboxFileManager).createAndWriteTempFileForUpload(
with(contactId), with(any(OutgoingSessionRecord.class))); with(contactId), with(any(OutgoingSessionRecord.class)));
will(throwException(new IOException())); // Oh noes! will(throwException(new IOException())); // Oh noes!
@@ -414,6 +387,13 @@ public class MailboxUploadWorkerTest extends BrambleMockTestCase {
worker.destroy(); worker.destroy();
} }
private void expectRunTaskOnIoExecutor() {
context.checking(new Expectations() {{
oneOf(ioExecutor).execute(with(any(Runnable.class)));
will(new RunAction());
}});
}
private void expectCheckForDataToSendNoDataWaiting() throws Exception { private void expectCheckForDataToSendNoDataWaiting() throws Exception {
Transaction txn = new Transaction(null, true); Transaction txn = new Transaction(null, true);