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