| OLD | NEW |
| 1 library IsolatesTest; | 1 library IsolatesTest; |
| 2 import '../../pkg/unittest/lib/unittest.dart'; | 2 import '../../pkg/unittest/lib/unittest.dart'; |
| 3 import '../../pkg/unittest/lib/html_config.dart'; | 3 import '../../pkg/unittest/lib/html_config.dart'; |
| 4 import 'dart:html'; | 4 import 'dart:html'; |
| 5 import 'dart:convert'; | 5 import 'dart:convert'; |
| 6 import 'dart:isolate' as isolate; | 6 import 'dart:isolate' as isolate; |
| 7 | 7 |
| 8 String responseFor(message) => 'response for $message'; | 8 String responseFor(message) => 'response for $message'; |
| 9 | 9 |
| 10 void isolateEntry() { | 10 void isolateEntry(isolate.SendPort initialReplyTo) { |
| 11 var port = new isolate.ReceivePort(); |
| 12 initialReplyTo.send(port.sendPort); |
| 13 |
| 11 bool wasThrown = false; | 14 bool wasThrown = false; |
| 12 try { | 15 try { |
| 13 window.alert('Test'); | 16 window.alert('Test'); |
| 14 } catch (e) { | 17 } catch (e) { |
| 15 wasThrown = true; | 18 wasThrown = true; |
| 16 } | 19 } |
| 17 // If wasn't thrown, do not listen to messages to make test fail. | 20 // If wasn't thrown, do not listen to messages to make test fail. |
| 18 if (!wasThrown) { | 21 if (!wasThrown) { |
| 19 return; | 22 return; |
| 20 } | 23 } |
| 21 | 24 |
| 22 // Check that convert library was loaded to isolate. | 25 // Check that convert library was loaded to isolate. |
| 23 JSON.encode([1, 2, 3]); | 26 JSON.encode([1, 2, 3]); |
| 24 | 27 |
| 25 isolate.port.receive((message, replyTo) { | 28 port.listen((message) { |
| 26 replyTo.send(responseFor(message), null); | 29 var data = message[0]; |
| 30 var replyTo = message[1]; |
| 31 replyTo.send(responseFor(data)); |
| 27 }); | 32 }); |
| 28 } | 33 } |
| 29 | 34 |
| 35 Future sendReceive(isolate.SendPort port, msg) { |
| 36 var response = new isolate.ReceivePort(); |
| 37 port.send([msg, response.sendPort]); |
| 38 return response.first; |
| 39 } |
| 40 |
| 30 main() { | 41 main() { |
| 31 useHtmlConfiguration(); | 42 useHtmlConfiguration(); |
| 32 test('IsolateSpawn', () { | 43 test('IsolateSpawn', () { |
| 33 isolate.spawnFunction(isolateEntry); | 44 var port = new isolate.ReceivePort(); |
| 45 isolate.Isolate.spawn(isolateEntry, port.sendPort); |
| 46 port.close(); |
| 34 }); | 47 }); |
| 35 test('NonDOMIsolates', () { | 48 test('NonDOMIsolates', () { |
| 36 var callback = expectAsync0((){}); | 49 var callback = expectAsync0((){}); |
| 37 var port = isolate.spawnFunction(isolateEntry); | 50 var response = new isolate.ReceivePort(); |
| 38 final msg1 = 'foo'; | 51 var remote = isolate.Isolate.spawn(isolateEntry, response.sendPort); |
| 39 final msg2 = 'bar'; | 52 response.first.then((port) { |
| 40 port.call(msg1).then((response) { | 53 final msg1 = 'foo'; |
| 41 guardAsync(() { | 54 final msg2 = 'bar'; |
| 42 expect(response, equals(responseFor(msg1))); | 55 sendReceive(port, msg1).then((response) { |
| 43 port.call(msg2).then((response) { | 56 guardAsync(() { |
| 44 guardAsync(() { | 57 expect(response, equals(responseFor(msg1))); |
| 45 expect(response, equals(responseFor(msg2))); | 58 sendReceive(port, msg2).then((response) { |
| 46 callback(); | 59 guardAsync(() { |
| 60 expect(response, equals(responseFor(msg2))); |
| 61 callback(); |
| 62 }); |
| 47 }); | 63 }); |
| 48 }); | 64 }); |
| 49 }); | 65 }); |
| 50 }); | 66 }); |
| 51 }); | 67 }); |
| 52 } | 68 } |
| OLD | NEW |