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 = (_) { | 30 port.handler = (_) { port.close(); }; |
31 port.close(); | |
32 }; | |
33 | 31 |
34 // Send a message to terminate our parent isolate. | 32 // Send a message to terminate our parent isolate. |
35 if (parentPort != null) { | 33 if (parentPort != null) { |
36 parentPort.send(null); | 34 parentPort.send(null); |
37 } | 35 } |
38 | 36 |
39 // Spawn a child worker. | 37 // Spawn a child worker. |
40 trySpawn(worker, port.sendPort); | 38 trySpawn(worker, port.sendPort); |
41 } | 39 } |
42 | 40 |
43 void main() { | 41 void main() { |
44 const numWorkers = 50; | 42 const numWorkers = 50; |
45 const delay = const Duration(milliseconds: (1000 ~/ numWorkers)); | 43 const delay = const Duration(milliseconds:(1000 ~/ numWorkers)); |
46 const exitDelay = const Duration(seconds: 2); | 44 const exitDelay = const Duration(seconds: 2); |
47 | 45 |
48 // Take about a second to spin up our workers in a staggered | 46 // Take about a second to spin up our workers in a staggered |
49 // fashion. We want to maximize the chance that they will be in a | 47 // fashion. We want to maximize the chance that they will be in a |
50 // variety of states when the vm shuts down. | 48 // variety of states when the vm shuts down. |
51 print('Starting ${numWorkers} workers...'); | 49 print('Starting ${numWorkers} workers...'); |
52 for (int i = 0; i < numWorkers; i++) { | 50 for (int i = 0; i < numWorkers; i++) { |
53 trySpawn(worker, null); | 51 trySpawn(worker, null); |
54 sleep(delay); | 52 sleep(delay); |
55 } | 53 } |
56 | 54 |
57 // Let them spin for a bit before terminating the program. | 55 // Let them spin for a bit before terminating the program. |
58 print('Waiting for ${exitDelay} before exit...'); | 56 print('Waiting for ${exitDelay} before exit...'); |
59 sleep(exitDelay); | 57 sleep(exitDelay); |
60 } | 58 } |
OLD | NEW |