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 library CountTest; | 5 library CountTest; |
6 import '../../pkg/unittest/lib/unittest.dart'; | 6 import '../../pkg/unittest/lib/unittest.dart'; |
7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
8 | 8 |
9 void countMessages() { | 9 void countMessages(replyTo) { |
10 int count = 0; | 10 int count = 0; |
11 port.receive((int message, SendPort replyTo) { | 11 var port = new ReceivePort(); |
| 12 replyTo.send(["init", port.sendPort]); |
| 13 port.listen((int message) { |
12 if (message == -1) { | 14 if (message == -1) { |
13 expect(count, 10); | 15 expect(count, 10); |
14 replyTo.send(-1, null); | 16 replyTo.send(["done"]); |
15 port.close(); | 17 port.close(); |
16 return; | 18 return; |
17 } | 19 } |
| 20 count++; |
18 expect(message, count); | 21 expect(message, count); |
19 count++; | 22 replyTo.send(["count", message * 2]); |
20 replyTo.send(message * 2, null); | |
21 }); | 23 }); |
22 } | 24 } |
23 | 25 |
24 void main() { | 26 void main() { |
25 test("count 10 consecutive messages", () { | 27 test("count 10 consecutive messages", () { |
| 28 ReceivePort local = new ReceivePort(); |
| 29 Isolate.spawn(countMessages, local.sendPort); |
| 30 SendPort remote; |
26 int count = 0; | 31 int count = 0; |
27 SendPort remote = spawnFunction(countMessages); | 32 var done = expectAsync0((){}); |
28 ReceivePort local = new ReceivePort(); | 33 local.listen((msg) { |
29 SendPort reply = local.toSendPort(); | 34 switch (msg[0]) { |
30 | 35 case "init": |
31 local.receive(expectAsync2((int message, SendPort replyTo) { | 36 expect(remote, null); |
32 if (message == -1) { | 37 remote = msg[1]; |
33 // [count] is '11' because when we sent '9' to [remote], | 38 remote.send(++count); |
34 // the other isolate will send another message '18', that this | 39 break; |
35 // isolate will receive. Then this isolate will send '10' to | 40 case "count": |
36 // [remote] and increment [count]. Note that this last '10' | 41 expect(msg[1], count * 2); |
37 // message will not be received by the other isolate, since it | 42 if (count == 10) { |
38 // received '-1' before. | 43 remote.send(-1); |
39 expect(count, 11); | 44 } else { |
40 local.close(); | 45 remote.send(++count); |
41 return; | 46 } |
| 47 break; |
| 48 case "done": |
| 49 expect(count, 10); |
| 50 local.close(); |
| 51 done(); |
| 52 break; |
| 53 default: |
| 54 fail("unreachable: ${msg[0]}"); |
42 } | 55 } |
43 | 56 }); |
44 expect(message, (count - 1) * 2); | |
45 remote.send(count++, reply); | |
46 if (count == 10) { | |
47 remote.send(-1, reply); | |
48 } | |
49 }, count: 11)); | |
50 remote.send(count++, reply); | |
51 }); | 57 }); |
52 } | 58 } |
OLD | NEW |