Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(425)

Side by Side Diff: tests/isolate/handle_error2_test.dart

Issue 169703002: Add an error listener on isolates. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rewrite. Now catch all in IsolateContext.eval Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
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.
4
5 library handle_error_test;
6
7 import "dart:isolate";
8 import "dart:async";
9 import "package:async_helper/async_helper.dart";
10 import "package:expect/expect.dart";
11
12 isomain1(replyPort) {
13 RawReceivePort port = new RawReceivePort();
14 port.handler = (v) {
15 switch (v) {
16 case 0:
17 replyPort.send(42);
18 break;
19 case 1:
20 throw new ArgumentError("whoops");
21 case 2:
22 throw new RangeError.value(37);
23 case 3:
24 port.close();
25 }
26 };
27 replyPort.send(port.sendPort);
28 }
29
30 Future spawn(entry) {
31 ReceivePort reply = new ReceivePort();
32 Future isolate = Isolate.spawn(entry, reply.sendPort, paused: true);
33 return isolate.then((Isolate isolate) {
34 isolate.setErrorsFatal(false);
35 isolate.resume(isolate.pauseCapability);
36 Future result = reply.first.then((sendPort) {
37 return [isolate, sendPort];
38 });
39 return result;
40 });
41 }
42
43 main(){
44 asyncStart();
45 RawReceivePort reply = new RawReceivePort(null);
46 RawReceivePort reply2 = new RawReceivePort(null);
47 // Start paused so we have time to set up the error handler.
floitsch 2014/05/15 12:15:25 Comment is at wrong place. Add comment here that
Lasse Reichstein Nielsen 2014/05/19 11:13:37 Will do.
48 Future iso1 = spawn(isomain1);
49 Future iso2 = spawn(isomain1);
50 Future.wait([iso1, iso2]).then((l) {
51 var isolate1 = l[0][0];
52 var sendPort1 = l[0][1];
53 var isolate2 = l[1][0];
54 var sendPort2 = l[1][1];
55 Stream errors = isolate1.errors; // Broadcast stream, never a done message.
56 int state = 1;
57 var subscription;
58 subscription = errors.listen(null, onError: (error, stack) {
59 switch (state) {
60 case 1:
61 Expect.equals(new ArgumentError("whoops").toString(), "$error");
62 state++;
63 break;
64 case 2:
65 Expect.equals(new RangeError.value(37).toString(), "$error");
66 state++;
67 reply.close();
68 subscription.cancel();
69 asyncEnd();
70 break;
71 default:
72 throw "Bad state for error: $state: $error";
73 }
74 });
75 sendPort1.send(0);
76 sendPort2.send(0);
77 sendPort1.send(1);
78 sendPort2.send(1);
79 sendPort1.send(2);
80 sendPort2.send(2);
81 sendPort1.send(3);
82 sendPort2.send(3);
83 });
84 }
85
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698