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() { |
| 18 // TODO(sigmund): use expectAsync2 when it is OK to use it within an isolate |
| 19 // (issue #6856) |
18 port.receive((msg, reply) => msg[1].send( | 20 port.receive((msg, reply) => msg[1].send( |
19 '${msg[0]}\nBeth says: Tim are you coming? And Bob?', reply)); | 21 '${msg[0]}\nBeth says: Tim are you coming? And Bob?', reply)); |
20 } | 22 } |
21 | 23 |
22 timIsolate() { | 24 timIsolate() { |
23 SendPort bob = spawnFunction(bobIsolate); | 25 SendPort bob = spawnFunction(bobIsolate); |
24 port.receive((msg, reply) => bob.send( | 26 port.receive((msg, reply) => bob.send( |
25 '$msg\nTim says: Can you tell "main" that we are all coming?', reply)); | 27 '$msg\nTim says: Can you tell "main" that we are all coming?', reply)); |
26 } | 28 } |
27 | 29 |
28 bobIsolate() { | 30 bobIsolate() { |
29 port.receive((msg, reply) => reply.send( | 31 port.receive((msg, reply) => reply.send( |
30 '$msg\nBob says: we are all coming!')); | 32 '$msg\nBob says: we are all coming!')); |
31 } | 33 } |
32 | 34 |
33 main() { | 35 baseTest({bool failForNegativeTest: false}) { |
34 test('Message chain with unresolved ports', () { | 36 test('Message chain with unresolved ports', () { |
35 ReceivePort port = new ReceivePort(); | 37 ReceivePort port = new ReceivePort(); |
36 port.receive(expectAsync2((msg, _) { | 38 port.receive(expectAsync2((msg, _) { |
37 expect(msg, equals('main says: Beth, find out if Tim is coming.' | 39 expect(msg, equals('main says: Beth, find out if Tim is coming.' |
38 '\nBeth says: Tim are you coming? And Bob?' | 40 '\nBeth says: Tim are you coming? And Bob?' |
39 '\nTim says: Can you tell "main" that we are all coming?' | 41 '\nTim says: Can you tell "main" that we are all coming?' |
40 '\nBob says: we are all coming!')); | 42 '\nBob says: we are all coming!')); |
| 43 expect(failForNegativeTest, isFalse); |
41 port.close(); | 44 port.close(); |
42 })); | 45 })); |
43 | 46 |
44 SendPort tim = spawnFunction(timIsolate); | 47 SendPort tim = spawnFunction(timIsolate); |
45 SendPort beth = spawnFunction(bethIsolate); | 48 SendPort beth = spawnFunction(bethIsolate); |
46 | 49 |
47 beth.send( | 50 beth.send( |
48 // because tim is created asynchronously, here we are sending an | 51 // because tim is created asynchronously, here we are sending an |
49 // unresolved port: | 52 // unresolved port: |
50 ['main says: Beth, find out if Tim is coming.', tim], | 53 ['main says: Beth, find out if Tim is coming.', tim], |
51 port.toSendPort()); | 54 port.toSendPort()); |
52 }); | 55 }); |
53 } | 56 } |
| 57 |
| 58 main() => baseTest(); |
OLD | NEW |