OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 library vmservice_test_helper; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 import 'dart:json' as JSON; |
| 10 import 'dart:utf' as UTF; |
| 11 import 'package:expect/expect.dart'; |
| 12 |
| 13 abstract class VmServiceRequestHelper { |
| 14 final Uri uri; |
| 15 final HttpClient client; |
| 16 |
| 17 VmServiceRequestHelper(String url) : |
| 18 uri = Uri.parse(url), |
| 19 client = new HttpClient(); |
| 20 |
| 21 Future makeRequest() { |
| 22 return client.getUrl(uri) |
| 23 .then((HttpClientRequest request) => request.close()) |
| 24 .then((HttpClientResponse response) { |
| 25 return response |
| 26 .fold(new BytesBuilder(), (b, d) => b..add(d)) |
| 27 .then((builder) { |
| 28 _requestCompleted(builder.takeBytes(), response); |
| 29 }); |
| 30 }).catchError((error) { |
| 31 onRequestFailed(error); |
| 32 }); |
| 33 } |
| 34 |
| 35 void _requestCompleted(List<int> data, HttpClientResponse response) { |
| 36 Expect.equals(200, response.statusCode); |
| 37 String replyAsString; |
| 38 try { |
| 39 replyAsString = UTF.decodeUtf8(data, 0, null, null); |
| 40 } catch (e) { |
| 41 onRequestFailed(e); |
| 42 return; |
| 43 } |
| 44 Map reply; |
| 45 try { |
| 46 reply = JSON.parse(replyAsString); |
| 47 } catch (e) { |
| 48 onRequestFailed(e); |
| 49 return; |
| 50 } |
| 51 // Received a map. |
| 52 Expect.isTrue(reply is Map); |
| 53 // Has a 'type' key. |
| 54 Expect.isNotNull(reply['type']); |
| 55 onRequestCompleted(reply); |
| 56 } |
| 57 |
| 58 void onRequestFailed(dynamic error) { |
| 59 Expect.fail('Failed to make request: $error'); |
| 60 } |
| 61 |
| 62 void onRequestCompleted(Map response); |
| 63 } |
| 64 |
| 65 class TestLauncher { |
| 66 final String script; |
| 67 Process process; |
| 68 |
| 69 TestLauncher(this.script); |
| 70 |
| 71 String get scriptPath { |
| 72 String dartScript = Platform.script; |
| 73 int splitPoint = dartScript.lastIndexOf(Platform.pathSeparator); |
| 74 String scriptDirectory = dartScript.substring(0, splitPoint); |
| 75 return scriptDirectory + Platform.pathSeparator + script; |
| 76 } |
| 77 |
| 78 Future<int> launch() { |
| 79 String dartExecutable = Platform.executable; |
| 80 return Process.start(dartExecutable, |
| 81 ['--enable-vm-service:0', scriptPath]).then((p) { |
| 82 Completer completer = new Completer(); |
| 83 process = p; |
| 84 int portNumber; |
| 85 bool blank; |
| 86 process.stdout.transform(new StringDecoder()) |
| 87 .transform(new LineTransformer()).listen((line) { |
| 88 if (line.startsWith('VmService listening on port ')) { |
| 89 RegExp portExp = new RegExp(r"\d+"); |
| 90 var port = portExp.stringMatch(line); |
| 91 portNumber = int.parse(port); |
| 92 } |
| 93 if (line == '') { |
| 94 // Received blank line. |
| 95 blank = true; |
| 96 } |
| 97 if (portNumber != null && blank == true) { |
| 98 completer.complete(portNumber); |
| 99 } |
| 100 }); |
| 101 process.stderr.transform(new StringDecoder()) |
| 102 .transform(new LineTransformer()).listen((line) { |
| 103 }); |
| 104 process.exitCode.then((_) { }); |
| 105 return completer.future; |
| 106 }); |
| 107 } |
| 108 |
| 109 void requestExit() { |
| 110 process.stdin.add([32]); |
| 111 } |
| 112 } |
| 113 |
| 114 class IsolateListTester { |
| 115 final Map isolateList; |
| 116 |
| 117 IsolateListTester(this.isolateList) { |
| 118 // The reply is an IsolateList. |
| 119 Expect.equals('IsolateList', isolateList['type']); |
| 120 } |
| 121 |
| 122 void checkIsolateCount(int n) { |
| 123 Expect.equals(n, isolateList['members'].length); |
| 124 } |
| 125 |
| 126 void checkIsolateIdExists(int id) { |
| 127 bool exists = false; |
| 128 isolateList['members'].forEach((isolate) { |
| 129 if (isolate['id'] == id) { |
| 130 exists = true; |
| 131 } |
| 132 }); |
| 133 Expect.isTrue(exists); |
| 134 } |
| 135 |
| 136 void checkIsolateNameContains(String name) { |
| 137 bool exists = false; |
| 138 isolateList['members'].forEach((isolate) { |
| 139 if (isolate['name'].contains(name)) { |
| 140 exists = true; |
| 141 } |
| 142 }); |
| 143 Expect.isTrue(exists); |
| 144 } |
| 145 |
| 146 void checkIsolateNamePrefix(int id, String name) { |
| 147 bool exists = false; |
| 148 isolateList['members'].forEach((isolate) { |
| 149 if (isolate['id'] == id) { |
| 150 exists = true; |
| 151 Expect.isTrue(isolate['name'].startsWith(name)); |
| 152 } |
| 153 }); |
| 154 Expect.isTrue(exists); |
| 155 } |
| 156 } |
OLD | NEW |