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 // spawns multiple isolates and sends unresolved ports between them. | 5 // spawns multiple isolates and sends unresolved ports between them. |
6 library unresolved_ports; | 6 library unresolved_ports; |
7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
8 import '../../pkg/unittest/lib/unittest.dart'; | 8 import '../../pkg/unittest/lib/unittest.dart'; |
9 | 9 |
10 // This test does the following: | 10 // This test does the following: |
11 // - main spawns two isolates: 'tim' and 'beth' | 11 // - main spawns two isolates: 'tim' and 'beth' |
12 // - 'tim' spawns an isolate: 'bob' | 12 // - 'tim' spawns an isolate: 'bob' |
13 // - main starts a message chain: | 13 // - main starts a message chain: |
14 // main -> beth -> tim -> bob -> main | 14 // main -> beth -> tim -> bob -> main |
15 // by giving 'beth' a send-port to 'tim'. | 15 // by giving 'beth' a send-port to 'tim'. |
16 | 16 |
17 bethIsolate() { | 17 bethIsolate(init) { |
| 18 ReceivePort port = initIsolate(init); |
18 // TODO(sigmund): use expectAsync2 when it is OK to use it within an isolate | 19 // TODO(sigmund): use expectAsync2 when it is OK to use it within an isolate |
19 // (issue #6856) | 20 // (issue #6856) |
20 port.receive((msg, reply) => msg[1].send( | 21 port.first.then((msg) => msg[1].send([ |
21 '${msg[0]}\nBeth says: Tim are you coming? And Bob?', reply)); | 22 '${msg[0]}\nBeth says: Tim are you coming? And Bob?', msg[2]])); |
22 } | 23 } |
23 | 24 |
24 timIsolate() { | 25 timIsolate(init) { |
25 SendPort bob = spawnFunction(bobIsolate); | 26 ReceivePort port = initIsolate(init); |
26 port.receive((msg, reply) => bob.send( | 27 spawnFunction(bobIsolate).then((bob) { |
27 '$msg\nTim says: Can you tell "main" that we are all coming?', reply)); | 28 port.first.then((msg) => bob.send([ |
| 29 '${msg[0]}\nTim says: Can you tell "main" that we are all coming?', |
| 30 msg[1]])); |
| 31 }); |
28 } | 32 } |
29 | 33 |
30 bobIsolate() { | 34 bobIsolate(init) { |
31 port.receive((msg, reply) => reply.send( | 35 ReceivePort port = initIsolate(init); |
32 '$msg\nBob says: we are all coming!')); | 36 port.first.then((msg) => msg[1].send( |
| 37 '${msg[0]}\nBob says: we are all coming!')); |
| 38 } |
| 39 |
| 40 Future<SendPort> spawnFunction(function) { |
| 41 ReceivePort init = new ReceivePort(); |
| 42 Isolate.spawn(function, init.sendPort); |
| 43 return init.first; |
| 44 } |
| 45 |
| 46 ReceivePort initIsolate(SendPort starter) { |
| 47 ReceivePort port = new ReceivePort(); |
| 48 starter.send(port.sendPort); |
| 49 return port; |
33 } | 50 } |
34 | 51 |
35 baseTest({bool failForNegativeTest: false}) { | 52 baseTest({bool failForNegativeTest: false}) { |
36 test('Message chain with unresolved ports', () { | 53 test('Message chain with unresolved ports', () { |
37 ReceivePort port = new ReceivePort(); | 54 ReceivePort port = new ReceivePort(); |
38 port.receive(expectAsync2((msg, _) { | 55 port.listen(expectAsync1((msg) { |
39 expect(msg, equals('main says: Beth, find out if Tim is coming.' | 56 expect(msg, equals('main says: Beth, find out if Tim is coming.' |
40 '\nBeth says: Tim are you coming? And Bob?' | 57 '\nBeth says: Tim are you coming? And Bob?' |
41 '\nTim says: Can you tell "main" that we are all coming?' | 58 '\nTim says: Can you tell "main" that we are all coming?' |
42 '\nBob says: we are all coming!')); | 59 '\nBob says: we are all coming!')); |
43 expect(failForNegativeTest, isFalse); | 60 expect(failForNegativeTest, isFalse); |
44 port.close(); | 61 port.close(); |
45 })); | 62 })); |
46 | 63 |
47 SendPort tim = spawnFunction(timIsolate); | 64 spawnFunction(timIsolate).then((tim) { |
48 SendPort beth = spawnFunction(bethIsolate); | 65 spawnFunction(bethIsolate).then((beth) { |
| 66 beth.send(['main says: Beth, find out if Tim is coming.', |
| 67 tim, port.sendPort]); |
| 68 }); |
| 69 }); |
49 | 70 |
50 beth.send( | |
51 // because tim is created asynchronously, here we are sending an | |
52 // unresolved port: | |
53 ['main says: Beth, find out if Tim is coming.', tim], | |
54 port.toSendPort()); | |
55 }); | 71 }); |
56 } | 72 } |
57 | 73 |
58 main() => baseTest(); | 74 main() => baseTest(); |
OLD | NEW |