|
| 1 | +// Copyright (c) 2022, 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 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | +import 'dart:io'; |
| 8 | + |
| 9 | +import 'package:test/test.dart'; |
| 10 | +import 'package:vm_service/vm_service.dart'; |
| 11 | +import 'package:vm_service/vm_service_io.dart'; |
| 12 | + |
| 13 | +late Uri remoteVmServiceUri; |
| 14 | + |
| 15 | +Future<Process> spawnDartProcess(String script) async { |
| 16 | + final executable = Platform.executable; |
| 17 | + final tmpDir = await Directory.systemTemp.createTemp('dart_service'); |
| 18 | + final serviceInfoUri = tmpDir.uri.resolve('service_info.json'); |
| 19 | + final serviceInfoFile = await File.fromUri(serviceInfoUri).create(); |
| 20 | + |
| 21 | + final arguments = [ |
| 22 | + '--disable-dart-dev', |
| 23 | + '--observe=0', |
| 24 | + '--disable-service-auth-codes', |
| 25 | + '--write-service-info=$serviceInfoUri', |
| 26 | + ...Platform.executableArguments, |
| 27 | + Platform.script.resolve(script).toString(), |
| 28 | + ]; |
| 29 | + final process = await Process.start(executable, arguments); |
| 30 | + process.stdout |
| 31 | + .transform(utf8.decoder) |
| 32 | + .listen((line) => print('TESTEE OUT: $line')); |
| 33 | + process.stderr |
| 34 | + .transform(utf8.decoder) |
| 35 | + .listen((line) => print('TESTEE ERR: $line')); |
| 36 | + while ((await serviceInfoFile.length()) <= 5) { |
| 37 | + await Future.delayed(const Duration(milliseconds: 50)); |
| 38 | + } |
| 39 | + final content = await serviceInfoFile.readAsString(); |
| 40 | + final infoJson = json.decode(content); |
| 41 | + remoteVmServiceUri = |
| 42 | + Uri.parse(infoJson['uri']).replace(scheme: 'ws', path: 'ws'); |
| 43 | + return process; |
| 44 | +} |
| 45 | + |
| 46 | +Future<void> waitForRunnableIsolate(VmService service, Isolate isolate) async { |
| 47 | + bool runnable = isolate.runnable!; |
| 48 | + while (!runnable) { |
| 49 | + await Future.delayed(const Duration(milliseconds: 100)); |
| 50 | + runnable = (await service.getIsolate(isolate.id!)).runnable!; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void main() { |
| 55 | + group('VM Service', () { |
| 56 | + late Process process; |
| 57 | + |
| 58 | + setUp(() async { |
| 59 | + process = |
| 60 | + await spawnDartProcess('external_compilation_service_script.dart'); |
| 61 | + }); |
| 62 | + |
| 63 | + tearDown(() async { |
| 64 | + process.kill(); |
| 65 | + }); |
| 66 | + |
| 67 | + test('evaluate invokes client provided compileExpression RPC', () async { |
| 68 | + final service = await vmServiceConnectUri(remoteVmServiceUri.toString()); |
| 69 | + await service.registerService( |
| 70 | + 'compileExpression', |
| 71 | + 'Custom Expression Compilation', |
| 72 | + ); |
| 73 | + bool invokedCompileExpression = false; |
| 74 | + service.registerServiceCallback('compileExpression', (params) async { |
| 75 | + invokedCompileExpression = true; |
| 76 | + throw 'error'; |
| 77 | + }); |
| 78 | + final vm = await service.getVM(); |
| 79 | + final isolate = await service.getIsolate(vm.isolates!.first.id!); |
| 80 | + await waitForRunnableIsolate(service, isolate); |
| 81 | + try { |
| 82 | + await service.evaluate( |
| 83 | + isolate.id!, isolate.libraries!.first.id!, '1 + 1'); |
| 84 | + } catch (_) { |
| 85 | + // ignore error |
| 86 | + } |
| 87 | + expect(invokedCompileExpression, true); |
| 88 | + }); |
| 89 | + |
| 90 | + test('evaluateInFrame invokes client provided compileExpression RPC', |
| 91 | + () async { |
| 92 | + final service = await vmServiceConnectUri(remoteVmServiceUri.toString()); |
| 93 | + await service.registerService( |
| 94 | + 'compileExpression', |
| 95 | + 'Custom Expression Compilation', |
| 96 | + ); |
| 97 | + bool invokedCompileExpression = false; |
| 98 | + service.registerServiceCallback('compileExpression', (params) async { |
| 99 | + invokedCompileExpression = true; |
| 100 | + throw 'error'; |
| 101 | + }); |
| 102 | + final vm = await service.getVM(); |
| 103 | + final isolate = await service.getIsolate(vm.isolates!.first.id!); |
| 104 | + await waitForRunnableIsolate(service, isolate); |
| 105 | + try { |
| 106 | + await service.evaluateInFrame(isolate.id!, 0, '1 + 1'); |
| 107 | + } catch (e) { |
| 108 | + // ignore error |
| 109 | + } |
| 110 | + expect(invokedCompileExpression, true); |
| 111 | + }); |
| 112 | + }); |
| 113 | +} |
0 commit comments