|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Regression test for https://github.com/flutter/flutter/issues/157296 |
| 6 | + |
| 7 | +import 'dart:async'; |
| 8 | + |
| 9 | +import 'package:vm_service/vm_service.dart'; |
| 10 | +import 'package:vm_service/vm_service_io.dart'; |
| 11 | + |
| 12 | +import 'common/service_test_common.dart'; |
| 13 | +import 'common/test_helper.dart'; |
| 14 | + |
| 15 | +Future<void> testMain() async {} |
| 16 | + |
| 17 | +final tests = <IsolateTest>[ |
| 18 | + hasStoppedAtExit, |
| 19 | + (VmService service, IsolateRef isolateRef) async { |
| 20 | + final continueExtensionExecutionCompleter = Completer<void>(); |
| 21 | + final extensionInvokedCompleter = Completer<void>(); |
| 22 | + |
| 23 | + // Setup the service extension. |
| 24 | + const String serviceName = 'testService'; |
| 25 | + service.registerServiceCallback( |
| 26 | + serviceName, |
| 27 | + (Map<String, dynamic> params) async { |
| 28 | + extensionInvokedCompleter.complete(); |
| 29 | + // Wait for the connection to go down before trying to send the |
| 30 | + // response. |
| 31 | + await continueExtensionExecutionCompleter.future; |
| 32 | + return <String, dynamic>{}; |
| 33 | + }, |
| 34 | + ); |
| 35 | + await service.registerService(serviceName, ''); |
| 36 | + |
| 37 | + // Create a secondary service client to invoke the extension. |
| 38 | + final client = await vmServiceConnectUri(service.wsUri!); |
| 39 | + final serviceExtensionCompleter = Completer<String>(); |
| 40 | + client.onServiceEvent.listen((e) { |
| 41 | + if (e.kind == EventKind.kServiceRegistered) { |
| 42 | + print('Service extension registered: ${e.method}'); |
| 43 | + serviceExtensionCompleter.complete(e.method!); |
| 44 | + } |
| 45 | + }); |
| 46 | + await client.streamListen(EventStreams.kService); |
| 47 | + |
| 48 | + // Invoke the extension and wait for the extension to begin executing. |
| 49 | + final extensionName = await serviceExtensionCompleter.future; |
| 50 | + unawaited( |
| 51 | + client |
| 52 | + .callServiceExtension(extensionName) |
| 53 | + .catchError((_) => Response(), test: (o) => o is RPCError), |
| 54 | + ); |
| 55 | + await extensionInvokedCompleter.future; |
| 56 | + |
| 57 | + // Dispose the connection for the VmService instance with the service |
| 58 | + // extension registered to simulate the VM service connection disappearing |
| 59 | + // in the middle of handling a service extension request. |
| 60 | + await service.dispose(); |
| 61 | + |
| 62 | + // Resume the service extension handler. This will cause a |
| 63 | + // `Bad state: StreamSink is closed` error if there's a regression. |
| 64 | + continueExtensionExecutionCompleter.complete(); |
| 65 | + |
| 66 | + // Wait a bit to make sure the exception is thrown before the test |
| 67 | + // completes. |
| 68 | + await Future.delayed(const Duration(milliseconds: 200)); |
| 69 | + }, |
| 70 | +]; |
| 71 | + |
| 72 | +void main([args = const <String>[]]) => runIsolateTests( |
| 73 | + args, |
| 74 | + tests, |
| 75 | + 'regress_157296_test.dart', |
| 76 | + testeeConcurrent: testMain, |
| 77 | + pauseOnExit: true, |
| 78 | + ); |
0 commit comments