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