Adding a shutdown hook to HotRunnerConfig (#26604)
This commit is contained in:
committed by
Mehmet Fidanboylu
parent
33bfa6a7c5
commit
3c0a74ef0f
@@ -36,6 +36,11 @@ class HotRunnerConfig {
|
||||
Future<bool> setupHotRestart() async {
|
||||
return true;
|
||||
}
|
||||
/// A hook for implementations to perform any necessary operations right
|
||||
/// before the runner is about to be shut down.
|
||||
Future<void> runPreShutdownOperations() async {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
HotRunnerConfig get hotRunnerConfig => context[HotRunnerConfig];
|
||||
@@ -851,6 +856,7 @@ class HotRunner extends ResidentRunner {
|
||||
@override
|
||||
Future<void> cleanupAfterSignal() async {
|
||||
await stopEchoingDeviceLog();
|
||||
await hotRunnerConfig.runPreShutdownOperations();
|
||||
if (_didAttach) {
|
||||
appFinished();
|
||||
} else {
|
||||
@@ -859,7 +865,10 @@ class HotRunner extends ResidentRunner {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> preStop() => _cleanupDevFS();
|
||||
Future<void> preStop() async {
|
||||
await _cleanupDevFS();
|
||||
await hotRunnerConfig.runPreShutdownOperations();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> cleanupAtFinish() async {
|
||||
|
||||
@@ -228,6 +228,47 @@ void main() {
|
||||
Artifacts: () => mockArtifacts,
|
||||
HotRunnerConfig: () => TestHotRunnerConfig(successfulSetup: true, computeDartDependencies: false),
|
||||
});
|
||||
|
||||
group('shutdown hook tests', () {
|
||||
TestHotRunnerConfig shutdownTestingConfig;
|
||||
|
||||
setUp(() {
|
||||
shutdownTestingConfig = TestHotRunnerConfig(
|
||||
successfulSetup: true,
|
||||
computeDartDependencies: false,
|
||||
);
|
||||
});
|
||||
|
||||
testUsingContext('shutdown hook called after signal', () async {
|
||||
final MockDevice mockDevice = MockDevice();
|
||||
when(mockDevice.supportsHotReload).thenReturn(true);
|
||||
when(mockDevice.supportsHotRestart).thenReturn(true);
|
||||
when(mockDevice.supportsStopApp).thenReturn(false);
|
||||
final List<FlutterDevice> devices = <FlutterDevice>[
|
||||
FlutterDevice(mockDevice, generator: residentCompiler, trackWidgetCreation: false)
|
||||
];
|
||||
await HotRunner(devices).cleanupAfterSignal();
|
||||
expect(shutdownTestingConfig.shutdownHookCalled, true);
|
||||
}, overrides: <Type, Generator> {
|
||||
Artifacts: () => mockArtifacts,
|
||||
HotRunnerConfig: () => shutdownTestingConfig,
|
||||
});
|
||||
|
||||
testUsingContext('shutdown hook called after app stop', () async {
|
||||
final MockDevice mockDevice = MockDevice();
|
||||
when(mockDevice.supportsHotReload).thenReturn(true);
|
||||
when(mockDevice.supportsHotRestart).thenReturn(true);
|
||||
when(mockDevice.supportsStopApp).thenReturn(false);
|
||||
final List<FlutterDevice> devices = <FlutterDevice>[
|
||||
FlutterDevice(mockDevice, generator: residentCompiler, trackWidgetCreation: false)
|
||||
];
|
||||
await HotRunner(devices).preStop();
|
||||
expect(shutdownTestingConfig.shutdownHookCalled, true);
|
||||
}, overrides: <Type, Generator> {
|
||||
Artifacts: () => mockArtifacts,
|
||||
HotRunnerConfig: () => shutdownTestingConfig,
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -247,9 +288,15 @@ class TestHotRunnerConfig extends HotRunnerConfig {
|
||||
}
|
||||
|
||||
bool successfulSetup;
|
||||
bool shutdownHookCalled = false;
|
||||
|
||||
@override
|
||||
Future<bool> setupHotRestart() async {
|
||||
return successfulSetup;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> runPreShutdownOperations() async {
|
||||
shutdownHookCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user