@@ -325,13 +325,15 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
325325
326326 final StringBuffer buffer = StringBuffer ();
327327 buffer.writeln ('''
328+ import 'dart:async';
328329import 'dart:ffi';
329330import 'dart:isolate';
330331import 'dart:ui';
331332
332333import 'package:ffi/ffi.dart';
333334import 'package:flutter_test/flutter_test.dart';
334335import 'package:stream_channel/isolate_channel.dart';
336+ import 'package:stream_channel/stream_channel.dart';
335337import 'package:test_api/backend.dart'; // flutter_ignore: test_api_import
336338''' );
337339
@@ -340,6 +342,7 @@ import 'package:test_api/backend.dart'; // flutter_ignore: test_api_import
340342 return path
341343 .replaceAll ('.' , '_' )
342344 .replaceAll (':' , '_' )
345+ .replaceAll ('-' , '_' )
343346 .replaceAll ('/' , '_' )
344347 .replaceAll (r'\' , '_' )
345348 .replaceRange (path.length - '.dart' .length, null , '' );
@@ -390,27 +393,59 @@ import 'package:test_api/backend.dart'; // flutter_ignore: test_api_import
390393@Native<Void Function(Pointer<Utf8>, Pointer<Utf8>)>(symbol: 'Spawn')
391394external void _spawn(Pointer<Utf8> entrypoint, Pointer<Utf8> route);
392395
393- void spawn({required SendPort port, String entrypoint = 'main', String route = '/'}) {
396+ void spawn({
397+ required SendPort portForPackageTestCommunication,
398+ required SendPort portForShutdownNotification,
399+ String entrypoint = 'main',
400+ String route = '/',
401+ }) {
394402 assert(
395403 entrypoint != 'main' || route != '/',
396404 'Spawn should not be used to spawn main with the default route name',
397405 );
398- IsolateNameServer.registerPortWithName(port, route);
406+ IsolateNameServer.registerPortWithName(
407+ portForPackageTestCommunication,
408+ '${route}_for_package_test_communication',
409+ );
410+ IsolateNameServer.registerPortWithName(
411+ portForShutdownNotification,
412+ '${route}_for_shutdown_notification',
413+ );
399414 _spawn(entrypoint.toNativeUtf8(), route.toNativeUtf8());
400415}
401416''' );
402417
403- buffer.write ('''
418+ buffer.writeln ('''
404419/// Runs on a spawned isolate.
405420void createChannelAndConnect(String path, String name, Function testMain) {
406421 goldenFileComparator = LocalFileComparator(Uri.parse(path));
407422 autoUpdateGoldenFiles = $autoUpdateGoldenFiles ;
408- final IsolateChannel<dynamic> channel = IsolateChannel<dynamic>.connectSend(
409- IsolateNameServer.lookupPortByName(name)!,
423+ ''' );
424+
425+ buffer.write (r'''
426+ final IsolateChannel<dynamic> channelForPackageTestCommunication = IsolateChannel<dynamic>.connectSend(
427+ IsolateNameServer.lookupPortByName('${name}_for_package_test_communication')!,
428+ );
429+ final IsolateChannel<dynamic> channelForShutdownNotification = IsolateChannel<dynamic>.connectSend(
430+ IsolateNameServer.lookupPortByName('${name}_for_shutdown_notification')!,
431+ );
432+
433+ final StreamChannel<Object?> remoteListenerChannel = RemoteListener.start(() => testMain);
434+ remoteListenerChannel.stream.pipe(channelForPackageTestCommunication.sink);
435+
436+ final StreamSubscription<dynamic> channelForPackageTestCommunicationSubscription = channelForPackageTestCommunication.stream.listen(
437+ remoteListenerChannel.sink.add,
410438 );
411- channel.pipe(RemoteListener.start(() => testMain));
439+
440+ channelForShutdownNotification.stream.listen(null, onDone: () async {
441+ await channelForPackageTestCommunicationSubscription.cancel();
442+ await remoteListenerChannel.sink.close();
443+ await channelForPackageTestCommunication.sink.close();
444+ await channelForShutdownNotification.sink.close();
445+ });
412446}
413447
448+ @pragma('vm:entry-point')
414449void testMain() {
415450 final String route = PlatformDispatcher.instance.defaultRouteName;
416451 switch (route) {
@@ -438,8 +473,19 @@ void main([dynamic sendPort]) {
438473 final ReceivePort receivePort = ReceivePort();
439474 receivePort.listen((dynamic msg) {
440475 switch (msg as List<dynamic>) {
441- case ['spawn', final SendPort port, final String entrypoint, final String route]:
442- spawn(port: port, entrypoint: entrypoint, route: route);
476+ case [
477+ 'spawn',
478+ final SendPort portForPackageTestCommunication,
479+ final SendPort portForShutdownNotification,
480+ final String entrypoint,
481+ final String route
482+ ]:
483+ spawn(
484+ portForPackageTestCommunication: portForPackageTestCommunication,
485+ portForShutdownNotification: portForShutdownNotification,
486+ entrypoint: entrypoint,
487+ route: route,
488+ );
443489 case ['close']:
444490 receivePort.close();
445491 }
@@ -499,15 +545,22 @@ bool readyToRun = false;
499545final Completer<void> readyToRunSignal = Completer<void>();
500546
501547Future<void> spawn({
502- required SendPort port,
548+ required SendPort portForPackageTestCommunication,
549+ required SendPort portForShutdownNotification,
503550 String entrypoint = 'main',
504551 String route = '/',
505552}) async {
506553 if (!readyToRun) {
507554 await readyToRunSignal.future;
508555 }
509556
510- commandPort.send(<Object>['spawn', port, entrypoint, route]);
557+ commandPort.send(<Object>[
558+ 'spawn',
559+ portForPackageTestCommunication,
560+ portForShutdownNotification,
561+ entrypoint,
562+ route,
563+ ]);
511564}
512565
513566void main() async {
@@ -555,20 +608,40 @@ String pathToImport(String path) {
555608 .replaceRange(path.length - '.dart'.length, null, '')
556609 .replaceAll('.', '_')
557610 .replaceAll(':', '_')
611+ .replaceAll('-', '_')
558612 .replaceAll('/', '_')
559613 .replaceAll(r'\', '_');
560614}
561615
562616class SpawnPlugin extends PlatformPlugin {
563617 SpawnPlugin();
564618
565- final Map<String, IsolateChannel <dynamic>> _channels = <String, IsolateChannel <dynamic>>{};
619+ final Map<String, StreamChannel <dynamic>> _channels = <String, StreamChannel <dynamic>>{};
566620
567621 Future<void> launchIsolate(String path) async {
622+ final ReceivePort portForShutdownNotification = ReceivePort();
623+ final IsolateChannel<dynamic> channelForShutdownNotification = IsolateChannel<dynamic>.connectReceive(
624+ portForShutdownNotification,
625+ );
626+
568627 final String name = pathToImport(path);
569- final ReceivePort port = ReceivePort();
570- _channels[name] = IsolateChannel<dynamic>.connectReceive(port);
571- await spawn(port: port.sendPort, route: name);
628+ final ReceivePort portForPackageTestCommunication = ReceivePort();
629+ late final StreamChannel<dynamic> channelForPackageTestCommunication;
630+ channelForPackageTestCommunication = IsolateChannel<dynamic>.connectReceive(portForPackageTestCommunication).transformStream(
631+ StreamTransformer<dynamic, dynamic>.fromHandlers(
632+ handleDone: (EventSink<dynamic> sink) async {
633+ await channelForShutdownNotification.sink.close();
634+ sink.close();
635+ },
636+ ),
637+ );
638+ _channels[name] = channelForPackageTestCommunication;
639+
640+ await spawn(
641+ portForPackageTestCommunication: portForPackageTestCommunication.sendPort,
642+ portForShutdownNotification: portForShutdownNotification.sendPort,
643+ route: name,
644+ );
572645 }
573646
574647 @override
0 commit comments