mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
New polling logic for Bluetooth. #251
The polling interval increases exponentially each time polling is unsuccessful, up to a maximum of 60 minutes. The interval is reset to 2 minutes whenever a connection is made and whenever Bluetooth is re-enabled.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package org.briarproject.plugins;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class BackoffImplTest extends BriarTestCase {
|
||||
|
||||
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);
|
||||
assertEquals(MIN_INTERVAL, b.getPollingInterval());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncrementIncreasesPollingInterval() {
|
||||
BackoffImpl b = new BackoffImpl(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();
|
||||
b.increment();
|
||||
b.reset();
|
||||
assertEquals(MIN_INTERVAL, b.getPollingInterval());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseAffectsBackoffSpeed() {
|
||||
BackoffImpl b = new BackoffImpl(MIN_INTERVAL, MAX_INTERVAL, BASE);
|
||||
b.increment();
|
||||
int interval = b.getPollingInterval();
|
||||
BackoffImpl b1 = new BackoffImpl(MIN_INTERVAL, MAX_INTERVAL, BASE * 2);
|
||||
b1.increment();
|
||||
int interval1 = b1.getPollingInterval();
|
||||
assertTrue(interval < interval1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntervalDoesNotExceedMaxInterval() {
|
||||
BackoffImpl b = new BackoffImpl(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);
|
||||
b.increment();
|
||||
assertEquals(MAX_INTERVAL, b.getPollingInterval());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
package org.briarproject.plugins.bluetooth;
|
||||
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.properties.TransportProperties;
|
||||
import org.briarproject.api.settings.Settings;
|
||||
import org.briarproject.plugins.DuplexClientTest;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
// This is not a JUnit test - it has to be run manually while the server test
|
||||
// is running on another machine
|
||||
public class BluetoothClientTest extends DuplexClientTest {
|
||||
|
||||
private BluetoothClientTest(Executor executor, String serverAddress) {
|
||||
// Store the server's Bluetooth address and UUID
|
||||
TransportProperties p = new TransportProperties();
|
||||
p.put("address", serverAddress);
|
||||
p.put("uuid", BluetoothTest.EMPTY_UUID);
|
||||
Map<ContactId, TransportProperties> remote =
|
||||
Collections.singletonMap(contactId, p);
|
||||
// Create the plugin
|
||||
callback = new ClientCallback(new Settings(),
|
||||
new TransportProperties(), remote);
|
||||
plugin = new BluetoothPlugin(executor, new SystemClock(),
|
||||
new SecureRandom(), callback, 0, 0);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length != 1) {
|
||||
System.err.println("Please specify the server's Bluetooth address");
|
||||
System.exit(1);
|
||||
}
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
try {
|
||||
new BluetoothClientTest(executor, args[0]).run();
|
||||
} finally {
|
||||
executor.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package org.briarproject.plugins.bluetooth;
|
||||
|
||||
import org.briarproject.api.properties.TransportProperties;
|
||||
import org.briarproject.api.settings.Settings;
|
||||
import org.briarproject.plugins.DuplexServerTest;
|
||||
import org.briarproject.system.SystemClock;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
// This is not a JUnit test - it has to be run manually while the client test
|
||||
// is running on another machine
|
||||
public class BluetoothServerTest extends DuplexServerTest {
|
||||
|
||||
private BluetoothServerTest(Executor executor) {
|
||||
// Store the UUID
|
||||
TransportProperties local = new TransportProperties();
|
||||
local.put("uuid", BluetoothTest.EMPTY_UUID);
|
||||
// Create the plugin
|
||||
callback = new ServerCallback(new Settings(), local,
|
||||
Collections.singletonMap(contactId, new TransportProperties()));
|
||||
plugin = new BluetoothPlugin(executor, new SystemClock(),
|
||||
new SecureRandom(), callback, 0, 0);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
try {
|
||||
new BluetoothServerTest(executor).run();
|
||||
} finally {
|
||||
executor.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package org.briarproject.plugins.bluetooth;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
class BluetoothTest {
|
||||
|
||||
static final String EMPTY_UUID =
|
||||
UUID.nameUUIDFromBytes(new byte[0]).toString();
|
||||
}
|
||||
Reference in New Issue
Block a user