|
| 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/dart-lang/sdk/issues/55559. |
| 6 | +// |
| 7 | +// Ensures that the `VmService` instance calls `dispose()` automatically if the |
| 8 | +// VM service connection goes down. Without the `dispose()` call, outstanding |
| 9 | +// requests won't complete unless the developer registered a callback for |
| 10 | +// `VmService.onDone` that calls `dispose()`. |
| 11 | + |
| 12 | +import 'dart:async'; |
| 13 | +import 'dart:io'; |
| 14 | + |
| 15 | +import 'package:test/test.dart'; |
| 16 | +import 'package:vm_service/vm_service.dart'; |
| 17 | +import 'package:vm_service/vm_service_io.dart'; |
| 18 | + |
| 19 | +import 'common/utils.dart'; |
| 20 | + |
| 21 | +void main() { |
| 22 | + (Process, Uri)? state; |
| 23 | + |
| 24 | + void killProcess() { |
| 25 | + if (state != null) { |
| 26 | + final (process, _) = state!; |
| 27 | + process.kill(); |
| 28 | + state = null; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + setUp(() async { |
| 33 | + state = await spawnDartProcess( |
| 34 | + 'regress_55559_script.dart', |
| 35 | + pauseOnStart: false, |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + tearDown(() { |
| 40 | + killProcess(); |
| 41 | + }); |
| 42 | + |
| 43 | + test( |
| 44 | + 'Regress 55559: VmService closes outstanding requests on service disconnect', |
| 45 | + () async { |
| 46 | + final (_, uri) = state!; |
| 47 | + final wsUri = uri.replace( |
| 48 | + scheme: 'ws', |
| 49 | + pathSegments: [ |
| 50 | + // The path will have a trailing '/', so the last path segment is the |
| 51 | + // empty string and should be removed. |
| 52 | + ...[...uri.pathSegments]..removeLast(), |
| 53 | + 'ws', |
| 54 | + ], |
| 55 | + ); |
| 56 | + final service = await vmServiceConnectUri(wsUri.toString()); |
| 57 | + final vm = await service.getVM(); |
| 58 | + final isolate = vm.isolates!.first; |
| 59 | + final errorCompleter = Completer<RPCError>(); |
| 60 | + unawaited( |
| 61 | + service.getIsolate(isolate.id!).then( |
| 62 | + (_) => fail('Future should throw'), |
| 63 | + onError: (e) => errorCompleter.complete(e), |
| 64 | + ), |
| 65 | + ); |
| 66 | + killProcess(); |
| 67 | + |
| 68 | + // Wait for the process to exit and the service connection to close. |
| 69 | + await service.onDone; |
| 70 | + |
| 71 | + // The outstanding getIsolate request should be completed with an error. |
| 72 | + final error = await errorCompleter.future; |
| 73 | + expect(error.code, RPCErrorKind.kServerError.code); |
| 74 | + expect(error.message, 'Service connection disposed'); |
| 75 | + }, |
| 76 | + ); |
| 77 | +} |
0 commit comments