Unverified Commit bd430081 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Make initial daemon devices population fast (#56959)

parent f66d3c5a
...@@ -297,19 +297,21 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery { ...@@ -297,19 +297,21 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
void startPolling() { void startPolling() {
if (_timer == null) { if (_timer == null) {
_items ??= ItemListNotifier<Device>(); _items ??= ItemListNotifier<Device>();
_timer = _initTimer(); // Make initial population the default, fast polling timeout.
_timer = _initTimer(null);
} }
} }
Timer _initTimer() { Timer _initTimer(Duration pollingTimeout) {
return Timer(_pollingInterval, () async { return Timer(_pollingInterval, () async {
try { try {
final List<Device> devices = await pollingGetDevices(timeout: _pollingTimeout); final List<Device> devices = await pollingGetDevices(timeout: pollingTimeout);
_items.updateWithNewList(devices); _items.updateWithNewList(devices);
} on TimeoutException { } on TimeoutException {
globals.printTrace('Device poll timed out. Will retry.'); globals.printTrace('Device poll timed out. Will retry.');
} }
_timer = _initTimer(); // Subsequent timeouts after initial population should wait longer.
_timer = _initTimer(_pollingTimeout);
}); });
} }
......
...@@ -194,7 +194,7 @@ void main() { ...@@ -194,7 +194,7 @@ void main() {
responses.add, responses.add,
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
); );
final MockPollingDeviceDiscovery discoverer = MockPollingDeviceDiscovery(); final FakePollingDeviceDiscovery discoverer = FakePollingDeviceDiscovery();
daemon.deviceDomain.addDeviceDiscoverer(discoverer); daemon.deviceDomain.addDeviceDiscoverer(discoverer);
discoverer.addDevice(MockAndroidDevice()); discoverer.addDevice(MockAndroidDevice());
commands.add(<String, dynamic>{'id': 0, 'method': 'device.getDevices'}); commands.add(<String, dynamic>{'id': 0, 'method': 'device.getDevices'});
...@@ -216,7 +216,7 @@ void main() { ...@@ -216,7 +216,7 @@ void main() {
notifyingLogger: notifyingLogger, notifyingLogger: notifyingLogger,
); );
final MockPollingDeviceDiscovery discoverer = MockPollingDeviceDiscovery(); final FakePollingDeviceDiscovery discoverer = FakePollingDeviceDiscovery();
daemon.deviceDomain.addDeviceDiscoverer(discoverer); daemon.deviceDomain.addDeviceDiscoverer(discoverer);
discoverer.addDevice(MockAndroidDevice()); discoverer.addDevice(MockAndroidDevice());
......
...@@ -9,6 +9,7 @@ import 'package:flutter_tools/src/device.dart'; ...@@ -9,6 +9,7 @@ import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/project.dart'; import 'package:flutter_tools/src/project.dart';
import 'package:mockito/mockito.dart'; import 'package:mockito/mockito.dart';
import 'package:quiver/testing/async.dart';
import '../src/common.dart'; import '../src/common.dart';
import '../src/context.dart'; import '../src/context.dart';
...@@ -63,6 +64,26 @@ void main() { ...@@ -63,6 +64,26 @@ void main() {
}); });
}); });
group('PollingDeviceDiscovery', () {
testUsingContext('startPolling', () async {
FakeAsync().run((FakeAsync time) {
final FakePollingDeviceDiscovery pollingDeviceDiscovery = FakePollingDeviceDiscovery();
pollingDeviceDiscovery.startPolling();
time.elapse(const Duration(milliseconds: 4001));
time.flushMicrotasks();
// First check should use the default polling timeout
// to quickly populate the list.
expect(pollingDeviceDiscovery.lastPollingTimeout, isNull);
time.elapse(const Duration(milliseconds: 4001));
time.flushMicrotasks();
// Subsequent polling should be much longer.
expect(pollingDeviceDiscovery.lastPollingTimeout, const Duration(seconds: 30));
pollingDeviceDiscovery.stopPolling();
});
});
});
group('Filter devices', () { group('Filter devices', () {
FakeDevice ephemeral; FakeDevice ephemeral;
FakeDevice nonEphemeralOne; FakeDevice nonEphemeralOne;
...@@ -198,12 +219,12 @@ void main() { ...@@ -198,12 +219,12 @@ void main() {
class TestDeviceManager extends DeviceManager { class TestDeviceManager extends DeviceManager {
TestDeviceManager(List<Device> allDevices) { TestDeviceManager(List<Device> allDevices) {
_deviceDiscoverer = MockPollingDeviceDiscovery(); _deviceDiscoverer = FakePollingDeviceDiscovery();
resetDevices(allDevices); resetDevices(allDevices);
} }
@override @override
List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[_deviceDiscoverer]; List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[_deviceDiscoverer];
MockPollingDeviceDiscovery _deviceDiscoverer; FakePollingDeviceDiscovery _deviceDiscoverer;
void resetDevices(List<Device> allDevices) { void resetDevices(List<Device> allDevices) {
_deviceDiscoverer.setDevices(allDevices); _deviceDiscoverer.setDevices(allDevices);
......
...@@ -482,8 +482,8 @@ class MockStdio extends Stdio { ...@@ -482,8 +482,8 @@ class MockStdio extends Stdio {
List<String> get writtenToStderr => _stderr.writes.map<String>(_stderr.encoding.decode).toList(); List<String> get writtenToStderr => _stderr.writes.map<String>(_stderr.encoding.decode).toList();
} }
class MockPollingDeviceDiscovery extends PollingDeviceDiscovery { class FakePollingDeviceDiscovery extends PollingDeviceDiscovery {
MockPollingDeviceDiscovery() : super('mock'); FakePollingDeviceDiscovery() : super('mock');
final List<Device> _devices = <Device>[]; final List<Device> _devices = <Device>[];
final StreamController<Device> _onAddedController = StreamController<Device>.broadcast(); final StreamController<Device> _onAddedController = StreamController<Device>.broadcast();
......
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