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:json'; | 5 import 'dart:json'; |
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() { |
11 bool wasThrown = false; | 11 bool wasThrown = false; |
12 try { | 12 try { |
13 window.alert('Test'); | 13 window.alert('Test'); |
14 } catch (e) { | 14 } catch (e) { |
15 wasThrown = true; | 15 wasThrown = true; |
16 } | 16 } |
17 // 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. |
18 if (!wasThrown) { | 18 if (!wasThrown) { |
19 return; | 19 return; |
20 } | 20 } |
21 | 21 |
22 // Check that JSON library was loaded to isolate. | 22 // Check that JSON library was loaded to isolate. |
23 JSON.stringify([1, 2, 3]); | 23 stringify([1, 2, 3]); |
24 | 24 |
25 isolate.port.receive((message, replyTo) { | 25 isolate.port.receive((message, replyTo) { |
26 replyTo.send(responseFor(message), null); | 26 replyTo.send(responseFor(message), null); |
27 }); | 27 }); |
28 } | 28 } |
29 | 29 |
30 main() { | 30 main() { |
31 useHtmlConfiguration(); | 31 useHtmlConfiguration(); |
32 test('IsolateSpawn', () { | 32 test('IsolateSpawn', () { |
33 isolate.spawnFunction(isolateEntry); | 33 isolate.spawnFunction(isolateEntry); |
34 }); | 34 }); |
35 test('NonDOMIsolates', () { | 35 test('NonDOMIsolates', () { |
36 var callback = expectAsync0((){}); | 36 var callback = expectAsync0((){}); |
37 var port = isolate.spawnFunction(isolateEntry); | 37 var port = isolate.spawnFunction(isolateEntry); |
38 final msg1 = 'foo'; | 38 final msg1 = 'foo'; |
39 final msg2 = 'bar'; | 39 final msg2 = 'bar'; |
40 port.call(msg1).then((response) { | 40 port.call(msg1).then((response) { |
41 guardAsync(() { | 41 guardAsync(() { |
42 expect(response, equals(responseFor(msg1))); | 42 expect(response, equals(responseFor(msg1))); |
43 port.call(msg2).then((response) { | 43 port.call(msg2).then((response) { |
44 guardAsync(() { | 44 guardAsync(() { |
45 expect(response, equals(responseFor(msg2))); | 45 expect(response, equals(responseFor(msg2))); |
46 callback(); | 46 callback(); |
47 }); | 47 }); |
48 }); | 48 }); |
49 }); | 49 }); |
50 }); | 50 }); |
51 }); | 51 }); |
52 } | 52 } |
OLD | NEW |