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:isolate'; | 7 import 'dart:isolate'; |
7 | 8 |
8 // Spawn an isolate |foo| that will continue trying to spawn isolates even after | 9 // This test attempts to check that the vm can shutdown cleanly when |
9 // the timer in |main| completes. This test ensures that the VM can shutdown | 10 // isolates are starting and stopping. |
10 // correctly even while an isolate is attempting to spawn more isolates. | 11 // |
| 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 |
| 14 // workers in a staggered fashion in an attempt to see a variety of |
| 15 // isolate states at the time that this program terminates. |
11 | 16 |
12 isolate1(sendPort) { | 17 void worker(SendPort parentPort) { |
13 var receivePort = new ReceivePort(); | 18 var port = new RawReceivePort(); |
14 sendPort.send(receivePort.sendPort); | |
15 receivePort.listen((msg) {}); | |
16 } | |
17 | 19 |
18 void foo(_) { | 20 // This worker will exit when it receives any message. |
19 while (true) { | 21 port.handler = (_) { port.close(); }; |
20 var receivePort = new ReceivePort(); | 22 |
21 Isolate.spawn(isolate1, receivePort.sendPort); | 23 // Send a message to terminate our parent isolate. |
22 receivePort.listen((sendPort) { | 24 if (parentPort != null) { |
23 Isolate.spawn(isolate1,sendPort); | 25 parentPort.send(null); |
24 receivePort.close(); | |
25 }); | |
26 } | 26 } |
| 27 |
| 28 // Spawn a child worker. |
| 29 Isolate.spawn(worker, port.sendPort); |
27 } | 30 } |
28 | 31 |
29 void main() { | 32 void main() { |
30 Isolate.spawn(foo, null); | 33 const numWorkers = 50; |
31 new Timer(const Duration(seconds: 10), () {}); | 34 const delay = const Duration(milliseconds:(1000 ~/ numWorkers)); |
| 35 const exitDelay = const Duration(seconds: 2); |
| 36 |
| 37 // Take about a second to spin up our workers in a staggered |
| 38 // fashion. We want to maximize the chance that they will be in a |
| 39 // variety of states when the vm shuts down. |
| 40 print('Starting ${numWorkers} workers...'); |
| 41 for (int i = 0; i < numWorkers; i++) { |
| 42 Isolate.spawn(worker, null); |
| 43 sleep(delay); |
| 44 } |
| 45 |
| 46 // Let them spin for a bit before terminating the program. |
| 47 print('Waiting for ${exitDelay} before exit...'); |
| 48 sleep(exitDelay); |
32 } | 49 } |
OLD | NEW |