| 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 import 'dart:async'; | |
| 6 import 'dart:isolate'; | |
| 7 | |
| 8 import '../../backend/group.dart'; | |
| 9 import '../../backend/live_test.dart'; | |
| 10 import '../../backend/live_test_controller.dart'; | |
| 11 import '../../backend/metadata.dart'; | |
| 12 import '../../backend/operating_system.dart'; | |
| 13 import '../../backend/state.dart'; | |
| 14 import '../../backend/suite.dart'; | |
| 15 import '../../backend/test.dart'; | |
| 16 import '../../backend/test_platform.dart'; | |
| 17 import '../../util/remote_exception.dart'; | |
| 18 import '../../utils.dart'; | |
| 19 | |
| 20 /// A test in another isolate. | |
| 21 class IsolateTest extends Test { | |
| 22 final String name; | |
| 23 final Metadata metadata; | |
| 24 | |
| 25 /// The port on which to communicate with the remote test. | |
| 26 final SendPort _sendPort; | |
| 27 | |
| 28 IsolateTest(this.name, this.metadata, this._sendPort); | |
| 29 | |
| 30 LiveTest load(Suite suite, {Iterable<Group> groups}) { | |
| 31 var controller; | |
| 32 | |
| 33 // We get a new send port for communicating with the live test, since | |
| 34 // [_sendPort] is only for communicating with the non-live test. This will | |
| 35 // be non-null once the test starts running. | |
| 36 var sendPortCompleter; | |
| 37 | |
| 38 var receivePort; | |
| 39 controller = new LiveTestController(suite, this, () { | |
| 40 controller.setState(const State(Status.running, Result.success)); | |
| 41 | |
| 42 sendPortCompleter = new Completer(); | |
| 43 receivePort = new ReceivePort(); | |
| 44 _sendPort.send({ | |
| 45 'command': 'run', | |
| 46 'reply': receivePort.sendPort | |
| 47 }); | |
| 48 | |
| 49 receivePort.listen((message) { | |
| 50 if (message['type'] == 'started') { | |
| 51 sendPortCompleter.complete(message['reply']); | |
| 52 } else if (message['type'] == 'error') { | |
| 53 var asyncError = RemoteException.deserialize(message['error']); | |
| 54 controller.addError(asyncError.error, asyncError.stackTrace); | |
| 55 } else if (message['type'] == 'state-change') { | |
| 56 controller.setState( | |
| 57 new State( | |
| 58 new Status.parse(message['status']), | |
| 59 new Result.parse(message['result']))); | |
| 60 } else if (message['type'] == 'print') { | |
| 61 controller.print(message['line']); | |
| 62 } else { | |
| 63 assert(message['type'] == 'complete'); | |
| 64 controller.completer.complete(); | |
| 65 } | |
| 66 }); | |
| 67 }, () { | |
| 68 // If the test has finished running, just disconnect the receive port. The | |
| 69 // Dart process won't terminate if there are any live receive ports open. | |
| 70 if (controller.completer.isCompleted) { | |
| 71 receivePort.close(); | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 invoke(() async { | |
| 76 // If the test is still running, send it a message telling it to shut | |
| 77 // down ASAP. This causes the [Invoker] to eagerly throw exceptions | |
| 78 // whenever the test touches it. | |
| 79 var sendPort = await sendPortCompleter.future; | |
| 80 sendPort.send({'command': 'close'}); | |
| 81 await controller.completer.future; | |
| 82 receivePort.close(); | |
| 83 }); | |
| 84 }, groups: groups); | |
| 85 return controller.liveTest; | |
| 86 } | |
| 87 | |
| 88 Test forPlatform(TestPlatform platform, {OperatingSystem os}) { | |
| 89 if (!metadata.testOn.evaluate(platform, os: os)) return null; | |
| 90 return new IsolateTest( | |
| 91 name, metadata.forPlatform(platform, os: os), _sendPort); | |
| 92 } | |
| 93 } | |
| OLD | NEW |