mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Poll less frequently when own hidden service is stable.
This commit is contained in:
@@ -86,8 +86,8 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
|
||||
BluetoothConnectionFactory<BluetoothSocket> connectionFactory =
|
||||
new AndroidBluetoothConnectionFactory(connectionLimiter,
|
||||
wakeLockManager, timeoutMonitor);
|
||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
Backoff backoff = backoffFactory.createBackoff(eventBus, ID,
|
||||
MIN_POLLING_INTERVAL, MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
AndroidBluetoothPlugin plugin = new AndroidBluetoothPlugin(
|
||||
connectionLimiter, connectionFactory, ioExecutor,
|
||||
wakefulIoExecutor, secureRandom, androidExecutor, app,
|
||||
|
||||
@@ -61,8 +61,8 @@ public class AndroidLanTcpPluginFactory implements DuplexPluginFactory {
|
||||
|
||||
@Override
|
||||
public DuplexPlugin createPlugin(PluginCallback callback) {
|
||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
Backoff backoff = backoffFactory.createBackoff(eventBus, ID,
|
||||
MIN_POLLING_INTERVAL, MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
AndroidLanTcpPlugin plugin = new AndroidLanTcpPlugin(ioExecutor,
|
||||
wakefulIoExecutor, app, backoff, callback,
|
||||
MAX_LATENCY, MAX_IDLE_TIME, CONNECTION_TIMEOUT);
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package org.briarproject.bramble.api.plugin;
|
||||
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
|
||||
public interface BackoffFactory {
|
||||
|
||||
Backoff createBackoff(int minInterval, int maxInterval,
|
||||
double base);
|
||||
Backoff createBackoff(EventBus eventBus, TransportId transportId,
|
||||
int minInterval, int maxInterval, double base);
|
||||
}
|
||||
|
||||
@@ -56,4 +56,9 @@ public interface PluginCallback extends ConnectionHandler {
|
||||
* This method can safely be called while holding a lock.
|
||||
*/
|
||||
void pluginStateChanged(State state);
|
||||
|
||||
/**
|
||||
* Informs the callback that the plugin's polling interval has decreased.
|
||||
*/
|
||||
void pollingIntervalDecreased();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.briarproject.bramble.api.plugin.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a plugin's polling interval decreases.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class PollingIntervalDecreasedEvent extends Event {
|
||||
|
||||
private final TransportId transportId;
|
||||
|
||||
public PollingIntervalDecreasedEvent(TransportId transportId) {
|
||||
this.transportId = transportId;
|
||||
}
|
||||
|
||||
public TransportId getTransportId() {
|
||||
return transportId;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package org.briarproject.bramble.plugin;
|
||||
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Backoff;
|
||||
import org.briarproject.bramble.api.plugin.BackoffFactory;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@@ -11,8 +13,9 @@ import javax.annotation.concurrent.Immutable;
|
||||
class BackoffFactoryImpl implements BackoffFactory {
|
||||
|
||||
@Override
|
||||
public Backoff createBackoff(int minInterval, int maxInterval,
|
||||
double base) {
|
||||
return new BackoffImpl(minInterval, maxInterval, base);
|
||||
public Backoff createBackoff(EventBus eventBus, TransportId transportId,
|
||||
int minInterval, int maxInterval, double base) {
|
||||
return new BackoffImpl(eventBus, transportId, minInterval, maxInterval,
|
||||
base);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package org.briarproject.bramble.plugin;
|
||||
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Backoff;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.event.PollingIntervalDecreasedEvent;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@@ -11,11 +14,16 @@ import javax.annotation.concurrent.ThreadSafe;
|
||||
@NotNullByDefault
|
||||
class BackoffImpl implements Backoff {
|
||||
|
||||
private final EventBus eventBus;
|
||||
private final TransportId transportId;
|
||||
private final int minInterval, maxInterval;
|
||||
private final double base;
|
||||
private final AtomicInteger backoff;
|
||||
|
||||
BackoffImpl(int minInterval, int maxInterval, double base) {
|
||||
BackoffImpl(EventBus eventBus, TransportId transportId,
|
||||
int minInterval, int maxInterval, double base) {
|
||||
this.eventBus = eventBus;
|
||||
this.transportId = transportId;
|
||||
this.minInterval = minInterval;
|
||||
this.maxInterval = maxInterval;
|
||||
this.base = base;
|
||||
@@ -37,6 +45,9 @@ class BackoffImpl implements Backoff {
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
backoff.set(0);
|
||||
int old = backoff.getAndSet(0);
|
||||
if (old > 0) {
|
||||
eventBus.broadcast(new PollingIntervalDecreasedEvent(transportId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.plugin.event.PollingIntervalDecreasedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
|
||||
@@ -362,6 +363,11 @@ class PluginManagerImpl implements PluginManager, Service {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pollingIntervalDecreased() {
|
||||
eventBus.broadcast(new PollingIntervalDecreasedEvent(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleConnection(DuplexTransportConnection d) {
|
||||
connectionManager.manageIncomingConnection(id, d);
|
||||
|
||||
@@ -20,7 +20,7 @@ import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.PollingIntervalDecreasedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
||||
@@ -107,10 +107,14 @@ class PollerImpl implements Poller, EventListener {
|
||||
if (!c.isIncoming() && c.isException()) {
|
||||
connectToContact(c.getContactId(), c.getTransportId());
|
||||
}
|
||||
} else if (e instanceof ConnectionOpenedEvent) {
|
||||
ConnectionOpenedEvent c = (ConnectionOpenedEvent) e;
|
||||
// Reschedule polling, the polling interval may have decreased
|
||||
reschedule(c.getTransportId());
|
||||
} else if (e instanceof PollingIntervalDecreasedEvent) {
|
||||
PollingIntervalDecreasedEvent p = (PollingIntervalDecreasedEvent) e;
|
||||
TransportId t = p.getTransportId();
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Polling interval decreased for " + t);
|
||||
}
|
||||
// Reschedule polling
|
||||
reschedule(t);
|
||||
} else if (e instanceof TransportActiveEvent) {
|
||||
TransportActiveEvent t = (TransportActiveEvent) e;
|
||||
// Poll the newly activated transport
|
||||
@@ -228,7 +232,7 @@ class PollerImpl implements Poller, EventListener {
|
||||
if (!connected.contains(c))
|
||||
properties.add(new Pair<>(e.getValue(), new Handler(c, t)));
|
||||
}
|
||||
if (!properties.isEmpty()) p.poll(properties);
|
||||
p.poll(properties);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
|
||||
@@ -335,7 +335,7 @@ abstract class AbstractBluetoothPlugin<S, SS> implements BluetoothPlugin,
|
||||
@Override
|
||||
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||
properties) {
|
||||
if (getState() != ACTIVE) return;
|
||||
if (properties.isEmpty() || getState() != ACTIVE) return;
|
||||
backoff.increment();
|
||||
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
||||
connect(p.getFirst(), p.getSecond());
|
||||
|
||||
@@ -56,8 +56,8 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
|
||||
|
||||
@Override
|
||||
public DuplexPlugin createPlugin(PluginCallback callback) {
|
||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
Backoff backoff = backoffFactory.createBackoff(eventBus, ID,
|
||||
MIN_POLLING_INTERVAL, MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
LanTcpPlugin plugin = new LanTcpPlugin(ioExecutor, wakefulIoExecutor,
|
||||
backoff, callback, MAX_LATENCY, MAX_IDLE_TIME,
|
||||
CONNECTION_TIMEOUT);
|
||||
|
||||
@@ -246,7 +246,7 @@ abstract class TcpPlugin implements DuplexPlugin, EventListener {
|
||||
@Override
|
||||
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||
properties) {
|
||||
if (getState() != ACTIVE) return;
|
||||
if (properties.isEmpty() || getState() != ACTIVE) return;
|
||||
backoff.increment();
|
||||
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
||||
connect(p.getFirst(), p.getSecond());
|
||||
|
||||
@@ -60,8 +60,8 @@ public class WanTcpPluginFactory implements DuplexPluginFactory {
|
||||
|
||||
@Override
|
||||
public DuplexPlugin createPlugin(PluginCallback callback) {
|
||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
Backoff backoff = backoffFactory.createBackoff(eventBus, ID,
|
||||
MIN_POLLING_INTERVAL, MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
PortMapper portMapper = new PortMapperImpl(shutdownManager);
|
||||
WanTcpPlugin plugin = new WanTcpPlugin(ioExecutor, wakefulIoExecutor,
|
||||
backoff, portMapper, callback, MAX_LATENCY, MAX_IDLE_TIME,
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.briarproject.bramble.api.plugin.TorConstants;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
||||
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
||||
@@ -66,6 +67,7 @@ import javax.net.SocketFactory;
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
@@ -123,6 +125,26 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
private static final int COOKIE_POLLING_INTERVAL_MS = 200;
|
||||
private static final Pattern ONION_V3 = Pattern.compile("[a-z2-7]{56}");
|
||||
|
||||
/**
|
||||
* After this many successful connections to our own hidden service we
|
||||
* consider the network to be stable.
|
||||
*/
|
||||
private static final int STABILITY_THRESHOLD = 3;
|
||||
|
||||
/**
|
||||
* How often to poll our own hidden service when the network is considered
|
||||
* to be stable.
|
||||
*/
|
||||
private static final int POLLING_INTERVAL_STABLE =
|
||||
(int) MINUTES.toMillis(5);
|
||||
|
||||
/**
|
||||
* How often to poll our own hidden service and our contacts' hidden
|
||||
* services when the network is considered to be unstable.
|
||||
*/
|
||||
private static final int POLLING_INTERVAL_UNSTABLE =
|
||||
(int) MINUTES.toMillis(2);
|
||||
|
||||
protected final Executor ioExecutor;
|
||||
private final Executor wakefulIoExecutor;
|
||||
private final Executor connectionStatusExecutor;
|
||||
@@ -150,6 +172,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
private volatile Socket controlSocket = null;
|
||||
private volatile TorControlConnection controlConnection = null;
|
||||
private volatile Settings settings = null;
|
||||
private volatile String ownOnion = null;
|
||||
|
||||
protected abstract int getProcessId();
|
||||
|
||||
@@ -508,6 +531,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
return;
|
||||
}
|
||||
String onion3 = response.get(HS_ADDRESS);
|
||||
ownOnion = onion3;
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("V3 hidden service " + scrubOnion(onion3));
|
||||
}
|
||||
@@ -612,13 +636,58 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
|
||||
@Override
|
||||
public int getPollingInterval() {
|
||||
return 120_000; // FIXME
|
||||
if (state.isNetworkStable()) {
|
||||
LOG.info("Using stable polling interval");
|
||||
return POLLING_INTERVAL_STABLE;
|
||||
} else {
|
||||
LOG.info("Using unstable polling interval");
|
||||
return POLLING_INTERVAL_UNSTABLE;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||
properties) {
|
||||
if (getState() != ACTIVE) return;
|
||||
String ownOnion = this.ownOnion;
|
||||
if (ownOnion == null) {
|
||||
// Our own hidden service hasn't been created yet
|
||||
pollContacts(properties);
|
||||
} else {
|
||||
// If the network is unstable, poll our contacts
|
||||
boolean stable = state.isNetworkStable();
|
||||
if (!stable) pollContacts(properties);
|
||||
// Poll our own hidden service to check if the network is stable
|
||||
wakefulIoExecutor.execute(() -> {
|
||||
LOG.info("Connecting to own hidden service");
|
||||
TransportProperties p = new TransportProperties();
|
||||
p.put(PROP_ONION_V3, ownOnion);
|
||||
DuplexTransportConnection d = createConnection(p);
|
||||
if (d == null) {
|
||||
LOG.info("Could not connect to own hidden service");
|
||||
state.resetNetworkStability();
|
||||
// If the network was previously considered stable then
|
||||
// we didn't poll our contacts above, so poll them now
|
||||
if (stable) pollContacts(properties);
|
||||
} else {
|
||||
LOG.info("Connected to own hidden service");
|
||||
// Close the connection (this will cause the other end of
|
||||
// the connection to log an EOFException)
|
||||
try {
|
||||
d.getWriter().dispose(false);
|
||||
d.getReader().dispose(false, false);
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
state.incrementNetworkStability();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void pollContacts(
|
||||
Collection<Pair<TransportProperties, ConnectionHandler>> properties) {
|
||||
if (properties.isEmpty() || getState() != ACTIVE) return;
|
||||
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
||||
connect(p.getFirst(), p.getSecond());
|
||||
}
|
||||
@@ -776,6 +845,9 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
@Override
|
||||
public void message(String severity, String msg) {
|
||||
if (LOG.isLoggable(INFO)) LOG.info(severity + " " + msg);
|
||||
if (msg.startsWith("Switching to guard context")) {
|
||||
state.resetNetworkStability();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -848,7 +920,15 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof SettingsUpdatedEvent) {
|
||||
if (e instanceof ConnectionClosedEvent) {
|
||||
ConnectionClosedEvent c = (ConnectionClosedEvent) e;
|
||||
if (c.getTransportId().equals(getId())
|
||||
&& !c.isIncoming() && c.isException()) {
|
||||
LOG.info("Outgoing connection closed with exception");
|
||||
// The failure may indicate that the network is unstable
|
||||
state.resetNetworkStability();
|
||||
}
|
||||
} else if (e instanceof SettingsUpdatedEvent) {
|
||||
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
|
||||
if (s.getNamespace().equals(ID.getString())) {
|
||||
LOG.info("Tor settings updated");
|
||||
@@ -991,7 +1071,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
settingsChecked = false;
|
||||
|
||||
@GuardedBy("this")
|
||||
private int reasonsDisabled = 0;
|
||||
private int reasonsDisabled = 0, networkStability = 0;
|
||||
|
||||
@GuardedBy("this")
|
||||
@Nullable
|
||||
@@ -1087,6 +1167,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
}
|
||||
logOrConnections();
|
||||
if (orConnectionsConnected == 0 && oldConnected != 0) {
|
||||
resetNetworkStability();
|
||||
callback.pluginStateChanged(getState());
|
||||
}
|
||||
}
|
||||
@@ -1097,5 +1178,31 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
LOG.info(orConnectionsConnected + " OR connections connected");
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized boolean isNetworkStable() {
|
||||
return networkStability >= STABILITY_THRESHOLD;
|
||||
}
|
||||
|
||||
private synchronized void incrementNetworkStability() {
|
||||
networkStability++;
|
||||
logNetworkStability();
|
||||
}
|
||||
|
||||
private synchronized void resetNetworkStability() {
|
||||
int old = networkStability;
|
||||
networkStability = 0;
|
||||
logNetworkStability();
|
||||
if (old >= STABILITY_THRESHOLD) {
|
||||
callback.pollingIntervalDecreased();
|
||||
}
|
||||
}
|
||||
|
||||
@GuardedBy("this")
|
||||
private void logNetworkStability() {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info(networkStability
|
||||
+ " successful connections to own hidden service");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,69 @@
|
||||
package org.briarproject.bramble.plugin;
|
||||
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.event.PollingIntervalDecreasedEvent;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class BackoffImplTest extends BrambleTestCase {
|
||||
public class BackoffImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final EventBus eventBus = context.mock(EventBus.class);
|
||||
|
||||
private final TransportId transportId = getTransportId();
|
||||
private static final int MIN_INTERVAL = 60 * 1000;
|
||||
private static final int MAX_INTERVAL = 60 * 60 * 1000;
|
||||
private static final double BASE = 1.2;
|
||||
|
||||
@Test
|
||||
public void testPollingIntervalStartsAtMinimum() {
|
||||
BackoffImpl b = new BackoffImpl(MIN_INTERVAL, MAX_INTERVAL, BASE);
|
||||
BackoffImpl b = new BackoffImpl(eventBus, transportId,
|
||||
MIN_INTERVAL, MAX_INTERVAL, BASE);
|
||||
assertEquals(MIN_INTERVAL, b.getPollingInterval());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncrementIncreasesPollingInterval() {
|
||||
BackoffImpl b = new BackoffImpl(MIN_INTERVAL, MAX_INTERVAL, BASE);
|
||||
public void testIncrementMethodIncreasesPollingInterval() {
|
||||
BackoffImpl b = new BackoffImpl(eventBus, transportId,
|
||||
MIN_INTERVAL, MAX_INTERVAL, BASE);
|
||||
b.increment();
|
||||
assertTrue(b.getPollingInterval() > MIN_INTERVAL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResetResetsPollingInterval() {
|
||||
BackoffImpl b = new BackoffImpl(MIN_INTERVAL, MAX_INTERVAL, BASE);
|
||||
b.increment();
|
||||
public void testResetMethodResetsPollingInterval() {
|
||||
BackoffImpl b = new BackoffImpl(eventBus, transportId,
|
||||
MIN_INTERVAL, MAX_INTERVAL, BASE);
|
||||
b.increment();
|
||||
assertTrue(b.getPollingInterval() > MIN_INTERVAL);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(eventBus).broadcast(with(any(
|
||||
PollingIntervalDecreasedEvent.class)));
|
||||
}});
|
||||
|
||||
b.reset();
|
||||
assertEquals(MIN_INTERVAL, b.getPollingInterval());
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Resetting again should not broadcast another event
|
||||
b.reset();
|
||||
assertEquals(MIN_INTERVAL, b.getPollingInterval());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseAffectsBackoffSpeed() {
|
||||
BackoffImpl b = new BackoffImpl(MIN_INTERVAL, MAX_INTERVAL, BASE);
|
||||
BackoffImpl b = new BackoffImpl(eventBus, transportId,
|
||||
MIN_INTERVAL, MAX_INTERVAL, BASE);
|
||||
b.increment();
|
||||
int interval = b.getPollingInterval();
|
||||
BackoffImpl b1 = new BackoffImpl(MIN_INTERVAL, MAX_INTERVAL, BASE * 2);
|
||||
BackoffImpl b1 = new BackoffImpl(eventBus, transportId, MIN_INTERVAL,
|
||||
MAX_INTERVAL, BASE * 2);
|
||||
b1.increment();
|
||||
int interval1 = b1.getPollingInterval();
|
||||
assertTrue(interval < interval1);
|
||||
@@ -47,15 +71,16 @@ public class BackoffImplTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testIntervalDoesNotExceedMaxInterval() {
|
||||
BackoffImpl b = new BackoffImpl(MIN_INTERVAL, MAX_INTERVAL, BASE);
|
||||
BackoffImpl b = new BackoffImpl(eventBus, transportId,
|
||||
MIN_INTERVAL, MAX_INTERVAL, BASE);
|
||||
for (int i = 0; i < 100; i++) b.increment();
|
||||
assertEquals(MAX_INTERVAL, b.getPollingInterval());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntervalDoesNotExceedMaxIntervalWithInfiniteMultiplier() {
|
||||
BackoffImpl b = new BackoffImpl(MIN_INTERVAL, MAX_INTERVAL,
|
||||
Double.POSITIVE_INFINITY);
|
||||
BackoffImpl b = new BackoffImpl(eventBus, transportId,
|
||||
MIN_INTERVAL, MAX_INTERVAL, Double.POSITIVE_INFINITY);
|
||||
b.increment();
|
||||
assertEquals(MAX_INTERVAL, b.getPollingInterval());
|
||||
}
|
||||
|
||||
@@ -342,6 +342,10 @@ public class LanTcpPluginTest extends BrambleTestCase {
|
||||
public void pluginStateChanged(State newState) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pollingIntervalDecreased() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleConnection(DuplexTransportConnection d) {
|
||||
connectionsLatch.countDown();
|
||||
|
||||
@@ -69,8 +69,8 @@ public class JavaBluetoothPluginFactory implements DuplexPluginFactory {
|
||||
BluetoothConnectionFactory<StreamConnection> connectionFactory =
|
||||
new JavaBluetoothConnectionFactory(connectionLimiter,
|
||||
timeoutMonitor);
|
||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
Backoff backoff = backoffFactory.createBackoff(eventBus, ID,
|
||||
MIN_POLLING_INTERVAL, MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
JavaBluetoothPlugin plugin = new JavaBluetoothPlugin(connectionLimiter,
|
||||
connectionFactory, ioExecutor, wakefulIoExecutor, secureRandom,
|
||||
backoff, callback, MAX_LATENCY, MAX_IDLE_TIME);
|
||||
|
||||
@@ -43,6 +43,10 @@ public class TestPluginCallback implements PluginCallback {
|
||||
public void pluginStateChanged(State state) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pollingIntervalDecreased() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleConnection(DuplexTransportConnection c) {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user