| 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 isolate_unhandled_exception_test; | 5 library isolate_unhandled_exception_test; |
| 6 | 6 |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
| 10 | 10 |
| 11 // Tests that an isolate's keeps message handling working after | 11 // Tests that an isolate's keeps message handling working after |
| 12 // throwing an unhandled exception, if it was created with an | 12 // throwing an unhandled exception, if it was created with an |
| 13 // unhandled exception callback that returns true (continue handling). | 13 // unhandled exception callback that returns true (continue handling). |
| 14 // This test verifies that a callback function specified in | 14 // This test verifies that a callback function specified in |
| 15 // Isolate.spawnFunction is called. | 15 // Isolate.spawnFunction is called. |
| 16 | 16 |
| 17 // Note: this test will hang if an uncaught exception isn't handled, | 17 // Note: this test will hang if an uncaught exception isn't handled, |
| 18 // either by an error in the callback or it returning false. | 18 // either by an error in the callback or it returning false. |
| 19 | 19 |
| 20 void entry() { | 20 void entry() { |
| 21 port.receive((message, replyTo) { | 21 port.receive((message, replyTo) { |
| 22 if (message == 'throw exception') { | 22 if (message == 'throw exception') { |
| 23 replyTo.call('throwing exception'); | 23 replyTo.call('throwing exception'); |
| 24 throw new RuntimeError('ignore this exception'); | 24 throw new UnsupportedError('ignore this exception'); |
| 25 } | 25 } |
| 26 replyTo.call('hello'); | 26 replyTo.call('hello'); |
| 27 port.close(); | 27 port.close(); |
| 28 }); | 28 }); |
| 29 } | 29 } |
| 30 | 30 |
| 31 bool exceptionCallback(IsolateUnhandledException e) { | 31 bool exceptionCallback(IsolateUnhandledException e) { |
| 32 return e.source.message == 'ignore this exception'; | 32 return e.source.message == 'ignore this exception'; |
| 33 } | 33 } |
| 34 | 34 |
| 35 void main() { | 35 void main() { |
| 36 var isolate_port = spawnFunction(entry, exceptionCallback); | 36 var isolate_port = spawnFunction(entry, exceptionCallback); |
| 37 | 37 |
| 38 // Send a message that will cause an ignorable exception to be thrown. | 38 // Send a message that will cause an ignorable exception to be thrown. |
| 39 Future f = isolate_port.call('throw exception'); | 39 Future f = isolate_port.call('throw exception'); |
| 40 f.catchError((error) { | 40 f.catchError((error) { |
| 41 // Exception wasn't ignored as it was supposed to be. | 41 // Exception wasn't ignored as it was supposed to be. |
| 42 Expect.fail("Error not expected"); | 42 Expect.fail("Error not expected"); |
| 43 }); | 43 }); |
| 44 | 44 |
| 45 // Verify that isolate can still handle messages. | 45 // Verify that isolate can still handle messages. |
| 46 isolate_port.call('hi').then((value) { | 46 isolate_port.call('hi').then((value) { |
| 47 Expect.equals('hello', value); | 47 Expect.equals('hello', value); |
| 48 }, onError: (error) { | 48 }, onError: (error) { |
| 49 Expect.fail("Error not expected"); | 49 Expect.fail("Error not expected"); |
| 50 }); | 50 }); |
| 51 } | 51 } |
| OLD | NEW |