| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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 import 'dart:mirrors'; | |
| 9 | |
| 10 import 'package:expect/expect.dart'; | |
| 11 import 'package:async_helper/async_helper.dart'; | |
| 12 | |
| 13 Map lookupServiceObject(String path) { | |
| 14 var io = currentMirrorSystem().findLibrary(const Symbol('dart.io')); | |
| 15 var m = MirrorSystem.getSymbol('_serviceObjectHandler', io); | |
| 16 var paths = Uri.parse(path).pathSegments; | |
| 17 Expect.equals('io', paths.first); | |
| 18 return JSON.decode( | |
| 19 io.invoke(m, [paths.sublist(1), [], []]).reflectee); | |
| 20 } | |
| 21 | |
| 22 | |
| 23 String getServicePath(obj) { | |
| 24 var io = currentMirrorSystem().findLibrary(const Symbol('dart.io')); | |
| 25 var m = MirrorSystem.getSymbol('_getServicePath', io); | |
| 26 return io.invoke(m, [obj]).reflectee; | |
| 27 } | |
| 28 | |
| 29 | |
| 30 Future testHttpServer1() { | |
| 31 return HttpServer.bind('localhost', 0).then((server) { | |
| 32 var path = getServicePath(server); | |
| 33 var map = lookupServiceObject(path); | |
| 34 Expect.equals(map['type'], 'HttpServer'); | |
| 35 Expect.equals(map['id'], path); | |
| 36 Expect.equals(map['address'], 'localhost'); | |
| 37 Expect.equals(map['port'], server.port); | |
| 38 Expect.equals(map['closed'], false); | |
| 39 Expect.listEquals(map['idle'], []); | |
| 40 Expect.listEquals(map['active'], []); | |
| 41 var socket = map['socket']; | |
| 42 Expect.equals(socket['type'], '@Socket'); | |
| 43 Expect.equals(socket['kind'], 'Listening'); | |
| 44 // Validate owner back-ref. | |
| 45 socket = lookupServiceObject(socket['id']); | |
| 46 Expect.equals(socket['owner']['id'], path); | |
| 47 return server.close(); | |
| 48 }); | |
| 49 } | |
| 50 | |
| 51 | |
| 52 Future testHttpServerConnection1() { | |
| 53 return HttpServer.bind('localhost', 0).then((server) { | |
| 54 server.listen((request) { | |
| 55 var map = lookupServiceObject(getServicePath(server)); | |
| 56 Expect.listEquals(map['idle'], []); | |
| 57 Expect.equals(map['active'].length, 1); | |
| 58 var active = map['active'].first; | |
| 59 Expect.equals(active['type'], '@HttpServerConnection'); | |
| 60 var path = active['id']; | |
| 61 map = lookupServiceObject(path); | |
| 62 Expect.equals(map['type'], 'HttpServerConnection'); | |
| 63 var socket = map['socket']; | |
| 64 Expect.equals(socket['type'], '@Socket'); | |
| 65 Expect.equals(socket['kind'], 'Normal'); | |
| 66 // Validate owner back-ref. | |
| 67 socket = lookupServiceObject(socket['id']); | |
| 68 Expect.equals(socket['owner']['id'], path); | |
| 69 request.response.close(); | |
| 70 }); | |
| 71 var client = new HttpClient(); | |
| 72 return client.get('localhost', server.port, '/') | |
| 73 .then((request) => request.close()) | |
| 74 .then((response) => response.drain()) | |
| 75 .then((_) { | |
| 76 // The connection should be idle now. | |
| 77 var map = lookupServiceObject(getServicePath(server)); | |
| 78 Expect.equals(map['idle'].length, 1); | |
| 79 Expect.listEquals(map['active'], []); | |
| 80 return server.close(); | |
| 81 }); | |
| 82 | |
| 83 }); | |
| 84 } | |
| 85 | |
| 86 | |
| 87 void main() { | |
| 88 final tests = [ | |
| 89 testHttpServer1(), | |
| 90 testHttpServerConnection1(), | |
| 91 ]; | |
| 92 | |
| 93 asyncStart(); | |
| 94 // Run one test at a time. | |
| 95 Future.forEach(tests, (f) => f) | |
| 96 .then((_) { | |
| 97 asyncEnd(); | |
| 98 }); | |
| 99 } | |
| OLD | NEW |