mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Refactor test expectations, add test for nothing to download.
This commit is contained in:
@@ -69,20 +69,47 @@ public class ContactMailboxDownloadWorkerTest extends BrambleMockTestCase {
|
|||||||
@Test
|
@Test
|
||||||
public void testChecksConnectivityWhenStartedAndRemovesObserverWhenDestroyed() {
|
public void testChecksConnectivityWhenStartedAndRemovesObserverWhenDestroyed() {
|
||||||
// When the worker is started it should start a connectivity check
|
// When the worker is started it should start a connectivity check
|
||||||
context.checking(new Expectations() {{
|
expectStartConnectivityCheck();
|
||||||
oneOf(connectivityChecker).checkConnectivity(
|
|
||||||
with(mailboxProperties), with(worker));
|
|
||||||
}});
|
|
||||||
|
|
||||||
worker.start();
|
worker.start();
|
||||||
|
|
||||||
// When the worker is destroyed it should remove the connectivity
|
// When the worker is destroyed it should remove the connectivity
|
||||||
// and reachability observers
|
// and reachability observers
|
||||||
context.checking(new Expectations() {{
|
expectRemoveObservers();
|
||||||
oneOf(connectivityChecker).removeObserver(worker);
|
worker.destroy();
|
||||||
oneOf(torReachabilityMonitor).removeObserver(worker);
|
}
|
||||||
}});
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testChecksForFilesWhenConnectivityCheckSucceeds()
|
||||||
|
throws Exception {
|
||||||
|
// When the worker is started it should start a connectivity check
|
||||||
|
expectStartConnectivityCheck();
|
||||||
|
worker.start();
|
||||||
|
|
||||||
|
// When the connectivity check succeeds, a list-inbox task should be
|
||||||
|
// started for the first download cycle
|
||||||
|
AtomicReference<ApiCall> listTask = new AtomicReference<>();
|
||||||
|
expectStartTask(listTask);
|
||||||
|
worker.onConnectivityCheckSucceeded();
|
||||||
|
|
||||||
|
// When the list-inbox tasks runs and finds no files to download,
|
||||||
|
// it should add a Tor reachability observer
|
||||||
|
expectCheckForFiles(emptyList());
|
||||||
|
expectAddReachabilityObserver();
|
||||||
|
assertFalse(listTask.get().callApi());
|
||||||
|
|
||||||
|
// When the reachability observer is called, a list-inbox task should
|
||||||
|
// be started for the second download cycle
|
||||||
|
expectStartTask(listTask);
|
||||||
|
worker.onTorReachable();
|
||||||
|
|
||||||
|
// When the list-inbox tasks runs and finds no files to download,
|
||||||
|
// it should finish the second download cycle
|
||||||
|
expectCheckForFiles(emptyList());
|
||||||
|
assertFalse(listTask.get().callApi());
|
||||||
|
|
||||||
|
// When the worker is destroyed it should remove the connectivity
|
||||||
|
// and reachability observers
|
||||||
|
expectRemoveObservers();
|
||||||
worker.destroy();
|
worker.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,150 +117,127 @@ public class ContactMailboxDownloadWorkerTest extends BrambleMockTestCase {
|
|||||||
public void testDownloadsFilesWhenConnectivityCheckSucceeds()
|
public void testDownloadsFilesWhenConnectivityCheckSucceeds()
|
||||||
throws Exception {
|
throws Exception {
|
||||||
// When the worker is started it should start a connectivity check
|
// When the worker is started it should start a connectivity check
|
||||||
context.checking(new Expectations() {{
|
expectStartConnectivityCheck();
|
||||||
oneOf(connectivityChecker).checkConnectivity(
|
|
||||||
with(mailboxProperties), with(worker));
|
|
||||||
}});
|
|
||||||
|
|
||||||
worker.start();
|
worker.start();
|
||||||
|
|
||||||
// When the connectivity check succeeds, a list-inbox task should be
|
// When the connectivity check succeeds, a list-inbox task should be
|
||||||
// started for the first download cycle
|
// started for the first download cycle
|
||||||
AtomicReference<ApiCall> listTask = new AtomicReference<>();
|
AtomicReference<ApiCall> listTask = new AtomicReference<>();
|
||||||
context.checking(new Expectations() {{
|
expectStartTask(listTask);
|
||||||
oneOf(mailboxApiCaller).retryWithBackoff(with(any(ApiCall.class)));
|
|
||||||
will(new DoAllAction(
|
|
||||||
new CaptureArgumentAction<>(listTask, ApiCall.class, 0),
|
|
||||||
returnValue(apiCall)
|
|
||||||
));
|
|
||||||
}});
|
|
||||||
|
|
||||||
worker.onConnectivityCheckSucceeded();
|
worker.onConnectivityCheckSucceeded();
|
||||||
|
|
||||||
// When the list-inbox tasks runs and finds some files to download,
|
// When the list-inbox tasks runs and finds some files to download,
|
||||||
// it should start a download task for the first file
|
// it should start a download task for the first file
|
||||||
AtomicReference<ApiCall> downloadTask = new AtomicReference<>();
|
AtomicReference<ApiCall> downloadTask = new AtomicReference<>();
|
||||||
context.checking(new Expectations() {{
|
expectCheckForFiles(files);
|
||||||
oneOf(mailboxApi).getFiles(mailboxProperties,
|
expectStartTask(downloadTask);
|
||||||
requireNonNull(mailboxProperties.getInboxId()));
|
|
||||||
will(returnValue(files));
|
|
||||||
oneOf(mailboxApiCaller).retryWithBackoff(with(any(ApiCall.class)));
|
|
||||||
will(new DoAllAction(
|
|
||||||
new CaptureArgumentAction<>(downloadTask, ApiCall.class, 0),
|
|
||||||
returnValue(apiCall)
|
|
||||||
));
|
|
||||||
}});
|
|
||||||
|
|
||||||
assertFalse(listTask.get().callApi());
|
assertFalse(listTask.get().callApi());
|
||||||
|
|
||||||
// When the first download task runs it should download the file to the
|
// When the first download task runs it should download the file to the
|
||||||
// location provided by the file manager and start a delete task
|
// location provided by the file manager and start a delete task
|
||||||
AtomicReference<ApiCall> deleteTask = new AtomicReference<>();
|
AtomicReference<ApiCall> deleteTask = new AtomicReference<>();
|
||||||
context.checking(new Expectations() {{
|
expectDownloadFile(file1);
|
||||||
oneOf(mailboxFileManager).createTempFileForDownload();
|
expectStartTask(deleteTask);
|
||||||
will(returnValue(tempFile));
|
|
||||||
oneOf(mailboxApi).getFile(mailboxProperties,
|
|
||||||
requireNonNull(mailboxProperties.getInboxId()),
|
|
||||||
file1.name, tempFile);
|
|
||||||
oneOf(mailboxFileManager).handleDownloadedFile(tempFile);
|
|
||||||
oneOf(mailboxApiCaller).retryWithBackoff(with(any(ApiCall.class)));
|
|
||||||
will(new DoAllAction(
|
|
||||||
new CaptureArgumentAction<>(deleteTask, ApiCall.class, 0),
|
|
||||||
returnValue(apiCall)
|
|
||||||
));
|
|
||||||
}});
|
|
||||||
|
|
||||||
assertFalse(downloadTask.get().callApi());
|
assertFalse(downloadTask.get().callApi());
|
||||||
|
|
||||||
// When the first delete task runs it should delete the file, ignore
|
// When the first delete task runs it should delete the file, ignore
|
||||||
// the tolerable failure, and start a download task for the next file
|
// the tolerable failure, and start a download task for the next file
|
||||||
context.checking(new Expectations() {{
|
expectDeleteFile(file1, true); // Delete fails tolerably
|
||||||
oneOf(mailboxApi).deleteFile(mailboxProperties,
|
expectStartTask(downloadTask);
|
||||||
requireNonNull(mailboxProperties.getInboxId()), file1.name);
|
|
||||||
will(throwException(new TolerableFailureException()));
|
|
||||||
oneOf(mailboxApiCaller).retryWithBackoff(with(any(ApiCall.class)));
|
|
||||||
will(new DoAllAction(
|
|
||||||
new CaptureArgumentAction<>(downloadTask, ApiCall.class, 0),
|
|
||||||
returnValue(apiCall)
|
|
||||||
));
|
|
||||||
}});
|
|
||||||
|
|
||||||
assertFalse(deleteTask.get().callApi());
|
assertFalse(deleteTask.get().callApi());
|
||||||
|
|
||||||
// When the second download task runs it should download the file to
|
// When the second download task runs it should download the file to
|
||||||
// the location provided by the file manager and start a delete task
|
// the location provided by the file manager and start a delete task
|
||||||
context.checking(new Expectations() {{
|
expectDownloadFile(file2);
|
||||||
oneOf(mailboxFileManager).createTempFileForDownload();
|
expectStartTask(deleteTask);
|
||||||
will(returnValue(tempFile));
|
|
||||||
oneOf(mailboxApi).getFile(mailboxProperties,
|
|
||||||
requireNonNull(mailboxProperties.getInboxId()),
|
|
||||||
file2.name, tempFile);
|
|
||||||
oneOf(mailboxFileManager).handleDownloadedFile(tempFile);
|
|
||||||
oneOf(mailboxApiCaller).retryWithBackoff(with(any(ApiCall.class)));
|
|
||||||
will(new DoAllAction(
|
|
||||||
new CaptureArgumentAction<>(deleteTask, ApiCall.class, 0),
|
|
||||||
returnValue(apiCall)
|
|
||||||
));
|
|
||||||
}});
|
|
||||||
|
|
||||||
assertFalse(downloadTask.get().callApi());
|
assertFalse(downloadTask.get().callApi());
|
||||||
|
|
||||||
// When the second delete task runs it should delete the file and
|
// When the second delete task runs it should delete the file and
|
||||||
// start a list-inbox task to check for files that may have arrived
|
// start a list-inbox task to check for files that may have arrived
|
||||||
// since the first download cycle started
|
// since the first download cycle started
|
||||||
context.checking(new Expectations() {{
|
expectDeleteFile(file2, false); // Delete succeeds
|
||||||
oneOf(mailboxApi).deleteFile(mailboxProperties,
|
expectStartTask(listTask);
|
||||||
requireNonNull(mailboxProperties.getInboxId()), file2.name);
|
|
||||||
will(throwException(new TolerableFailureException()));
|
|
||||||
oneOf(mailboxApiCaller).retryWithBackoff(with(any(ApiCall.class)));
|
|
||||||
will(new DoAllAction(
|
|
||||||
new CaptureArgumentAction<>(listTask, ApiCall.class, 0),
|
|
||||||
returnValue(apiCall)
|
|
||||||
));
|
|
||||||
}});
|
|
||||||
|
|
||||||
assertFalse(deleteTask.get().callApi());
|
assertFalse(deleteTask.get().callApi());
|
||||||
|
|
||||||
// When the list-inbox tasks runs and finds no more files to download,
|
// When the list-inbox tasks runs and finds no more files to download,
|
||||||
// it should add a Tor reachability observer
|
// it should add a Tor reachability observer
|
||||||
context.checking(new Expectations() {{
|
expectCheckForFiles(emptyList());
|
||||||
oneOf(mailboxApi).getFiles(mailboxProperties,
|
expectAddReachabilityObserver();
|
||||||
requireNonNull(mailboxProperties.getInboxId()));
|
|
||||||
will(returnValue(emptyList()));
|
|
||||||
oneOf(torReachabilityMonitor).addOneShotObserver(worker);
|
|
||||||
}});
|
|
||||||
|
|
||||||
assertFalse(listTask.get().callApi());
|
assertFalse(listTask.get().callApi());
|
||||||
|
|
||||||
// When the reachability observer is called, a list-inbox task should
|
// When the reachability observer is called, a list-inbox task should
|
||||||
// be started for the second download cycle
|
// be started for the second download cycle
|
||||||
context.checking(new Expectations() {{
|
expectStartTask(listTask);
|
||||||
oneOf(mailboxApiCaller).retryWithBackoff(with(any(ApiCall.class)));
|
|
||||||
will(new DoAllAction(
|
|
||||||
new CaptureArgumentAction<>(listTask, ApiCall.class, 0),
|
|
||||||
returnValue(apiCall)
|
|
||||||
));
|
|
||||||
}});
|
|
||||||
|
|
||||||
worker.onTorReachable();
|
worker.onTorReachable();
|
||||||
|
|
||||||
// When the list-inbox tasks runs and finds no more files to download,
|
// When the list-inbox tasks runs and finds no more files to download,
|
||||||
// it should finish the second download cycle
|
// it should finish the second download cycle
|
||||||
context.checking(new Expectations() {{
|
expectCheckForFiles(emptyList());
|
||||||
oneOf(mailboxApi).getFiles(mailboxProperties,
|
|
||||||
requireNonNull(mailboxProperties.getInboxId()));
|
|
||||||
will(returnValue(emptyList()));
|
|
||||||
}});
|
|
||||||
|
|
||||||
assertFalse(listTask.get().callApi());
|
assertFalse(listTask.get().callApi());
|
||||||
|
|
||||||
// When the worker is destroyed it should remove the connectivity
|
// When the worker is destroyed it should remove the connectivity
|
||||||
// and reachability observers
|
// and reachability observers
|
||||||
|
expectRemoveObservers();
|
||||||
|
worker.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void expectStartConnectivityCheck() {
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(connectivityChecker).checkConnectivity(
|
||||||
|
with(mailboxProperties), with(worker));
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void expectStartTask(AtomicReference<ApiCall> task) {
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(mailboxApiCaller).retryWithBackoff(with(any(ApiCall.class)));
|
||||||
|
will(new DoAllAction(
|
||||||
|
new CaptureArgumentAction<>(task, ApiCall.class, 0),
|
||||||
|
returnValue(apiCall)
|
||||||
|
));
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void expectCheckForFiles(List<MailboxFile> files) throws Exception {
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(mailboxApi).getFiles(mailboxProperties,
|
||||||
|
requireNonNull(mailboxProperties.getInboxId()));
|
||||||
|
will(returnValue(files));
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void expectDownloadFile(MailboxFile file) throws Exception {
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(mailboxFileManager).createTempFileForDownload();
|
||||||
|
will(returnValue(tempFile));
|
||||||
|
oneOf(mailboxApi).getFile(mailboxProperties,
|
||||||
|
requireNonNull(mailboxProperties.getInboxId()),
|
||||||
|
file.name, tempFile);
|
||||||
|
oneOf(mailboxFileManager).handleDownloadedFile(tempFile);
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void expectDeleteFile(MailboxFile file, boolean tolerableFailure)
|
||||||
|
throws Exception {
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(mailboxApi).deleteFile(mailboxProperties,
|
||||||
|
requireNonNull(mailboxProperties.getInboxId()), file.name);
|
||||||
|
if (tolerableFailure) {
|
||||||
|
will(throwException(new TolerableFailureException()));
|
||||||
|
}
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void expectAddReachabilityObserver() {
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(torReachabilityMonitor).addOneShotObserver(worker);
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void expectRemoveObservers() {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(connectivityChecker).removeObserver(worker);
|
oneOf(connectivityChecker).removeObserver(worker);
|
||||||
oneOf(torReachabilityMonitor).removeObserver(worker);
|
oneOf(torReachabilityMonitor).removeObserver(worker);
|
||||||
}});
|
}});
|
||||||
|
|
||||||
worker.destroy();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user