OLD | NEW |
1 // Copyright (c) 2011, 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('dart:isolate'); | 6 #import('dart:isolate'); |
7 #import('../../lib/unittest/unittest.dart'); | 7 #import('../../lib/unittest/unittest.dart'); |
8 | 8 |
9 class TestIsolate extends Isolate { | 9 void countMessages() { |
10 | 10 int count = 0; |
11 TestIsolate() : super(); | 11 port.receive((int message, SendPort replyTo) { |
12 | 12 if (message == -1) { |
13 void main() { | 13 Expect.equals(10, count); |
14 int count = 0; | 14 replyTo.send(-1, null); |
15 this.port.receive((int message, SendPort replyTo) { | 15 port.close(); |
16 if (message == -1) { | 16 return; |
17 Expect.equals(10, count); | 17 } |
18 replyTo.send(-1, null); | 18 Expect.equals(count, message); |
19 this.port.close(); | 19 count++; |
20 return; | 20 replyTo.send(message * 2, null); |
21 } | 21 }); |
22 | |
23 Expect.equals(count, message); | |
24 count++; | |
25 replyTo.send(message * 2, null); | |
26 }); | |
27 } | |
28 } | 22 } |
29 | 23 |
30 void main() { | 24 void main() { |
31 test("count 10 consecutive messages", () { | 25 test("count 10 consecutive messages", () { |
32 int count = 0; | 26 int count = 0; |
33 new TestIsolate().spawn().then(expectAsync1((SendPort remote) { | 27 SendPort remote = spawnFunction(countMessages); |
34 ReceivePort local = new ReceivePort(); | 28 ReceivePort local = new ReceivePort(); |
35 SendPort reply = local.toSendPort(); | 29 SendPort reply = local.toSendPort(); |
36 | 30 |
37 local.receive(expectAsync2((int message, SendPort replyTo) { | 31 local.receive(expectAsync2((int message, SendPort replyTo) { |
38 if (message == -1) { | 32 if (message == -1) { |
39 Expect.equals(11, count); | 33 Expect.equals(11, count); |
40 local.close(); | 34 local.close(); |
41 return; | 35 return; |
42 } | 36 } |
43 | 37 |
44 Expect.equals((count - 1) * 2, message); | 38 Expect.equals((count - 1) * 2, message); |
45 remote.send(count++, reply); | |
46 if (count == 10) { | |
47 remote.send(-1, reply); | |
48 } | |
49 }, count: 11)); | |
50 remote.send(count++, reply); | 39 remote.send(count++, reply); |
51 })); | 40 if (count == 10) { |
| 41 remote.send(-1, reply); |
| 42 } |
| 43 }, count: 11)); |
| 44 remote.send(count++, reply); |
52 }); | 45 }); |
53 } | 46 } |
OLD | NEW |