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 var replyAsString; |
| 38 try { |
| 39 replyAsString = UTF.decodeUtf8(data, 0, null, null); |
| 40 } catch (e) { |
| 41 onRequestFailed(e); |
| 42 return; |
| 43 } |
| 44 var 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 var dartScript = Platform.script; |
| 73 var splitPoint = dartScript.lastIndexOf(Platform.pathSeparator); |
| 74 var 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 var portNumber; |
| 85 var blank; |
| 86 var first = true; |
| 87 process.stdout.transform(new StringDecoder()) |
| 88 .transform(new LineTransformer()).listen((line) { |
| 89 if (line.startsWith('VmService listening on port ')) { |
| 90 RegExp portExp = new RegExp(r"\d+"); |
| 91 var port = portExp.stringMatch(line); |
| 92 portNumber = int.parse(port); |
| 93 } |
| 94 if (line == '') { |
| 95 // Received blank line. |
| 96 blank = true; |
| 97 } |
| 98 if (portNumber != null && blank == true && first == true) { |
| 99 completer.complete(portNumber); |
| 100 // Stop repeat completions. |
| 101 first = false; |
| 102 } |
| 103 }); |
| 104 process.stderr.transform(new StringDecoder()) |
| 105 .transform(new LineTransformer()).listen((line) { |
| 106 }); |
| 107 process.exitCode.then((_) { }); |
| 108 return completer.future; |
| 109 }); |
| 110 } |
| 111 |
| 112 void requestExit() { |
| 113 process.stdin.add([32]); |
| 114 } |
| 115 } |
| 116 |
| 117 class IsolateListTester { |
| 118 final Map isolateList; |
| 119 |
| 120 IsolateListTester(this.isolateList) { |
| 121 // The reply is an IsolateList. |
| 122 Expect.equals('IsolateList', isolateList['type']); |
| 123 } |
| 124 |
| 125 void checkIsolateCount(int n) { |
| 126 Expect.equals(n, isolateList['members'].length); |
| 127 } |
| 128 |
| 129 void checkIsolateIdExists(int id) { |
| 130 var exists = false; |
| 131 isolateList['members'].forEach((isolate) { |
| 132 if (isolate['id'] == id) { |
| 133 exists = true; |
| 134 } |
| 135 }); |
| 136 Expect.isTrue(exists); |
| 137 } |
| 138 |
| 139 void checkIsolateNameContains(String name) { |
| 140 var exists = false; |
| 141 isolateList['members'].forEach((isolate) { |
| 142 if (isolate['name'].contains(name)) { |
| 143 exists = true; |
| 144 } |
| 145 }); |
| 146 Expect.isTrue(exists); |
| 147 } |
| 148 |
| 149 void checkIsolateNamePrefix(int id, String name) { |
| 150 var exists = false; |
| 151 isolateList['members'].forEach((isolate) { |
| 152 if (isolate['id'] == id) { |
| 153 exists = true; |
| 154 Expect.isTrue(isolate['name'].startsWith(name)); |
| 155 } |
| 156 }); |
| 157 Expect.isTrue(exists); |
| 158 } |
| 159 } |
OLD | NEW |