OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 unittest.vm_listener; |
| 6 |
| 7 import 'dart:isolate'; |
| 8 import 'dart:async'; |
| 9 |
| 10 import 'declarer.dart'; |
| 11 import 'remote_exception.dart'; |
| 12 import 'suite.dart'; |
| 13 import 'test.dart'; |
| 14 |
| 15 /// A class that runs tests in a separate isolate and communicates the results |
| 16 /// back to the main isolate. |
| 17 class VmListener { |
| 18 /// The test suite to run. |
| 19 final Suite _suite; |
| 20 |
| 21 /// Extracts metadata about all the tests in [main] and sends information |
| 22 /// about them over [sendPort]. |
| 23 /// |
| 24 /// Once that's done, this starts listening for commands about which tests to |
| 25 /// run. |
| 26 static void start(SendPort sendPort, main()) { |
| 27 var declarer = new Declarer(); |
| 28 runZoned(main, zoneValues: {#unittest.declarer: declarer}); |
| 29 new VmListener._(new Suite("VmListener", declarer.tests)) |
| 30 ._listen(sendPort); |
| 31 } |
| 32 |
| 33 VmListener._(this._suite); |
| 34 |
| 35 /// Send information about [_suite] across [sendPort] and start listening for |
| 36 /// commands to run the tests. |
| 37 void _listen(SendPort sendPort) { |
| 38 var tests = []; |
| 39 for (var i = 0; i < _suite.tests.length; i++) { |
| 40 var test = _suite.tests[i]; |
| 41 var receivePort = new ReceivePort(); |
| 42 tests.add({"name": test.name, "sendPort": receivePort.sendPort}); |
| 43 |
| 44 receivePort.listen((message) { |
| 45 assert(message['command'] == 'run'); |
| 46 _runTest(test, message['reply']); |
| 47 }); |
| 48 } |
| 49 |
| 50 sendPort.send(tests); |
| 51 } |
| 52 |
| 53 /// Runs [test] and send the results across [sendPort]. |
| 54 void _runTest(Test test, SendPort sendPort) { |
| 55 var liveTest = test.load(_suite); |
| 56 |
| 57 liveTest.onStateChange.listen((state) { |
| 58 sendPort.send({ |
| 59 "type": "state-change", |
| 60 "status": state.status.name, |
| 61 "result": state.result.name |
| 62 }); |
| 63 }); |
| 64 |
| 65 liveTest.onError.listen((asyncError) { |
| 66 sendPort.send({ |
| 67 "type": "error", |
| 68 "error": RemoteException.serialize( |
| 69 asyncError.error, asyncError.stackTrace) |
| 70 }); |
| 71 }); |
| 72 |
| 73 liveTest.run().then((_) => sendPort.send({"type": "complete"})); |
| 74 } |
| 75 } |
OLD | NEW |