| 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('../../../lib/unittest/unittest.dart'); | |
| 9 | 8 |
| 10 // This test does the following: | 9 // This test does the following: |
| 11 // - main spawns two isolates: 'tim' and 'beth' | 10 // - main spawns two isolates: 'tim' and 'beth' |
| 12 // - 'tim' spawns an isolate: 'bob' | 11 // - 'tim' spawns an isolate: 'bob' |
| 13 // - main starts a message chain: | 12 // - main starts a message chain: |
| 14 // main -> beth -> tim -> bob -> main | 13 // main -> beth -> tim -> bob -> main |
| 15 // by giving 'beth' a send-port to 'tim'. | 14 // by giving 'beth' a send-port to 'tim'. |
| 16 | 15 |
| 17 bethIsolate() { | 16 bethIsolate() { |
| 18 port.receive((msg, reply) => msg[1].send( | 17 port.receive((msg, reply) => msg[1].send( |
| 19 '${msg[0]}\nBeth says: Tim are you coming? And Bob?', reply)); | 18 '${msg[0]}\nBeth says: Tim are you coming? And Bob?', reply)); |
| 20 } | 19 } |
| 21 | 20 |
| 22 timIsolate() { | 21 timIsolate() { |
| 23 SendPort bob = spawnFunction(bobIsolate); | 22 SendPort bob = spawnFunction(bobIsolate); |
| 24 port.receive((msg, reply) => bob.send( | 23 port.receive((msg, reply) => bob.send( |
| 25 '$msg\nTim says: Can you tell "main" that we are all coming?', reply)); | 24 '$msg\nTim says: Can you tell "main" that we are all coming?', reply)); |
| 26 } | 25 } |
| 27 | 26 |
| 28 bobIsolate() { | 27 bobIsolate() { |
| 29 port.receive((msg, reply) => reply.send( | 28 port.receive((msg, reply) => reply.send( |
| 30 '$msg\nBob says: we are all coming!')); | 29 '$msg\nBob says: we are all coming!')); |
| 31 } | 30 } |
| 32 | 31 |
| 33 main() { | 32 main() { |
| 34 test('Message chain with unresolved ports', () { | 33 ReceivePort port = new ReceivePort(); |
| 35 ReceivePort port = new ReceivePort(); | 34 port.receive((msg, _) { |
| 36 port.receive(expectAsync2((msg, _) { | 35 Expect.equals('main says: Beth, find out if Tim is coming.' |
| 37 expect(msg).equals('main says: Beth, find out if Tim is coming.' | 36 + '\nBeth says: Tim are you coming? And Bob?' |
| 38 + '\nBeth says: Tim are you coming? And Bob?' | 37 + '\nTim says: Can you tell "main" that we are all coming?' |
| 39 + '\nTim says: Can you tell "main" that we are all coming?' | 38 + '\nBob says: we are all coming!', msg); |
| 40 + '\nBob says: we are all coming!'); | 39 port.close(); |
| 41 port.close(); | 40 }); |
| 42 })); | |
| 43 | 41 |
| 44 SendPort tim = spawnFunction(timIsolate); | 42 SendPort tim = spawnFunction(timIsolate); |
| 45 SendPort beth = spawnFunction(bethIsolate); | 43 SendPort beth = spawnFunction(bethIsolate); |
| 46 | 44 |
| 47 beth.send( | 45 beth.send( |
| 48 // because tim is created asynchronously, here we are sending an | 46 // because tim is created asynchronously, here we are sending an |
| 49 // unresolved port: | 47 // unresolved port: |
| 50 ['main says: Beth, find out if Tim is coming.', tim], | 48 ['main says: Beth, find out if Tim is coming.', tim], |
| 51 port.toSendPort()); | 49 port.toSendPort()); |
| 52 }); | |
| 53 } | 50 } |
| OLD | NEW |