mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
Merge branch '2229-mailbox-client' into 'master'
Add connectivity check tasks, refactor mailbox properties See merge request briar/briar!1650
This commit is contained in:
@@ -21,8 +21,7 @@ import org.briarproject.bramble.api.db.Metadata;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxFolderId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxUpdate;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxUpdateWithMailbox;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxVersion;
|
||||
@@ -52,6 +51,7 @@ import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY
|
||||
import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_ONION;
|
||||
import static org.briarproject.bramble.api.mailbox.MailboxUpdateManager.PROP_KEY_OUTBOXID;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAuthor;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMailboxProperties;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMessage;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
@@ -111,22 +111,18 @@ public class ClientHelperImplTest extends BrambleMockTestCase {
|
||||
someServerSupports = BdfList.of(BdfList.of(1, 0));
|
||||
validMailboxUpdateWithMailbox = new MailboxUpdateWithMailbox(
|
||||
singletonList(new MailboxVersion(1, 0)),
|
||||
singletonList(new MailboxVersion(1, 0)),
|
||||
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd",
|
||||
new MailboxAuthToken(getRandomId()),
|
||||
new MailboxFolderId(getRandomId()),
|
||||
new MailboxFolderId(getRandomId()));
|
||||
getMailboxProperties(false,
|
||||
singletonList(new MailboxVersion(1, 0))));
|
||||
}
|
||||
|
||||
private BdfDictionary getValidMailboxUpdateWithMailboxDict() {
|
||||
BdfDictionary dict = new BdfDictionary();
|
||||
dict.put(PROP_KEY_ONION, validMailboxUpdateWithMailbox.getOnion());
|
||||
dict.put(PROP_KEY_AUTHTOKEN, validMailboxUpdateWithMailbox
|
||||
.getAuthToken().getBytes());
|
||||
dict.put(PROP_KEY_INBOXID, validMailboxUpdateWithMailbox.getInboxId()
|
||||
.getBytes());
|
||||
dict.put(PROP_KEY_OUTBOXID, validMailboxUpdateWithMailbox.getOutboxId()
|
||||
.getBytes());
|
||||
MailboxProperties properties =
|
||||
validMailboxUpdateWithMailbox.getMailboxProperties();
|
||||
dict.put(PROP_KEY_ONION, properties.getOnion());
|
||||
dict.put(PROP_KEY_AUTHTOKEN, properties.getAuthToken());
|
||||
dict.put(PROP_KEY_INBOXID, properties.getInboxId());
|
||||
dict.put(PROP_KEY_OUTBOXID, properties.getOutboxId());
|
||||
return dict;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
package org.briarproject.bramble.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.Cancellable;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.mailbox.ConnectivityChecker.ConnectivityObserver;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import static org.briarproject.bramble.mailbox.ConnectivityCheckerImpl.CONNECTIVITY_CHECK_FRESHNESS_MS;
|
||||
import static org.briarproject.bramble.mailbox.MailboxApi.CLIENT_SUPPORTS;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMailboxProperties;
|
||||
|
||||
public class ConnectivityCheckerImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final Clock clock = context.mock(Clock.class);
|
||||
private final MailboxApiCaller mailboxApiCaller =
|
||||
context.mock(MailboxApiCaller.class);
|
||||
private final ApiCall apiCall = context.mock(ApiCall.class);
|
||||
private final Cancellable task = context.mock(Cancellable.class);
|
||||
private final ConnectivityObserver observer1 =
|
||||
context.mock(ConnectivityObserver.class, "1");
|
||||
private final ConnectivityObserver observer2 =
|
||||
context.mock(ConnectivityObserver.class, "2");
|
||||
|
||||
private final MailboxProperties properties =
|
||||
getMailboxProperties(true, CLIENT_SUPPORTS);
|
||||
private final long now = System.currentTimeMillis();
|
||||
|
||||
@Test
|
||||
public void testFirstObserverStartsCheck() {
|
||||
ConnectivityCheckerImpl checker = createChecker();
|
||||
|
||||
// When checkConnectivity() is called a check should be started
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(mailboxApiCaller).retryWithBackoff(apiCall);
|
||||
will(returnValue(task));
|
||||
}});
|
||||
|
||||
checker.checkConnectivity(properties, observer1);
|
||||
|
||||
// When the check succeeds the observer should be called
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(observer1).onConnectivityCheckSucceeded();
|
||||
}});
|
||||
|
||||
checker.onConnectivityCheckSucceeded(now);
|
||||
|
||||
// The observer should not be called again when subsequent checks
|
||||
// succeed
|
||||
checker.onConnectivityCheckSucceeded(now);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObserverIsAddedToExistingCheck() {
|
||||
ConnectivityCheckerImpl checker = createChecker();
|
||||
|
||||
// When checkConnectivity() is called a check should be started
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(mailboxApiCaller).retryWithBackoff(apiCall);
|
||||
will(returnValue(task));
|
||||
}});
|
||||
|
||||
checker.checkConnectivity(properties, observer1);
|
||||
|
||||
// When checkConnectivity() is called again before the first check
|
||||
// succeeds, the observer should be added to the existing check
|
||||
checker.checkConnectivity(properties, observer2);
|
||||
|
||||
// When the check succeeds both observers should be called
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(observer1).onConnectivityCheckSucceeded();
|
||||
oneOf(observer2).onConnectivityCheckSucceeded();
|
||||
}});
|
||||
|
||||
checker.onConnectivityCheckSucceeded(now);
|
||||
|
||||
// The observers should not be called again when subsequent checks
|
||||
// succeed
|
||||
checker.onConnectivityCheckSucceeded(now);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFreshResultIsReused() {
|
||||
ConnectivityCheckerImpl checker = createChecker();
|
||||
|
||||
// When checkConnectivity() is called a check should be started
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(mailboxApiCaller).retryWithBackoff(apiCall);
|
||||
will(returnValue(task));
|
||||
}});
|
||||
|
||||
checker.checkConnectivity(properties, observer1);
|
||||
|
||||
// When the check succeeds the observer should be called
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(observer1).onConnectivityCheckSucceeded();
|
||||
}});
|
||||
|
||||
checker.onConnectivityCheckSucceeded(now);
|
||||
|
||||
// When checkConnectivity() is called again within
|
||||
// CONNECTIVITY_CHECK_FRESHNESS_MS the observer should be called with
|
||||
// the result of the recent check
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now + CONNECTIVITY_CHECK_FRESHNESS_MS));
|
||||
oneOf(observer2).onConnectivityCheckSucceeded();
|
||||
}});
|
||||
|
||||
checker.checkConnectivity(properties, observer2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaleResultIsNotReused() {
|
||||
ConnectivityCheckerImpl checker = createChecker();
|
||||
|
||||
// When checkConnectivity() is called a check should be started
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(mailboxApiCaller).retryWithBackoff(apiCall);
|
||||
will(returnValue(task));
|
||||
}});
|
||||
|
||||
checker.checkConnectivity(properties, observer1);
|
||||
|
||||
// When the check succeeds the observer should be called
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(observer1).onConnectivityCheckSucceeded();
|
||||
}});
|
||||
|
||||
checker.onConnectivityCheckSucceeded(now);
|
||||
|
||||
// When checkConnectivity() is called again after more than
|
||||
// CONNECTIVITY_CHECK_FRESHNESS_MS another check should be started
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now + CONNECTIVITY_CHECK_FRESHNESS_MS + 1));
|
||||
oneOf(mailboxApiCaller).retryWithBackoff(apiCall);
|
||||
will(returnValue(task));
|
||||
}});
|
||||
|
||||
checker.checkConnectivity(properties, observer2);
|
||||
|
||||
// When the check succeeds the observer should be called
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(observer2).onConnectivityCheckSucceeded();
|
||||
}});
|
||||
|
||||
checker.onConnectivityCheckSucceeded(
|
||||
now + CONNECTIVITY_CHECK_FRESHNESS_MS + 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCheckIsCancelledWhenCheckerIsDestroyed() {
|
||||
ConnectivityCheckerImpl checker = createChecker();
|
||||
|
||||
// When checkConnectivity() is called a check should be started
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(mailboxApiCaller).retryWithBackoff(apiCall);
|
||||
will(returnValue(task));
|
||||
}});
|
||||
|
||||
checker.checkConnectivity(properties, observer1);
|
||||
|
||||
// When the checker is destroyed the check should be cancelled
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(task).cancel();
|
||||
}});
|
||||
|
||||
checker.destroy();
|
||||
|
||||
// If the check runs anyway (cancellation came too late) the observer
|
||||
// should not be called
|
||||
checker.onConnectivityCheckSucceeded(now);
|
||||
}
|
||||
|
||||
private ConnectivityCheckerImpl createChecker() {
|
||||
|
||||
return new ConnectivityCheckerImpl(clock, mailboxApiCaller) {
|
||||
@Override
|
||||
@Nonnull
|
||||
protected ApiCall createConnectivityCheckTask(
|
||||
@Nonnull MailboxProperties properties) {
|
||||
return apiCall;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package org.briarproject.bramble.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.Cancellable;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.mailbox.ConnectivityChecker.ConnectivityObserver;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.CaptureArgumentAction;
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.lib.action.DoAllAction;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.briarproject.bramble.mailbox.MailboxApi.CLIENT_SUPPORTS;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMailboxProperties;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ContactMailboxConnectivityCheckerTest extends BrambleMockTestCase {
|
||||
|
||||
private final Clock clock = context.mock(Clock.class);
|
||||
private final MailboxApiCaller mailboxApiCaller =
|
||||
context.mock(MailboxApiCaller.class);
|
||||
private final MailboxApi mailboxApi = context.mock(MailboxApi.class);
|
||||
private final Cancellable task = context.mock(Cancellable.class);
|
||||
private final ConnectivityObserver observer =
|
||||
context.mock(ConnectivityObserver.class);
|
||||
|
||||
private final MailboxProperties properties =
|
||||
getMailboxProperties(false, CLIENT_SUPPORTS);
|
||||
private final long now = System.currentTimeMillis();
|
||||
|
||||
@Test
|
||||
public void testObserverIsCalledWhenCheckSucceeds() throws Exception {
|
||||
ContactMailboxConnectivityChecker checker = createChecker();
|
||||
AtomicReference<ApiCall> apiCall = new AtomicReference<>(null);
|
||||
|
||||
// When checkConnectivity() is called a check should be started
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(mailboxApiCaller).retryWithBackoff(with(any(ApiCall.class)));
|
||||
will(new DoAllAction(
|
||||
new CaptureArgumentAction<>(apiCall, ApiCall.class, 0),
|
||||
returnValue(task)
|
||||
));
|
||||
}});
|
||||
|
||||
checker.checkConnectivity(properties, observer);
|
||||
|
||||
// When the check succeeds the observer should be called
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(mailboxApi).checkStatus(properties);
|
||||
will(returnValue(true));
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(observer).onConnectivityCheckSucceeded();
|
||||
}});
|
||||
|
||||
// The call should not be retried
|
||||
assertFalse(apiCall.get().callApi());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObserverIsNotCalledWhenCheckFails() throws Exception {
|
||||
ContactMailboxConnectivityChecker checker = createChecker();
|
||||
AtomicReference<ApiCall> apiCall = new AtomicReference<>(null);
|
||||
|
||||
// When checkConnectivity() is called a check should be started
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(mailboxApiCaller).retryWithBackoff(with(any(ApiCall.class)));
|
||||
will(new DoAllAction(
|
||||
new CaptureArgumentAction<>(apiCall, ApiCall.class, 0),
|
||||
returnValue(task)
|
||||
));
|
||||
}});
|
||||
|
||||
checker.checkConnectivity(properties, observer);
|
||||
|
||||
// When the check fails, the observer should not be called
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(mailboxApi).checkStatus(properties);
|
||||
will(throwException(new IOException()));
|
||||
}});
|
||||
|
||||
// The call should be retried
|
||||
assertTrue(apiCall.get().callApi());
|
||||
}
|
||||
|
||||
private ContactMailboxConnectivityChecker createChecker() {
|
||||
return new ContactMailboxConnectivityChecker(clock, mailboxApiCaller,
|
||||
mailboxApi);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.briarproject.bramble.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.Cancellable;
|
||||
import org.briarproject.bramble.api.Supplier;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.CaptureArgumentAction;
|
||||
@@ -23,8 +22,7 @@ public class MailboxApiCallerImplTest extends BrambleMockTestCase {
|
||||
private final TaskScheduler taskScheduler =
|
||||
context.mock(TaskScheduler.class);
|
||||
private final Executor ioExecutor = context.mock(Executor.class);
|
||||
private final BooleanSupplier supplier =
|
||||
context.mock(BooleanSupplier.class);
|
||||
private final ApiCall apiCall = context.mock(ApiCall.class);
|
||||
private final Cancellable scheduledTask = context.mock(Cancellable.class);
|
||||
|
||||
private final MailboxApiCallerImpl caller =
|
||||
@@ -39,12 +37,12 @@ public class MailboxApiCallerImplTest extends BrambleMockTestCase {
|
||||
will(new CaptureArgumentAction<>(runnable, Runnable.class, 0));
|
||||
}});
|
||||
|
||||
caller.retryWithBackoff(supplier);
|
||||
caller.retryWithBackoff(apiCall);
|
||||
|
||||
// When the task runs, the supplier should be called. The supplier
|
||||
// When the task runs, the API call should be called. The call
|
||||
// returns false, so no retries should be scheduled
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(supplier).get();
|
||||
oneOf(apiCall).callApi();
|
||||
will(returnValue(false));
|
||||
}});
|
||||
|
||||
@@ -60,12 +58,12 @@ public class MailboxApiCallerImplTest extends BrambleMockTestCase {
|
||||
will(new CaptureArgumentAction<>(runnable, Runnable.class, 0));
|
||||
}});
|
||||
|
||||
Cancellable returned = caller.retryWithBackoff(supplier);
|
||||
Cancellable returned = caller.retryWithBackoff(apiCall);
|
||||
|
||||
// When the task runs, the supplier should be called. The supplier
|
||||
// When the task runs, the API call should be called. The call
|
||||
// returns true, so a retry should be scheduled
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(supplier).get();
|
||||
oneOf(apiCall).callApi();
|
||||
will(returnValue(true));
|
||||
oneOf(taskScheduler).schedule(with(any(Runnable.class)),
|
||||
with(ioExecutor), with(MIN_RETRY_INTERVAL_MS),
|
||||
@@ -90,7 +88,7 @@ public class MailboxApiCallerImplTest extends BrambleMockTestCase {
|
||||
returned.cancel();
|
||||
|
||||
// If the scheduled task runs anyway (cancellation came too late),
|
||||
// the supplier should not be called and no further tries should be
|
||||
// the API call should not be called and no further tries should be
|
||||
// scheduled
|
||||
runnable.get().run();
|
||||
}
|
||||
@@ -114,13 +112,13 @@ public class MailboxApiCallerImplTest extends BrambleMockTestCase {
|
||||
will(new CaptureArgumentAction<>(runnable, Runnable.class, 0));
|
||||
}});
|
||||
|
||||
caller.retryWithBackoff(supplier);
|
||||
caller.retryWithBackoff(apiCall);
|
||||
|
||||
// Each time the task runs, the supplier returns true, so a retry
|
||||
// Each time the task runs, the API call returns true, so a retry
|
||||
// should be scheduled with a longer interval
|
||||
for (long interval : expectedIntervals) {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(supplier).get();
|
||||
oneOf(apiCall).callApi();
|
||||
will(returnValue(true));
|
||||
oneOf(taskScheduler).schedule(with(any(Runnable.class)),
|
||||
with(ioExecutor), with(interval), with(MILLISECONDS));
|
||||
@@ -134,8 +132,4 @@ public class MailboxApiCallerImplTest extends BrambleMockTestCase {
|
||||
runnable.get().run();
|
||||
}
|
||||
}
|
||||
|
||||
// Reify the generic type to mollify jMock
|
||||
private interface BooleanSupplier extends Supplier<Boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,9 @@ import okio.Buffer;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.briarproject.bramble.mailbox.MailboxApi.CLIENT_SUPPORTS;
|
||||
import static org.briarproject.bramble.test.TestUtils.getContactId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMailboxProperties;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.test.TestUtils.mailboxPropertiesEqual;
|
||||
@@ -102,9 +104,9 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
String baseUrl = getBaseUrl(server);
|
||||
List<MailboxVersion> versions = singletonList(new MailboxVersion(1, 0));
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token, new ArrayList<>());
|
||||
MailboxProperties properties2 =
|
||||
new MailboxProperties(baseUrl, token2, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token2, new ArrayList<>());
|
||||
|
||||
RecordedRequest request;
|
||||
|
||||
@@ -199,9 +201,9 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token, new ArrayList<>());
|
||||
MailboxProperties properties2 =
|
||||
new MailboxProperties(baseUrl, token2, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token2, new ArrayList<>());
|
||||
|
||||
// valid response with valid token
|
||||
mailboxPropertiesEqual(properties2, api.setup(properties));
|
||||
@@ -282,7 +284,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testSetupOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
getMailboxProperties(false, CLIENT_SUPPORTS);
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> api.setup(properties)
|
||||
@@ -298,9 +300,9 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token, new ArrayList<>());
|
||||
MailboxProperties properties2 =
|
||||
new MailboxProperties(baseUrl, token2, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token2, new ArrayList<>());
|
||||
|
||||
assertTrue(api.checkStatus(properties));
|
||||
RecordedRequest request1 = server.takeRequest();
|
||||
@@ -318,16 +320,6 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
assertToken(request3, token);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStatusOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> api.checkStatus(properties)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWipe() throws Exception {
|
||||
MockWebServer server = new MockWebServer();
|
||||
@@ -338,9 +330,9 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token, new ArrayList<>());
|
||||
MailboxProperties properties2 =
|
||||
new MailboxProperties(baseUrl, token2, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token2, new ArrayList<>());
|
||||
|
||||
api.wipeMailbox(properties);
|
||||
RecordedRequest request1 = server.takeRequest();
|
||||
@@ -370,7 +362,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testWipeOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
getMailboxProperties(false, CLIENT_SUPPORTS);
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
api.wipeMailbox(properties));
|
||||
}
|
||||
@@ -384,7 +376,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token, new ArrayList<>());
|
||||
|
||||
// contact gets added as expected
|
||||
api.addContact(properties, mailboxContact);
|
||||
@@ -416,7 +408,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testAddContactOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
getMailboxProperties(false, CLIENT_SUPPORTS);
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
api.addContact(properties, mailboxContact));
|
||||
}
|
||||
@@ -431,7 +423,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token, new ArrayList<>());
|
||||
|
||||
// contact gets deleted as expected
|
||||
api.deleteContact(properties, contactId);
|
||||
@@ -468,7 +460,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testDeleteContactOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
getMailboxProperties(false, CLIENT_SUPPORTS);
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
api.deleteContact(properties, contactId));
|
||||
}
|
||||
@@ -495,7 +487,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token, new ArrayList<>());
|
||||
|
||||
// valid response with two contacts
|
||||
assertEquals(singletonList(contactId), api.getContacts(properties));
|
||||
@@ -560,7 +552,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testGetContactsOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
getMailboxProperties(false, CLIENT_SUPPORTS);
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> api.getContacts(properties)
|
||||
@@ -580,7 +572,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token, new ArrayList<>());
|
||||
|
||||
// file gets uploaded as expected
|
||||
api.addFile(properties, contactInboxId, file);
|
||||
@@ -639,7 +631,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token, new ArrayList<>());
|
||||
|
||||
// valid response with one file
|
||||
List<MailboxFile> received1 = api.getFiles(properties, contactInboxId);
|
||||
@@ -735,7 +727,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token, new ArrayList<>());
|
||||
|
||||
// file gets downloaded as expected
|
||||
api.getFile(properties, contactOutboxId, name, file1);
|
||||
@@ -779,7 +771,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token, new ArrayList<>());
|
||||
|
||||
// file gets deleted as expected
|
||||
api.deleteFile(properties, contactInboxId, name);
|
||||
@@ -843,7 +835,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
new MailboxProperties(baseUrl, token, new ArrayList<>());
|
||||
|
||||
// valid response with one folders
|
||||
assertEquals(singletonList(id1), api.getFolders(properties));
|
||||
@@ -912,7 +904,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testGetFoldersOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
getMailboxProperties(false, CLIENT_SUPPORTS);
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
api.getFolders(properties));
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
||||
|
||||
if (ownerProperties != null) return;
|
||||
MailboxProperties setupProperties = new MailboxProperties(
|
||||
URL_BASE, SETUP_TOKEN, true, new ArrayList<>());
|
||||
URL_BASE, SETUP_TOKEN, new ArrayList<>());
|
||||
ownerProperties = api.setup(setupProperties);
|
||||
}
|
||||
|
||||
@@ -108,13 +108,29 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
||||
|
||||
// new setup doesn't work as mailbox is stopping
|
||||
MailboxProperties setupProperties = new MailboxProperties(
|
||||
URL_BASE, SETUP_TOKEN, true, new ArrayList<>());
|
||||
URL_BASE, SETUP_TOKEN, new ArrayList<>());
|
||||
assertThrows(ApiException.class, () -> api.setup(setupProperties));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStatus() throws Exception {
|
||||
// Owner calls status endpoint
|
||||
assertTrue(api.checkStatus(ownerProperties));
|
||||
|
||||
// Owner adds contact
|
||||
ContactId contactId = new ContactId(1);
|
||||
MailboxContact contact = getMailboxContact(contactId);
|
||||
MailboxProperties contactProperties = new MailboxProperties(
|
||||
ownerProperties.getBaseUrl(), contact.token,
|
||||
new ArrayList<>(), contact.inboxId, contact.outboxId);
|
||||
api.addContact(ownerProperties, contact);
|
||||
|
||||
// Contact calls status endpoint
|
||||
assertTrue(api.checkStatus(contactProperties));
|
||||
|
||||
// Owner deletes contact again to leave clean state for other tests
|
||||
api.deleteContact(ownerProperties, contactId);
|
||||
assertEquals(emptyList(), api.getContacts(ownerProperties));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -151,8 +167,8 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
||||
ContactId contactId = new ContactId(1);
|
||||
MailboxContact contact = getMailboxContact(contactId);
|
||||
MailboxProperties contactProperties = new MailboxProperties(
|
||||
ownerProperties.getBaseUrl(), contact.token, false,
|
||||
new ArrayList<>());
|
||||
ownerProperties.getBaseUrl(), contact.token,
|
||||
new ArrayList<>(), contact.inboxId, contact.outboxId);
|
||||
api.addContact(ownerProperties, contact);
|
||||
|
||||
// upload a file for our contact
|
||||
|
||||
@@ -57,7 +57,7 @@ public class MailboxPairingTaskImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final String onion = getRandomString(56);
|
||||
private final byte[] onionBytes = getRandomBytes(32);
|
||||
private final String onionAddress = "http://" + onion + ".onion";
|
||||
private final String baseUrl = "http://" + onion + ".onion"; // TODO
|
||||
private final MailboxAuthToken setupToken =
|
||||
new MailboxAuthToken(getRandomId());
|
||||
private final MailboxAuthToken ownerToken =
|
||||
@@ -65,9 +65,9 @@ public class MailboxPairingTaskImplTest extends BrambleMockTestCase {
|
||||
private final String validPayload = getValidPayload();
|
||||
private final long time = System.currentTimeMillis();
|
||||
private final MailboxProperties setupProperties = new MailboxProperties(
|
||||
onionAddress, setupToken, true, new ArrayList<>());
|
||||
baseUrl, setupToken, new ArrayList<>());
|
||||
private final MailboxProperties ownerProperties = new MailboxProperties(
|
||||
onionAddress, ownerToken, true, new ArrayList<>());
|
||||
baseUrl, ownerToken, new ArrayList<>());
|
||||
|
||||
@Test
|
||||
public void testInitialQrCodeReceivedState() {
|
||||
|
||||
@@ -97,7 +97,7 @@ public class MailboxSettingsManagerImplTest extends BrambleMockTestCase {
|
||||
expectedSettings.put(SETTINGS_KEY_TOKEN, token.toString());
|
||||
expectedSettings.putIntArray(SETTINGS_KEY_SERVER_SUPPORTS,
|
||||
serverSupportsInts);
|
||||
MailboxProperties properties = new MailboxProperties(onion, token, true,
|
||||
MailboxProperties properties = new MailboxProperties(onion, token,
|
||||
serverSupports);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
|
||||
@@ -11,8 +11,6 @@ import org.briarproject.bramble.api.data.MetadataParser;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.db.Metadata;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxFolderId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxSettingsManager;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxUpdate;
|
||||
@@ -48,6 +46,7 @@ import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
||||
import static org.briarproject.bramble.api.sync.validation.IncomingMessageHook.DeliveryAction.ACCEPT_DO_NOT_SHARE;
|
||||
import static org.briarproject.bramble.test.TestUtils.getContact;
|
||||
import static org.briarproject.bramble.test.TestUtils.getGroup;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMailboxProperties;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMessage;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.test.TestUtils.hasEvent;
|
||||
@@ -82,6 +81,7 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
private final List<MailboxVersion> someServerSupportsList;
|
||||
private final BdfList someServerSupports;
|
||||
private final BdfList emptyServerSupports = new BdfList();
|
||||
private final MailboxProperties updateProps;
|
||||
private final MailboxUpdateWithMailbox updateWithMailbox;
|
||||
private final MailboxUpdate updateNoMailbox;
|
||||
private final MailboxProperties ownProps;
|
||||
@@ -108,22 +108,16 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
|
||||
updateNoMailbox = new MailboxUpdate(someClientSupportsList);
|
||||
|
||||
ownProps = new MailboxProperties("http://bar.onion",
|
||||
new MailboxAuthToken(getRandomId()), true,
|
||||
someServerSupportsList);
|
||||
updateProps = getMailboxProperties(false, someServerSupportsList);
|
||||
ownProps = new MailboxProperties(updateProps.getBaseUrl(),
|
||||
updateProps.getAuthToken(), someServerSupportsList);
|
||||
updateWithMailbox = new MailboxUpdateWithMailbox(someClientSupportsList,
|
||||
someServerSupportsList, ownProps.getOnion(),
|
||||
new MailboxAuthToken(getRandomId()),
|
||||
new MailboxFolderId(getRandomId()),
|
||||
new MailboxFolderId(getRandomId()));
|
||||
updateProps);
|
||||
propsDict = new BdfDictionary();
|
||||
propsDict.put(PROP_KEY_ONION, updateWithMailbox.getOnion());
|
||||
propsDict.put(PROP_KEY_AUTHTOKEN, updateWithMailbox.getAuthToken()
|
||||
.getBytes());
|
||||
propsDict.put(PROP_KEY_INBOXID, updateWithMailbox.getInboxId()
|
||||
.getBytes());
|
||||
propsDict.put(PROP_KEY_OUTBOXID, updateWithMailbox.getOutboxId()
|
||||
.getBytes());
|
||||
propsDict.put(PROP_KEY_ONION, updateProps.getOnion());
|
||||
propsDict.put(PROP_KEY_AUTHTOKEN, updateProps.getAuthToken());
|
||||
propsDict.put(PROP_KEY_INBOXID, updateProps.getInboxId());
|
||||
propsDict.put(PROP_KEY_OUTBOXID, updateProps.getOutboxId());
|
||||
}
|
||||
|
||||
private MailboxUpdateManagerImpl createInstance(
|
||||
@@ -219,11 +213,11 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn);
|
||||
will(returnValue(ownProps));
|
||||
oneOf(crypto).generateUniqueId();
|
||||
will(returnValue(updateWithMailbox.getAuthToken()));
|
||||
will(returnValue(updateProps.getAuthToken()));
|
||||
oneOf(crypto).generateUniqueId();
|
||||
will(returnValue(updateWithMailbox.getInboxId()));
|
||||
will(returnValue(updateProps.getInboxId()));
|
||||
oneOf(crypto).generateUniqueId();
|
||||
will(returnValue(updateWithMailbox.getOutboxId()));
|
||||
will(returnValue(updateProps.getOutboxId()));
|
||||
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
|
||||
MAJOR_VERSION, contact);
|
||||
will(returnValue(contactGroup));
|
||||
@@ -478,11 +472,11 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
oneOf(mailboxSettingsManager).getOwnMailboxProperties(txn);
|
||||
will(returnValue(ownProps));
|
||||
oneOf(crypto).generateUniqueId();
|
||||
will(returnValue(updateWithMailbox.getAuthToken()));
|
||||
will(returnValue(updateProps.getAuthToken()));
|
||||
oneOf(crypto).generateUniqueId();
|
||||
will(returnValue(updateWithMailbox.getInboxId()));
|
||||
will(returnValue(updateProps.getInboxId()));
|
||||
oneOf(crypto).generateUniqueId();
|
||||
will(returnValue(updateWithMailbox.getOutboxId()));
|
||||
will(returnValue(updateProps.getOutboxId()));
|
||||
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
|
||||
MAJOR_VERSION, contact);
|
||||
will(returnValue(contactGroup));
|
||||
@@ -668,11 +662,11 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
oneOf(db).getContacts(txn);
|
||||
will(returnValue(contacts));
|
||||
oneOf(crypto).generateUniqueId();
|
||||
will(returnValue(updateWithMailbox.getAuthToken()));
|
||||
will(returnValue(updateProps.getAuthToken()));
|
||||
oneOf(crypto).generateUniqueId();
|
||||
will(returnValue(updateWithMailbox.getInboxId()));
|
||||
will(returnValue(updateProps.getInboxId()));
|
||||
oneOf(crypto).generateUniqueId();
|
||||
will(returnValue(updateWithMailbox.getOutboxId()));
|
||||
will(returnValue(updateProps.getOutboxId()));
|
||||
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
|
||||
MAJOR_VERSION, contact);
|
||||
will(returnValue(contactGroup));
|
||||
|
||||
@@ -6,8 +6,7 @@ import org.briarproject.bramble.api.data.BdfDictionary;
|
||||
import org.briarproject.bramble.api.data.BdfEntry;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.data.MetadataEncoder;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxFolderId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxUpdate;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxUpdateManager;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxUpdateWithMailbox;
|
||||
@@ -24,8 +23,8 @@ import java.util.List;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.briarproject.bramble.test.TestUtils.getGroup;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMailboxProperties;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMessage;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MailboxUpdateValidatorTest extends BrambleMockTestCase {
|
||||
@@ -35,7 +34,6 @@ public class MailboxUpdateValidatorTest extends BrambleMockTestCase {
|
||||
private final BdfDictionary bdfDict;
|
||||
private final BdfList emptyServerSupports;
|
||||
private final BdfList someClientSupports;
|
||||
private final List<MailboxVersion> someClientSupportsList;
|
||||
private final BdfList someServerSupports;
|
||||
private final MailboxUpdateWithMailbox updateMailbox;
|
||||
private final MailboxUpdate updateNoMailbox;
|
||||
@@ -49,17 +47,15 @@ public class MailboxUpdateValidatorTest extends BrambleMockTestCase {
|
||||
// {@link ClientHelper#parseAndValidateMailboxUpdate(BdfList, BdfList, BdfDictionary)}
|
||||
emptyServerSupports = new BdfList();
|
||||
someClientSupports = BdfList.of(BdfList.of(1, 0));
|
||||
someClientSupportsList = singletonList(new MailboxVersion(1, 0));
|
||||
List<MailboxVersion> someClientSupportsList =
|
||||
singletonList(new MailboxVersion(1, 0));
|
||||
someServerSupports = BdfList.of(BdfList.of(1, 0));
|
||||
bdfDict = BdfDictionary.of(new BdfEntry("foo", "bar"));
|
||||
|
||||
MailboxProperties props = getMailboxProperties(false,
|
||||
singletonList(new MailboxVersion(1, 0)));
|
||||
updateMailbox = new MailboxUpdateWithMailbox(
|
||||
singletonList(new MailboxVersion(1, 0)),
|
||||
singletonList(new MailboxVersion(1, 0)),
|
||||
"baz",
|
||||
new MailboxAuthToken(getRandomId()),
|
||||
new MailboxFolderId(getRandomId()),
|
||||
new MailboxFolderId(getRandomId()));
|
||||
singletonList(new MailboxVersion(1, 0)), props);
|
||||
updateNoMailbox = new MailboxUpdate(someClientSupportsList);
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package org.briarproject.bramble.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.Cancellable;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.db.TransactionManager;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxSettingsManager;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.mailbox.ConnectivityChecker.ConnectivityObserver;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.CaptureArgumentAction;
|
||||
import org.briarproject.bramble.test.DbExpectations;
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.lib.action.DoAllAction;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.briarproject.bramble.mailbox.MailboxApi.CLIENT_SUPPORTS;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMailboxProperties;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class OwnMailboxConnectivityCheckerTest extends BrambleMockTestCase {
|
||||
|
||||
private final Clock clock = context.mock(Clock.class);
|
||||
private final MailboxApiCaller mailboxApiCaller =
|
||||
context.mock(MailboxApiCaller.class);
|
||||
private final MailboxApi mailboxApi = context.mock(MailboxApi.class);
|
||||
private final TransactionManager db =
|
||||
context.mock(TransactionManager.class);
|
||||
private final MailboxSettingsManager mailboxSettingsManager =
|
||||
context.mock(MailboxSettingsManager.class);
|
||||
private final Cancellable task = context.mock(Cancellable.class);
|
||||
private final ConnectivityObserver observer =
|
||||
context.mock(ConnectivityObserver.class);
|
||||
|
||||
private final MailboxProperties properties =
|
||||
getMailboxProperties(true, CLIENT_SUPPORTS);
|
||||
private final long now = System.currentTimeMillis();
|
||||
|
||||
@Test
|
||||
public void testObserverIsCalledWhenCheckSucceeds() throws Exception {
|
||||
OwnMailboxConnectivityChecker checker = createChecker();
|
||||
AtomicReference<ApiCall> apiCall = new AtomicReference<>(null);
|
||||
Transaction txn = new Transaction(null, false);
|
||||
|
||||
// When checkConnectivity() is called a check should be started
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(mailboxApiCaller).retryWithBackoff(with(any(ApiCall.class)));
|
||||
will(new DoAllAction(
|
||||
new CaptureArgumentAction<>(apiCall, ApiCall.class, 0),
|
||||
returnValue(task)
|
||||
));
|
||||
}});
|
||||
|
||||
checker.checkConnectivity(properties, observer);
|
||||
|
||||
// When the check succeeds, the success should be recorded in the DB
|
||||
// and the observer should be called
|
||||
context.checking(new DbExpectations() {{
|
||||
oneOf(mailboxApi).checkStatus(properties);
|
||||
will(returnValue(true));
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn));
|
||||
oneOf(mailboxSettingsManager).recordSuccessfulConnection(txn, now);
|
||||
oneOf(observer).onConnectivityCheckSucceeded();
|
||||
}});
|
||||
|
||||
// The call should not be retried
|
||||
assertFalse(apiCall.get().callApi());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObserverIsNotCalledWhenCheckFails() throws Exception {
|
||||
OwnMailboxConnectivityChecker checker = createChecker();
|
||||
AtomicReference<ApiCall> apiCall = new AtomicReference<>(null);
|
||||
Transaction txn = new Transaction(null, false);
|
||||
|
||||
// When checkConnectivity() is called a check should be started
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(mailboxApiCaller).retryWithBackoff(with(any(ApiCall.class)));
|
||||
will(new DoAllAction(
|
||||
new CaptureArgumentAction<>(apiCall, ApiCall.class, 0),
|
||||
returnValue(task)
|
||||
));
|
||||
}});
|
||||
|
||||
checker.checkConnectivity(properties, observer);
|
||||
|
||||
// When the check fails, the failure should be recorded in the DB and
|
||||
// the observer should not be called
|
||||
context.checking(new DbExpectations() {{
|
||||
oneOf(mailboxApi).checkStatus(properties);
|
||||
will(throwException(new IOException()));
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(now));
|
||||
oneOf(db).transaction(with(false), withDbRunnable(txn));
|
||||
oneOf(mailboxSettingsManager)
|
||||
.recordFailedConnectionAttempt(txn, now);
|
||||
}});
|
||||
|
||||
// The call should be retried
|
||||
assertTrue(apiCall.get().callApi());
|
||||
}
|
||||
|
||||
private OwnMailboxConnectivityChecker createChecker() {
|
||||
return new OwnMailboxConnectivityChecker(clock, mailboxApiCaller,
|
||||
mailboxApi, db, mailboxSettingsManager);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user