Commit 344cfac8 authored by akwizgran's avatar akwizgran

Add test for Bluetooth discovery.

parent 56c7ef02
......@@ -22,6 +22,7 @@ import static org.briarproject.interferometer.MainViewModel.StartResult.INVALID_
import static org.briarproject.interferometer.MainViewModel.StartResult.NO_BLUETOOTH;
import static org.briarproject.interferometer.MainViewModel.TestPhase.BASELINE;
import static org.briarproject.interferometer.MainViewModel.TestPhase.BLUETOOTH;
import static org.briarproject.interferometer.MainViewModel.TestPhase.DISCOVERY;
import static org.briarproject.interferometer.MainViewModel.TestPhase.READY;
public class MainActivity extends AppCompatActivity {
......@@ -78,6 +79,9 @@ public class MainActivity extends AppCompatActivity {
} else if (phase == BLUETOOTH) {
testButton.setText(R.string.stop_test);
testPhaseTextView.setText(R.string.testing_bluetooth);
} else if (phase == DISCOVERY) {
testButton.setText(R.string.stop_test);
testPhaseTextView.setText(R.string.testing_discovery);
}
}
......
......@@ -28,6 +28,7 @@ import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import static android.bluetooth.BluetoothAdapter.ACTION_DISCOVERY_FINISHED;
import static android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED;
import static android.bluetooth.BluetoothAdapter.EXTRA_STATE;
import static android.bluetooth.BluetoothAdapter.STATE_OFF;
......@@ -41,12 +42,13 @@ import static org.briarproject.interferometer.MainViewModel.StartResult.NO_BLUET
import static org.briarproject.interferometer.MainViewModel.StartResult.STARTED;
import static org.briarproject.interferometer.MainViewModel.TestPhase.BASELINE;
import static org.briarproject.interferometer.MainViewModel.TestPhase.BLUETOOTH;
import static org.briarproject.interferometer.MainViewModel.TestPhase.DISCOVERY;
import static org.briarproject.interferometer.MainViewModel.TestPhase.READY;
import static org.briarproject.interferometer.Statistics.mean;
public class MainViewModel extends AndroidViewModel {
enum TestPhase {READY, BASELINE, BLUETOOTH}
enum TestPhase {READY, BASELINE, BLUETOOTH, DISCOVERY}
enum ConnectionState {DISCONNECTED, DOWNLOADING, UPLOADING}
......@@ -116,9 +118,18 @@ public class MainViewModel extends AndroidViewModel {
List<Long> uploadBt = new ArrayList<>();
if (!testThroughput(hostPort, downloadBt, uploadBt)) return;
Future<?> discoveryTask = startDiscovery(adapter);
try {
testPhase.postValue(DISCOVERY);
Log.i(TAG, "Testing throughput with Bluetooth enabled");
List<Long> downloadDiscovery = new ArrayList<>();
List<Long> uploadDiscovery = new ArrayList<>();
if (!testThroughput(hostPort, downloadDiscovery, uploadDiscovery)) return;
} finally {
discoveryTask.cancel(true);
}
Log.i(TAG, "Test finished");
} catch (InterruptedException e) {
Log.w(TAG, "Interrupted");
} finally {
testPhase.postValue(READY);
}
......@@ -159,7 +170,7 @@ public class MainViewModel extends AndroidViewModel {
}
}
private boolean disableBluetooth(BluetoothAdapter adapter) throws InterruptedException {
private boolean disableBluetooth(BluetoothAdapter adapter) {
if (!adapter.isEnabled()) return true;
BluetoothStateReceiver receiver = new BluetoothStateReceiver(STATE_OFF);
Application app = getApplication();
......@@ -168,12 +179,15 @@ public class MainViewModel extends AndroidViewModel {
if (!adapter.disable()) return false;
if (!adapter.isEnabled()) return true;
return receiver.waitForTargetState();
} catch (InterruptedException e) {
Log.w(TAG, "Interrupted while disabling Bluetooth");
return false;
} finally {
app.unregisterReceiver(receiver);
}
}
private boolean enableBluetooth(BluetoothAdapter adapter) throws InterruptedException {
private boolean enableBluetooth(BluetoothAdapter adapter) {
if (adapter.isEnabled()) return true;
BluetoothStateReceiver receiver = new BluetoothStateReceiver(STATE_ON);
Application app = getApplication();
......@@ -182,11 +196,39 @@ public class MainViewModel extends AndroidViewModel {
if (!adapter.enable()) return false;
if (adapter.isEnabled()) return true;
return receiver.waitForTargetState();
} catch (InterruptedException e) {
Log.w(TAG, "Interrupted while enabling Bluetooth");
return false;
} finally {
app.unregisterReceiver(receiver);
}
}
private Future<?> startDiscovery(BluetoothAdapter adapter) {
Application app = getApplication();
return executorService.submit(() -> {
while (!Thread.currentThread().isInterrupted()) {
BluetoothDiscoveryReceiver receiver = new BluetoothDiscoveryReceiver();
app.registerReceiver(receiver, new IntentFilter(ACTION_DISCOVERY_FINISHED));
try {
Log.i(TAG, "Starting discovery");
adapter.startDiscovery();
if (receiver.waitForDiscoveryToFinish()) {
Log.i(TAG, "Discovery finished");
} else {
Log.w(TAG, "Discovery did not finish within timeout");
}
} catch (InterruptedException e) {
Log.w(TAG, "Discovery interrupted");
return;
} finally {
app.unregisterReceiver(receiver);
adapter.cancelDiscovery();
}
}
});
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean testThroughput(HostPort hostPort, List<Long> downloadThroughput,
List<Long> uploadThroughput) {
......@@ -367,4 +409,21 @@ public class MainViewModel extends AndroidViewModel {
return latch.await(TIMEOUT, MILLISECONDS);
}
}
private static class BluetoothDiscoveryReceiver extends BroadcastReceiver {
private static final int TIMEOUT = 60_000;
private final CountDownLatch latch = new CountDownLatch(1);
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Discovery finished");
latch.countDown();
}
private boolean waitForDiscoveryToFinish() throws InterruptedException {
return latch.await(TIMEOUT, MILLISECONDS);
}
}
}
......@@ -11,6 +11,7 @@
<string name="testing_baseline">Testing with Bluetooth disabled</string>
<string name="testing_bluetooth">Testing with Bluetooth enabled</string>
<string name="testing_discovery">Testing with Bluetooth discovery</string>
<string name="download_format">Download: %,d bytes/sec</string>
<string name="upload_format">Upload: %,d bytes/sec</string>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment