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 |
6 import 'dart:isolate'; | 7 import 'dart:isolate'; |
7 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
8 import "remote_unittest_helper.dart"; | 9 import "remote_unittest_helper.dart"; |
9 | 10 |
10 void countMessages(replyTo) { | 11 void countMessages(replyTo) { |
11 int count = 0; | 12 int count = 0; |
12 var port = new ReceivePort(); | 13 var port = new ReceivePort(); |
13 replyTo.send(["init", port.sendPort]); | 14 replyTo.send(["init", port.sendPort]); |
14 port.listen((int message) { | 15 port.listen((int message) { |
15 if (message == -1) { | 16 if (message == -1) { |
16 expect(count, 10); | 17 expect(count, 10); |
17 replyTo.send(["done"]); | 18 replyTo.send(["done"]); |
18 port.close(); | 19 port.close(); |
19 return; | 20 return; |
20 } | 21 } |
21 count++; | 22 count++; |
22 expect(message, count); | 23 expect(message, count); |
23 replyTo.send(["count", message * 2]); | 24 replyTo.send(["count", message * 2]); |
24 }); | 25 }); |
25 } | 26 } |
26 | 27 |
27 void main([args, port]) { | 28 void main([args, port]) { |
28 if (testRemote(main, port)) return; | 29 if (testRemote(main, port)) return; |
29 test("count 10 consecutive messages", () { | 30 test("count 10 consecutive messages", () { |
30 ReceivePort local = new ReceivePort(); | 31 ReceivePort local = new ReceivePort(); |
31 Isolate.spawn(countMessages, local.sendPort); | 32 Isolate.spawn(countMessages, local.sendPort); |
32 SendPort remote; | 33 SendPort remote; |
33 int count = 0; | 34 int count = 0; |
34 var done = expectAsync((){}); | 35 var done = expectAsync(() {}); |
35 local.listen((msg) { | 36 local.listen((msg) { |
36 switch (msg[0]) { | 37 switch (msg[0]) { |
37 case "init": | 38 case "init": |
38 expect(remote, null); | 39 expect(remote, null); |
39 remote = msg[1]; | 40 remote = msg[1]; |
40 remote.send(++count); | 41 remote.send(++count); |
41 break; | 42 break; |
42 case "count": | 43 case "count": |
43 expect(msg[1], count * 2); | 44 expect(msg[1], count * 2); |
44 if (count == 10) { | 45 if (count == 10) { |
45 remote.send(-1); | 46 remote.send(-1); |
46 } else { | 47 } else { |
47 remote.send(++count); | 48 remote.send(++count); |
48 } | 49 } |
49 break; | 50 break; |
50 case "done": | 51 case "done": |
51 expect(count, 10); | 52 expect(count, 10); |
52 local.close(); | 53 local.close(); |
53 done(); | 54 done(); |
54 break; | 55 break; |
55 default: | 56 default: |
56 fail("unreachable: ${msg[0]}"); | 57 fail("unreachable: ${msg[0]}"); |
57 } | 58 } |
58 }); | 59 }); |
59 }); | 60 }); |
60 } | 61 } |
OLD | NEW |