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