OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
8 | 8 |
9 // This test attempts to check that the vm can shutdown cleanly when | 9 // This test attempts to check that the vm can shutdown cleanly when |
10 // isolates are starting and stopping. | 10 // isolates are starting and stopping. |
11 // | 11 // |
12 // We spawn a set of workers. Each worker will kill its parent | 12 // We spawn a set of workers. Each worker will kill its parent |
13 // worker (if any) and then spawn a child worker. We start these | 13 // worker (if any) and then spawn a child worker. We start these |
14 // workers in a staggered fashion in an attempt to see a variety of | 14 // workers in a staggered fashion in an attempt to see a variety of |
15 // isolate states at the time that this program terminates. | 15 // isolate states at the time that this program terminates. |
16 | 16 |
17 trySpawn(Function f, Object o) async { | 17 trySpawn(Function f, Object o) async { |
18 try { | 18 try { |
19 await Isolate.spawn(f, o); | 19 await Isolate.spawn(f, o); |
20 } catch (e) { | 20 } catch (e) { |
21 // Isolate spawning may fail if the program is ending. | 21 // Isolate spawning may fail if the program is ending. |
22 assert(e is IsolateSpawnException); | 22 assert(e is IsolateSpawnException); |
23 } | 23 } |
24 } | 24 } |
25 | 25 |
26 void worker(SendPort parentPort) { | 26 void worker(SendPort parentPort) { |
27 var port = new RawReceivePort(); | 27 var port = new RawReceivePort(); |
28 | 28 |
29 // This worker will exit when it receives any message. | 29 // This worker will exit when it receives any message. |
30 port.handler = (_) { port.close(); }; | 30 port.handler = (_) { |
| 31 port.close(); |
| 32 }; |
31 | 33 |
32 // Send a message to terminate our parent isolate. | 34 // Send a message to terminate our parent isolate. |
33 if (parentPort != null) { | 35 if (parentPort != null) { |
34 parentPort.send(null); | 36 parentPort.send(null); |
35 } | 37 } |
36 | 38 |
37 // Spawn a child worker. | 39 // Spawn a child worker. |
38 trySpawn(worker, port.sendPort); | 40 trySpawn(worker, port.sendPort); |
39 } | 41 } |
40 | 42 |
41 void main() { | 43 void main() { |
42 const numWorkers = 50; | 44 const numWorkers = 50; |
43 const delay = const Duration(milliseconds:(1000 ~/ numWorkers)); | 45 const delay = const Duration(milliseconds: (1000 ~/ numWorkers)); |
44 const exitDelay = const Duration(seconds: 2); | 46 const exitDelay = const Duration(seconds: 2); |
45 | 47 |
46 // Take about a second to spin up our workers in a staggered | 48 // Take about a second to spin up our workers in a staggered |
47 // fashion. We want to maximize the chance that they will be in a | 49 // fashion. We want to maximize the chance that they will be in a |
48 // variety of states when the vm shuts down. | 50 // variety of states when the vm shuts down. |
49 print('Starting ${numWorkers} workers...'); | 51 print('Starting ${numWorkers} workers...'); |
50 for (int i = 0; i < numWorkers; i++) { | 52 for (int i = 0; i < numWorkers; i++) { |
51 trySpawn(worker, null); | 53 trySpawn(worker, null); |
52 sleep(delay); | 54 sleep(delay); |
53 } | 55 } |
54 | 56 |
55 // Let them spin for a bit before terminating the program. | 57 // Let them spin for a bit before terminating the program. |
56 print('Waiting for ${exitDelay} before exit...'); | 58 print('Waiting for ${exitDelay} before exit...'); |
57 sleep(exitDelay); | 59 sleep(exitDelay); |
58 } | 60 } |
OLD | NEW |