Chromium Code Reviews| 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 // Helper functions and classes for running a set of unittests in a | |
| 6 // remote isolate. | |
| 7 // Used to test Isolate.spawn because dartium/drt does not allow it in the DOM | |
| 8 // isolate. | |
| 9 | |
| 10 import "dart:isolate"; | |
| 11 import "package:unittest/unittest.dart"; | |
| 12 @MirrorsUsed(symbols: "main", targets: "main", override: "*") | |
| 13 import "dart:mirrors"; | |
| 14 | |
| 15 /** | |
| 16 * Use this function at the beginning of the main method: | |
| 17 * | |
| 18 * void main([args, port]) { | |
| 19 * if (testRemote(main, port)) return; | |
| 20 * // the usual test. | |
| 21 * } | |
| 22 * | |
| 23 * Remember to import unittest using the URI `package:inittest/unittest.dart`. | |
| 24 * Otherwise it won't be sharing the `unittestConfiguration` with this library, | |
| 25 * and the override set below won't work. | |
| 26 * | |
| 27 * Returns `true` if the tests are being run remotely, and | |
| 28 * `false` if the tests should be run locally. | |
| 29 */ | |
| 30 bool testRemote(Function main, SendPort port) { | |
| 31 if (port != null) { | |
| 32 unittestConfiguration = new RemoteConfiguration(port); | |
| 33 return false; | |
| 34 } | |
| 35 var testResponses = new Map<String, List>(); | |
| 36 | |
| 37 | |
| 38 ClosureMirror closure = reflect(main); | |
| 39 LibraryMirror library = closure.function.owner; | |
| 40 | |
| 41 var receivePort = new ReceivePort(); | |
| 42 void remoteAction(message) { | |
| 43 switch (message[0]) { | |
| 44 case "testStart": | |
| 45 String name = message[1]; | |
| 46 testResponses[name] = null; | |
| 47 break; | |
| 48 case "testResult": | |
| 49 case "testResultChanged": | |
| 50 String name = message[1]; | |
| 51 testResponses[name] = message; | |
| 52 break; | |
| 53 case "logMessage": | |
| 54 break; // Ignore. | |
| 55 case "summary": | |
| 56 throw message[1]; // Uncaught error. | |
| 57 case "done": | |
| 58 receivePort.close(); | |
| 59 _simulateTests(testResponses); | |
| 60 break; | |
| 61 } | |
| 62 } | |
| 63 try { | |
| 64 Isolate.spawnUri(library.uri, null, receivePort.sendPort); | |
|
floitsch
2013/11/22 17:02:04
normally spawnUri should not throw, but return the
| |
| 65 receivePort.listen(remoteAction); | |
| 66 return true; | |
| 67 } catch (e) { | |
| 68 // spawnUri is only supported by dart2js if web workers are available. | |
| 69 // If the spawnUri fails, run the tests locally instead, since we are | |
| 70 // not in a browser anyway. | |
| 71 // | |
| 72 // That is, we assume that either Isolate.spawn or Isolate.spawnUri must | |
| 73 // work, so if spawnUri doesn't work, we can run the tests directly. | |
| 74 receivePort.close(); | |
| 75 return false; | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 class RemoteConfiguration implements Configuration { | |
| 80 final SendPort _port; | |
| 81 Duration timeout = const Duration(minutes: 2); | |
| 82 | |
| 83 RemoteConfiguration(this._port); | |
| 84 | |
| 85 bool get autoStart => true; | |
| 86 | |
| 87 void onInit() { } | |
| 88 | |
| 89 void onStart() { } | |
| 90 | |
| 91 void onTestStart(TestCase testCase) { | |
| 92 _port.send(["testStart", testCase.description]); | |
| 93 } | |
| 94 | |
| 95 void onTestResult(TestCase testCase) { | |
| 96 _port.send(["testResult", testCase.description, | |
| 97 testCase.result, testCase.message]); | |
| 98 } | |
| 99 | |
| 100 void onTestResultChanged(TestCase testCase) { | |
| 101 _port.send(["testResultChanged", testCase.description, | |
| 102 testCase.result, testCase.message]); | |
| 103 } | |
| 104 | |
| 105 void onLogMessage(TestCase testCase, String message) { | |
| 106 _port.send(["logMessage", testCase.description, message]); | |
| 107 } | |
| 108 | |
| 109 void onDone(bool success) { | |
| 110 _port.send(["done", success]); | |
| 111 } | |
| 112 | |
| 113 void onSummary(int passed, int failed, int errors, List<TestCase> results, | |
| 114 String uncaughtError) { | |
| 115 if (uncaughtError != null) { | |
| 116 _port.send(["summary", uncaughtError]); | |
| 117 } | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void _simulateTests(Map<String, List> responses) { | |
| 122 // Start all unit tests in the same event. | |
| 123 responses.forEach((name, response) { | |
| 124 test(name, () { | |
| 125 var result = response[2]; | |
| 126 var message = response[3]; | |
| 127 if (result == FAIL) { | |
| 128 fail(message); | |
| 129 } else if (result == ERROR) { | |
| 130 throw message; | |
| 131 } | |
| 132 }); | |
| 133 }); | |
| 134 } | |
| OLD | NEW |