OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Helper functions and classes for running a set of unittests in a | 5 // Helper functions and classes for running a set of unittests in a |
6 // remote isolate. | 6 // remote isolate. |
7 // Used to test Isolate.spawn because dartium/drt does not allow it in the DOM | 7 // Used to test Isolate.spawn because dartium/drt does not allow it in the DOM |
8 // isolate. | 8 // isolate. |
9 | 9 |
10 import "dart:isolate"; | 10 import "dart:isolate"; |
(...skipping 16 matching lines...) Expand all Loading... |
27 * Returns `true` if the tests are being run remotely, and | 27 * Returns `true` if the tests are being run remotely, and |
28 * `false` if the tests should be run locally. | 28 * `false` if the tests should be run locally. |
29 */ | 29 */ |
30 bool testRemote(Function main, SendPort port) { | 30 bool testRemote(Function main, SendPort port) { |
31 if (port != null) { | 31 if (port != null) { |
32 unittestConfiguration = new RemoteConfiguration(port); | 32 unittestConfiguration = new RemoteConfiguration(port); |
33 return false; | 33 return false; |
34 } | 34 } |
35 var testResponses = new Map<String, List>(); | 35 var testResponses = new Map<String, List>(); |
36 | 36 |
37 | |
38 ClosureMirror closure = reflect(main); | 37 ClosureMirror closure = reflect(main); |
39 LibraryMirror library = closure.function.owner; | 38 LibraryMirror library = closure.function.owner; |
40 | 39 |
41 var receivePort = new ReceivePort(); | 40 var receivePort = new ReceivePort(); |
42 void remoteAction(message) { | 41 void remoteAction(message) { |
43 switch (message[0]) { | 42 switch (message[0]) { |
44 case "testStart": | 43 case "testStart": |
45 String name = message[1]; | 44 String name = message[1]; |
46 testResponses[name] = null; | 45 testResponses[name] = null; |
47 break; | 46 break; |
48 case "testResult": | 47 case "testResult": |
49 case "testResultChanged": | 48 case "testResultChanged": |
50 String name = message[1]; | 49 String name = message[1]; |
51 testResponses[name] = message; | 50 testResponses[name] = message; |
52 break; | 51 break; |
53 case "logMessage": | 52 case "logMessage": |
54 break; // Ignore. | 53 break; // Ignore. |
55 case "summary": | 54 case "summary": |
56 throw message[1]; // Uncaught error. | 55 throw message[1]; // Uncaught error. |
57 case "done": | 56 case "done": |
58 receivePort.close(); | 57 receivePort.close(); |
59 _simulateTests(testResponses); | 58 _simulateTests(testResponses); |
60 break; | 59 break; |
61 } | 60 } |
62 } | 61 } |
| 62 |
63 try { | 63 try { |
64 Isolate.spawnUri(library.uri, null, receivePort.sendPort); | 64 Isolate.spawnUri(library.uri, null, receivePort.sendPort); |
65 receivePort.listen(remoteAction); | 65 receivePort.listen(remoteAction); |
66 return true; | 66 return true; |
67 } catch (e) { | 67 } catch (e) { |
68 // spawnUri is only supported by dart2js if web workers are available. | 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 | 69 // If the spawnUri fails, run the tests locally instead, since we are |
70 // not in a browser anyway. | 70 // not in a browser anyway. |
71 // | 71 // |
72 // That is, we assume that either Isolate.spawn or Isolate.spawnUri must | 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. | 73 // work, so if spawnUri doesn't work, we can run the tests directly. |
74 receivePort.close(); | 74 receivePort.close(); |
75 return false; | 75 return false; |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
79 class RemoteConfiguration implements Configuration { | 79 class RemoteConfiguration implements Configuration { |
80 final SendPort _port; | 80 final SendPort _port; |
81 Duration timeout = const Duration(minutes: 2); | 81 Duration timeout = const Duration(minutes: 2); |
82 | 82 |
83 RemoteConfiguration(this._port); | 83 RemoteConfiguration(this._port); |
84 | 84 |
85 bool get autoStart => true; | 85 bool get autoStart => true; |
86 | 86 |
87 void onInit() { } | 87 void onInit() {} |
88 | 88 |
89 void onStart() { } | 89 void onStart() {} |
90 | 90 |
91 void onTestStart(TestCase testCase) { | 91 void onTestStart(TestCase testCase) { |
92 _port.send(["testStart", testCase.description]); | 92 _port.send(["testStart", testCase.description]); |
93 } | 93 } |
94 | 94 |
95 void onTestResult(TestCase testCase) { | 95 void onTestResult(TestCase testCase) { |
96 _port.send(["testResult", testCase.description, | 96 _port.send([ |
97 testCase.result, testCase.message]); | 97 "testResult", |
| 98 testCase.description, |
| 99 testCase.result, |
| 100 testCase.message |
| 101 ]); |
98 } | 102 } |
99 | 103 |
100 void onTestResultChanged(TestCase testCase) { | 104 void onTestResultChanged(TestCase testCase) { |
101 _port.send(["testResultChanged", testCase.description, | 105 _port.send([ |
102 testCase.result, testCase.message]); | 106 "testResultChanged", |
| 107 testCase.description, |
| 108 testCase.result, |
| 109 testCase.message |
| 110 ]); |
103 } | 111 } |
104 | 112 |
105 void onLogMessage(TestCase testCase, String message) { | 113 void onLogMessage(TestCase testCase, String message) { |
106 _port.send(["logMessage", testCase.description, message]); | 114 _port.send(["logMessage", testCase.description, message]); |
107 } | 115 } |
108 | 116 |
109 void onDone(bool success) { | 117 void onDone(bool success) { |
110 _port.send(["done", success]); | 118 _port.send(["done", success]); |
111 } | 119 } |
112 | 120 |
(...skipping 12 matching lines...) Expand all Loading... |
125 var result = response[2]; | 133 var result = response[2]; |
126 var message = response[3]; | 134 var message = response[3]; |
127 if (result == FAIL) { | 135 if (result == FAIL) { |
128 fail(message); | 136 fail(message); |
129 } else if (result == ERROR) { | 137 } else if (result == ERROR) { |
130 throw message; | 138 throw message; |
131 } | 139 } |
132 }); | 140 }); |
133 }); | 141 }); |
134 } | 142 } |
OLD | NEW |