Clean up PollingDeviceDiscovery dispose (#59709)

This commit is contained in:
Jenn Magder 2020-06-18 16:38:02 -07:00 committed by GitHub
parent 339f0363a6
commit f41f795640
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 14 deletions

View File

@ -179,10 +179,10 @@ class Daemon {
void _send(Map<String, dynamic> map) => sendCommand(map);
void shutdown({ dynamic error }) {
_commandSubscription?.cancel();
Future<void> shutdown({ dynamic error }) async {
await _commandSubscription?.cancel();
for (final Domain domain in _domainMap.values) {
domain.dispose();
await domain.dispose();
}
if (!_onExitCompleter.isCompleted) {
if (error == null) {
@ -273,7 +273,7 @@ abstract class Domain {
return val as int;
}
void dispose() { }
Future<void> dispose() async { }
}
/// This domain responds to methods like [version] and [shutdown].
@ -351,8 +351,8 @@ class DaemonDomain extends Domain {
}
@override
void dispose() {
_subscription?.cancel();
Future<void> dispose() async {
await _subscription?.cancel();
}
/// Enumerates the platforms supported by the provided project.
@ -828,9 +828,9 @@ class DeviceDomain extends Domain {
}
@override
void dispose() {
Future<void> dispose() async {
for (final PollingDeviceDiscovery discoverer in _discoverers) {
discoverer.dispose();
await discoverer.dispose();
}
}

View File

@ -352,7 +352,7 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
return deviceNotifier.onRemoved;
}
void dispose() => stopPolling();
Future<void> dispose() async => await stopPolling();
@override
String toString() => '$name device discovery';

View File

@ -43,11 +43,6 @@ class IOSDevices extends PollingDeviceDiscovery {
_logger = logger ?? globals.logger,
super('iOS devices');
@override
void dispose() {
_observedDeviceEventsSubscription?.cancel();
}
final Platform _platform;
final XCDevice _xcdevice;
final IOSWorkflow _iosWorkflow;

View File

@ -482,6 +482,28 @@ void main() {
expect(rescheduledStream.hasListener, isFalse);
});
testWithoutContext('dispose cancels polling subscription', () async {
final IOSDevices iosDevices = IOSDevices(
platform: macPlatform,
xcdevice: mockXcdevice,
iosWorkflow: mockIosWorkflow,
logger: logger,
);
when(mockXcdevice.isInstalled).thenReturn(true);
when(mockXcdevice.getAvailableIOSDevices())
.thenAnswer((Invocation invocation) => Future<List<IOSDevice>>.value(<IOSDevice>[]));
final StreamController<Map<XCDeviceEvent, String>> eventStream = StreamController<Map<XCDeviceEvent, String>>();
when(mockXcdevice.observedDeviceEvents()).thenAnswer((_) => eventStream.stream);
await iosDevices.startPolling();
expect(iosDevices.deviceNotifier.items, isEmpty);
expect(eventStream.hasListener, isTrue);
await iosDevices.dispose();
expect(eventStream.hasListener, isFalse);
});
final List<Platform> unsupportedPlatforms = <Platform>[linuxPlatform, windowsPlatform];
for (final Platform unsupportedPlatform in unsupportedPlatforms) {
testWithoutContext('pollingGetDevices throws Unsupported Operation exception on ${unsupportedPlatform.operatingSystem}', () async {