OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library MandelIsolateTest; | 5 library MandelIsolateTest; |
| 6 |
6 import 'dart:async'; | 7 import 'dart:async'; |
7 import 'dart:isolate'; | 8 import 'dart:isolate'; |
8 import 'dart:math'; | 9 import 'dart:math'; |
9 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
10 import "remote_unittest_helper.dart"; | 11 import "remote_unittest_helper.dart"; |
11 | 12 |
12 const TERMINATION_MESSAGE = -1; | 13 const TERMINATION_MESSAGE = -1; |
13 const N = 100; | 14 const N = 100; |
14 const ISOLATES = 20; | 15 const ISOLATES = 20; |
15 | 16 |
16 void main([args, port]) { | 17 void main([args, port]) { |
17 if (testRemote(main, port)) return; | 18 if (testRemote(main, port)) return; |
18 // Test is really slow in debug builds of the VM. | 19 // Test is really slow in debug builds of the VM. |
19 var configuration = unittestConfiguration; | 20 var configuration = unittestConfiguration; |
20 configuration.timeout = const Duration(seconds: 480); | 21 configuration.timeout = const Duration(seconds: 480); |
21 test("Render Mandelbrot in parallel", () { | 22 test("Render Mandelbrot in parallel", () { |
22 final state = new MandelbrotState(); | 23 final state = new MandelbrotState(); |
23 state._validated.future.then(expectAsync((result) { | 24 state._validated.future.then(expectAsync((result) { |
24 expect(result, isTrue); | 25 expect(result, isTrue); |
25 })); | 26 })); |
26 for (int i = 0; i < min(ISOLATES, N); i++) state.startClient(i); | 27 for (int i = 0; i < min(ISOLATES, N); i++) state.startClient(i); |
27 }); | 28 }); |
28 } | 29 } |
29 | 30 |
30 | |
31 class MandelbrotState { | 31 class MandelbrotState { |
32 | |
33 MandelbrotState() { | 32 MandelbrotState() { |
34 _result = new List<List<int>>(N); | 33 _result = new List<List<int>>(N); |
35 _lineProcessedBy = new List<LineProcessorClient>(N); | 34 _lineProcessedBy = new List<LineProcessorClient>(N); |
36 _sent = 0; | 35 _sent = 0; |
37 _missing = N; | 36 _missing = N; |
38 _validated = new Completer<bool>(); | 37 _validated = new Completer<bool>(); |
39 } | 38 } |
40 | 39 |
41 void startClient(int id) { | 40 void startClient(int id) { |
42 assert(_sent < N); | 41 assert(_sent < N); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 // print(output); | 81 // print(output); |
83 } | 82 } |
84 | 83 |
85 List<List<int>> _result; | 84 List<List<int>> _result; |
86 List<LineProcessorClient> _lineProcessedBy; | 85 List<LineProcessorClient> _lineProcessedBy; |
87 int _sent; | 86 int _sent; |
88 int _missing; | 87 int _missing; |
89 Completer<bool> _validated; | 88 Completer<bool> _validated; |
90 } | 89 } |
91 | 90 |
92 | |
93 class LineProcessorClient { | 91 class LineProcessorClient { |
94 MandelbrotState _state; | 92 MandelbrotState _state; |
95 int _id; | 93 int _id; |
96 SendPort _port; | 94 SendPort _port; |
97 | 95 |
98 LineProcessorClient(this._state, this._id, this._port); | 96 LineProcessorClient(this._state, this._id, this._port); |
99 | 97 |
100 static Future<LineProcessorClient> create(MandelbrotState state, int id) { | 98 static Future<LineProcessorClient> create(MandelbrotState state, int id) { |
101 ReceivePort reply = new ReceivePort(); | 99 ReceivePort reply = new ReceivePort(); |
102 return Isolate.spawn(processLines, reply.sendPort).then((_) { | 100 return Isolate.spawn(processLines, reply.sendPort).then((_) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 if (message != TERMINATION_MESSAGE) { | 150 if (message != TERMINATION_MESSAGE) { |
153 int line = message[0]; | 151 int line = message[0]; |
154 SendPort replyTo = message[1]; | 152 SendPort replyTo = message[1]; |
155 replyTo.send(processLine(line)); | 153 replyTo.send(processLine(line)); |
156 } else { | 154 } else { |
157 port.close(); | 155 port.close(); |
158 } | 156 } |
159 }); | 157 }); |
160 replyPort.send(port.sendPort); | 158 replyPort.send(port.sendPort); |
161 } | 159 } |
OLD | NEW |