OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 // Dart test program for testing that heavy and light isolates can be mixed. | |
6 | |
7 #library('Mixed2Test'); | |
8 #import("dart:isolate"); | |
9 #import('../../lib/unittest/unittest.dart'); | |
10 | |
11 // We want to send a message from the main-isolate to a chain of different | |
12 // isolates and then get a reply back. | |
13 // In the following description heavy2 is not used, because it is shut down | |
14 // just after heaving created heavy2.light1,2 and light3. | |
15 // main-> | |
16 // heavy1->heavy1.light1->heavy1.light2->heavy1.light3-> | |
17 // heavy2.light1->heavy2.light2->heavy2.light3-> | |
18 // heavy3->heavy3.pong | |
19 | |
20 class LightRedirect extends Isolate { | |
21 LightRedirect() : super.light(); | |
22 | |
23 void main() { | |
24 this.port.receive((targetPort, ignored) { | |
25 this.port.receive((msg, replyTo) { | |
26 targetPort.send(msg + 1000, replyTo); | |
27 this.port.close(); | |
28 }); | |
29 }); | |
30 } | |
31 } | |
32 | |
33 class HeavyIsolate1 extends Isolate { | |
34 HeavyIsolate1() : super.heavy(); | |
35 | |
36 void main() { | |
37 Future<SendPort> light1 = new LightRedirect().spawn(); | |
38 Future<SendPort> light2 = new LightRedirect().spawn(); | |
39 Future<SendPort> light3 = new LightRedirect().spawn(); | |
40 | |
41 this.port.receive((SendPort heavy2Light1Port, ignored) { | |
42 light3.then((SendPort light3Port) { | |
43 light3Port.send(heavy2Light1Port, null); | |
44 light2.then((SendPort light2Port) { | |
45 light2Port.send(light3Port, null); | |
46 light1.then((SendPort light1Port) { | |
47 light1Port.send(light2Port, null); | |
48 // Next message we receive is the one that must go through the | |
49 // chain. | |
50 this.port.receive((msg, SendPort replyTo) { | |
51 light1Port.send(msg + 1, replyTo); | |
52 this.port.close(); | |
53 }); | |
54 }); | |
55 }); | |
56 }); | |
57 }); | |
58 } | |
59 } | |
60 | |
61 class HeavyIsolate2 extends Isolate { | |
62 HeavyIsolate2() : super.heavy(); | |
63 | |
64 void main() { | |
65 Future<SendPort> light1 = new LightRedirect().spawn(); | |
66 Future<SendPort> light2 = new LightRedirect().spawn(); | |
67 Future<SendPort> light3 = new LightRedirect().spawn(); | |
68 | |
69 this.port.receive((heavy3Port, replyWithLight1Port) { | |
70 light3.then((SendPort light3Port) { | |
71 light3Port.send(heavy3Port, null); | |
72 light2.then((SendPort light2Port) { | |
73 light2Port.send(light3Port, null); | |
74 light1.then((SendPort light1Port) { | |
75 light1Port.send(light2Port, null); | |
76 replyWithLight1Port.send(light1Port, null); | |
77 this.port.close(); | |
78 }); | |
79 }); | |
80 }); | |
81 }); | |
82 } | |
83 } | |
84 | |
85 class LightPong extends Isolate { | |
86 LightPong() : super.light(); | |
87 | |
88 void main() { | |
89 this.port.receive((msg, replyTo) { | |
90 replyTo.send(msg + 499, null); | |
91 this.port.close(); | |
92 }); | |
93 } | |
94 } | |
95 | |
96 class HeavyIsolate3 extends Isolate { | |
97 HeavyIsolate3() : super.heavy(); | |
98 | |
99 void main() { | |
100 Future<SendPort> pong = new LightPong().spawn(); | |
101 this.port.receive((msg, replyTo) { | |
102 pong.then((SendPort pongPort) { | |
103 pongPort.send(msg + 30, replyTo); | |
104 this.port.close(); | |
105 }); | |
106 }); | |
107 } | |
108 } | |
109 | |
110 | |
111 main() { | |
112 test("heavy and light isolates can be mixed", () { | |
113 Future<SendPort> heavy1 = new HeavyIsolate1().spawn(); | |
114 Future<SendPort> heavy2 = new HeavyIsolate2().spawn(); | |
115 Future<SendPort> heavy3 = new HeavyIsolate3().spawn(); | |
116 | |
117 heavy2.then(expectAsync1((SendPort heavy2Port) { | |
118 heavy3.then(expectAsync1((SendPort heavy3Port) { | |
119 heavy2Port.call(heavy3Port).then(expectAsync1((h2l1Port) { | |
120 heavy1.then(expectAsync1((SendPort heavy1Port) { | |
121 heavy1Port.send(h2l1Port, null); | |
122 // --------------- | |
123 // Setup complete. | |
124 // Start the chain-message. | |
125 heavy1Port.call(1).then(expectAsync1((result) { | |
126 Expect.equals(6531, result); | |
127 })); | |
128 })); | |
129 })); | |
130 })); | |
131 })); | |
132 }); | |
133 } | |
OLD | NEW |