OLD | NEW |
1 // Copyright (c) 2011, 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 #import("dart:isolate"); | 5 #import("dart:isolate"); |
6 | 6 |
7 class Fields { | 7 class Fields { |
8 Fields(int i, int j) : fld1 = i, fld2 = j, fld5 = true {} | 8 Fields(int i, int j) : fld1 = i, fld2 = j, fld5 = true {} |
9 int fld1; | 9 int fld1; |
10 final int fld2; | 10 final int fld2; |
11 static int fld3; | 11 static int fld3; |
(...skipping 1082 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1094 } | 1094 } |
1095 | 1095 |
1096 class PingPongGame { | 1096 class PingPongGame { |
1097 | 1097 |
1098 PingPongGame.play() | 1098 PingPongGame.play() |
1099 : _ping = new ReceivePort(), | 1099 : _ping = new ReceivePort(), |
1100 _pingPort = _ping.toSendPort(), | 1100 _pingPort = _ping.toSendPort(), |
1101 _pong = null, | 1101 _pong = null, |
1102 _warmedup = false, | 1102 _warmedup = false, |
1103 _iterations = 0 { | 1103 _iterations = 0 { |
1104 new Pong().spawn().then((SendPort port) { | 1104 SendPort _pong = spawnFunction(pong); |
1105 _pong = port; | 1105 play(); |
1106 play(); | |
1107 }); | |
1108 } | 1106 } |
1109 | 1107 |
1110 void startRound() { | 1108 void startRound() { |
1111 _iterations++; | 1109 _iterations++; |
1112 _pong.send(Benchmark1.INIT_MESSAGE, _pingPort); | 1110 _pong.send(Benchmark1.INIT_MESSAGE, _pingPort); |
1113 } | 1111 } |
1114 | 1112 |
1115 void evaluateRound() { | 1113 void evaluateRound() { |
1116 int time = (new Date.now().difference(_start)).inMilliseconds; | 1114 int time = (new Date.now().difference(_start)).inMilliseconds; |
1117 if (!_warmedup && time < Benchmark1.WARMUP_TIME) { | 1115 if (!_warmedup && time < Benchmark1.WARMUP_TIME) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1152 } | 1150 } |
1153 | 1151 |
1154 Date _start; | 1152 Date _start; |
1155 SendPort _pong; | 1153 SendPort _pong; |
1156 SendPort _pingPort; | 1154 SendPort _pingPort; |
1157 ReceivePort _ping; | 1155 ReceivePort _ping; |
1158 bool _warmedup; | 1156 bool _warmedup; |
1159 int _iterations; | 1157 int _iterations; |
1160 } | 1158 } |
1161 | 1159 |
1162 class Pong extends Isolate { | 1160 void pong() { |
1163 | 1161 port.receive((message, SendPort replyTo) { |
1164 // TODO(hpayer): can be removed as soon as we have default constructors | 1162 if (message == Benchmark1.INIT_MESSAGE) { |
1165 Pong() : super() { } | 1163 replyTo.send(message, null); |
1166 | 1164 } else if (message == Benchmark1.TERMINATION_MESSAGE) { |
1167 void main() { | 1165 port.close(); |
1168 this.port.receive((message, SendPort replyTo) { | 1166 } else { |
1169 if (message == Benchmark1.INIT_MESSAGE) { | 1167 replyTo.send(message, null); |
1170 _reply = replyTo; | 1168 } |
1171 _reply.send(message, null); | 1169 }); |
1172 } else if (message == Benchmark1.TERMINATION_MESSAGE) { | |
1173 this.port.close(); | |
1174 } else { | |
1175 _reply.send(message, null); | |
1176 } | |
1177 }); | |
1178 } | |
1179 | |
1180 SendPort _reply; | |
1181 } | 1170 } |
1182 | 1171 |
1183 | 1172 |
1184 class ManyGenericInstanceofTest { | 1173 class ManyGenericInstanceofTest { |
1185 static testMain() { | 1174 static testMain() { |
1186 for (int i = 0; i < 5000; i++) { | 1175 for (int i = 0; i < 5000; i++) { |
1187 GenericInstanceof.testMain(); | 1176 GenericInstanceof.testMain(); |
1188 } | 1177 } |
1189 } | 1178 } |
1190 } | 1179 } |
1191 | 1180 |
1192 // Dart test program for testing that exceptions in other isolates bring down | |
1193 // the program. | |
1194 // ImportOptions=IsolateTestFramework | |
1195 | |
1196 class Isolate2NegativeTest extends Isolate { | |
1197 Isolate2NegativeTest() : super(); | |
1198 | |
1199 static void testMain() { | |
1200 // We will never call 'done'. This test fails with a timeout. | |
1201 IsolateTestFramework.waitForDone(); | |
1202 new Isolate2NegativeTest().spawn(); | |
1203 } | |
1204 | |
1205 void main() { | |
1206 throw "foo"; | |
1207 } | |
1208 } | |
1209 | |
1210 | |
1211 class IsolateTestFramework { | |
1212 static void waitForDone() { | |
1213 // By keeping an open receive port we keep the isolate alive. This way we | |
1214 // can wait for other isolates to finish before terminating the main | |
1215 // isolate. | |
1216 _port = new ReceivePort(); | |
1217 if (waitForDoneCallback !== null) waitForDoneCallback(); | |
1218 } | |
1219 static void done() { | |
1220 _port.close(); | |
1221 if (doneCallback !== null) doneCallback(); | |
1222 } | |
1223 | |
1224 static Function waitForDoneCallback; | |
1225 static Function doneCallback; | |
1226 static ReceivePort _port; | |
1227 } | |
1228 | |
1229 // Dart test program for testing that the exit-handler is executed at the end. | |
1230 | |
1231 | |
1232 class IsolateExitHandlerTest extends Isolate { | |
1233 static int counter; | |
1234 | |
1235 IsolateExitHandlerTest() : super.heavy(); | |
1236 | |
1237 static void testMain() { | |
1238 Isolate.setExitHandler(() { | |
1239 Expect.equals(1, counter); | |
1240 }); | |
1241 new IsolateExitHandlerTest().spawn().then((SendPort p) { | |
1242 p.call("bar").then((msg) { | |
1243 counter++; | |
1244 }); | |
1245 }); | |
1246 } | |
1247 | |
1248 void main() { | |
1249 bool encounteredException = false; | |
1250 try { | |
1251 Isolate.setExitHandler(() { | |
1252 Expect.equals(true, false); | |
1253 }); | |
1254 } catch(var e) { | |
1255 encounteredException = true; | |
1256 } | |
1257 Expect.equals(true, encounteredException); | |
1258 | |
1259 this.port.receive((ignored, replyTo) { | |
1260 replyTo.send("foo", null); | |
1261 this.port.close(); | |
1262 }); | |
1263 } | |
1264 } | |
1265 | |
1266 | |
1267 // --------------------------------------------------------------------------- | 1181 // --------------------------------------------------------------------------- |
1268 // THE REST OF THIS FILE COULD BE AUTOGENERATED | 1182 // THE REST OF THIS FILE COULD BE AUTOGENERATED |
1269 // --------------------------------------------------------------------------- | 1183 // --------------------------------------------------------------------------- |
1270 | 1184 |
| 1185 // --------------------------------------------------------------------------- |
| 1186 // tests/isolate/spawn_test.dart |
| 1187 // --------------------------------------------------------------------------- |
1271 | 1188 |
1272 // ImportOptions=IsolateTestFramework | 1189 spawn_test_main() { |
1273 | 1190 test("spawn a new isolate", () { |
1274 class SpawnTest { | 1191 SendPort port = spawnFunction(entry); |
1275 | 1192 port.call(42).then(expectAsync1((message) { |
1276 static void testMain() { | 1193 Expect.equals(42, message); |
1277 spawnIsolate(); | 1194 })); |
1278 } | 1195 }); |
1279 | |
1280 static void spawnIsolate() { | |
1281 IsolateTestFramework.waitForDone(); | |
1282 SpawnedIsolate isolate = new SpawnedIsolate(); | |
1283 isolate.spawn().then((SendPort port) { | |
1284 port.call(42).then((message) { | |
1285 Expect.equals(42, message); | |
1286 IsolateTestFramework.done(); | |
1287 }); | |
1288 }); | |
1289 } | |
1290 } | 1196 } |
1291 | 1197 |
1292 class SpawnedIsolate extends Isolate { | 1198 void entry() { |
1293 | 1199 port.receive((message, SendPort replyTo) { |
1294 SpawnedIsolate() : super() { } | 1200 Expect.equals(42, message); |
1295 | 1201 replyTo.send(42, null); |
1296 void main() { | 1202 port.close(); |
1297 this.port.receive((message, SendPort replyTo) { | 1203 }); |
1298 Expect.equals(42, message); | |
1299 replyTo.send(42, null); | |
1300 this.port.close(); | |
1301 }); | |
1302 } | |
1303 | |
1304 } | 1204 } |
1305 | 1205 |
1306 // Dart test program for testing that the exit-handler is executed. | 1206 // --------------------------------------------------------------------------- |
| 1207 // tests/isolate/isolate_negative_test.dart |
| 1208 // --------------------------------------------------------------------------- |
1307 | 1209 |
1308 | 1210 void isolate_negative_entry() { |
1309 class IsolateExitHandlerNegativeTest extends Isolate { | 1211 port.receive((ignored, replyTo) { |
1310 IsolateExitHandlerNegativeTest() : super.heavy(); | 1212 replyTo.send("foo", null); |
1311 | 1213 }); |
1312 static void testMain() { | |
1313 Isolate.setExitHandler(() { | |
1314 Expect.equals(true, false); // <=-------- Should fail here. | |
1315 }); | |
1316 new IsolateExitHandlerNegativeTest().spawn(); | |
1317 } | |
1318 | |
1319 void main() { | |
1320 this.port.close(); | |
1321 } | |
1322 } | 1214 } |
1323 | 1215 |
1324 // ImportOptions=IsolateTestFramework | 1216 isolate_negative_test_main() { |
1325 | 1217 test("ensure isolate code is executed", () { |
1326 class ConstructorTest extends Isolate { | 1218 SendPort port = spawnFunction(isolate_negative_entry); |
1327 final int field; | 1219 port.call("foo").then(expectAsync1((message) { |
1328 ConstructorTest() : super(), field = 499; | 1220 Expect.equals(true, "Expected fail"); // <=-------- Should fail here. |
1329 | 1221 })); |
1330 void main() { | 1222 }); |
1331 IsolateTestFramework.waitForDone(); | |
1332 this.port.receive((ignoredMessage, reply) { | |
1333 reply.send(field, null); | |
1334 this.port.close(); | |
1335 IsolateTestFramework.done(); | |
1336 }); | |
1337 } | |
1338 | |
1339 static void testMain() { | |
1340 ConstructorTest test = new ConstructorTest(); | |
1341 test.spawn().then((SendPort port) { | |
1342 port.call("ignored").then((message) { | |
1343 Expect.equals(499, message); | |
1344 }); | |
1345 }); | |
1346 } | |
1347 } | 1223 } |
1348 | 1224 |
1349 // Dart test program for testing isolate communication with | |
1350 // simple messages. | |
1351 // ImportOptions=IsolateTestFramework | |
1352 | |
1353 class IsolateTest { | |
1354 | |
1355 static void testMain() { | |
1356 _waitingForDoneCount = 0; | |
1357 IsolateTestFramework.waitForDone(); | |
1358 RequestReplyTest.test(); | |
1359 CountTest.test(); | |
1360 StaticStateTest.test(); | |
1361 } | |
1362 | |
1363 static void waitForDone() { | |
1364 _waitingForDoneCount++; | |
1365 } | |
1366 | |
1367 static void done() { | |
1368 if (--_waitingForDoneCount == 0) { | |
1369 IsolateTestFramework.done(); | |
1370 } | |
1371 } | |
1372 | |
1373 static int _waitingForDoneCount; | |
1374 } | |
1375 | |
1376 | |
1377 // --------------------------------------------------------------------------- | 1225 // --------------------------------------------------------------------------- |
1378 // Request-reply test. | 1226 // tests/isolate/message_test.dart |
1379 // --------------------------------------------------------------------------- | 1227 // --------------------------------------------------------------------------- |
1380 | 1228 |
1381 class RequestReplyTest { | |
1382 | |
1383 static void test() { | |
1384 testCall(); | |
1385 testSend(); | |
1386 } | |
1387 | |
1388 static void testCall() { | |
1389 IsolateTest.waitForDone(); | |
1390 new RequestReplyIsolate().spawn().then((SendPort port) { | |
1391 port.call(42).then((message) { | |
1392 Expect.equals(42 + 87, message); | |
1393 IsolateTest.done(); | |
1394 }); | |
1395 }); | |
1396 } | |
1397 | |
1398 static void testSend() { | |
1399 IsolateTest.waitForDone(); | |
1400 new RequestReplyIsolate().spawn().then((SendPort port) { | |
1401 ReceivePort reply = new ReceivePort(); | |
1402 port.send(99, reply.toSendPort()); | |
1403 reply.receive((message, replyTo) { | |
1404 Expect.equals(99 + 87, message); | |
1405 reply.close(); | |
1406 IsolateTest.done(); | |
1407 }); | |
1408 }); | |
1409 } | |
1410 } | |
1411 | |
1412 | |
1413 class RequestReplyIsolate extends Isolate { | |
1414 | |
1415 RequestReplyIsolate() : super() { } | |
1416 | |
1417 void main() { | |
1418 this.port.receive((message, SendPort replyTo) { | |
1419 replyTo.send(message + 87, null); | |
1420 this.port.close(); | |
1421 }); | |
1422 } | |
1423 | |
1424 } | |
1425 | |
1426 | |
1427 // --------------------------------------------------------------------------- | |
1428 // Simple counting test. | |
1429 // --------------------------------------------------------------------------- | |
1430 | |
1431 class CountTest { | |
1432 | |
1433 static int count; | |
1434 | |
1435 static void test() { | |
1436 IsolateTest.waitForDone(); | |
1437 print("Hello "); | |
1438 new CountIsolate().spawn().then((SendPort remote) { | |
1439 ReceivePort local = new ReceivePort(); | |
1440 SendPort reply = local.toSendPort(); | |
1441 | |
1442 local.receive((int message, SendPort replyTo) { | |
1443 if (message == -1) { | |
1444 Expect.equals(11, count); | |
1445 // Close the only ReceivePort to terminate the isolate after the | |
1446 // callback returns. | |
1447 local.close(); | |
1448 print("IsolateTest exiting."); | |
1449 IsolateTest.done(); | |
1450 return; | |
1451 } | |
1452 Expect.equals((count - 1) * 2, message); | |
1453 print("IsolateTest: $message"); | |
1454 remote.send(count++, reply); | |
1455 if (count == 10) { | |
1456 remote.send(-1, reply); | |
1457 } | |
1458 }); | |
1459 | |
1460 print("!"); | |
1461 count = 0; | |
1462 remote.send(count++, reply); | |
1463 }); | |
1464 } | |
1465 } | |
1466 | |
1467 | |
1468 class CountIsolate extends Isolate { | |
1469 | |
1470 CountIsolate() : super() { } | |
1471 | |
1472 void main() { | |
1473 print("World"); | |
1474 int count = 0; | |
1475 | |
1476 this.port.receive((int message, SendPort replyTo) { | |
1477 print("Remote: $message"); | |
1478 if (message == -1) { | |
1479 Expect.equals(10, count); | |
1480 replyTo.send(-1, null); | |
1481 // Close the only ReceivePort to terminate the isolate after the | |
1482 // callback returns. | |
1483 this.port.close(); | |
1484 print("RemoteRunner exiting."); | |
1485 return; | |
1486 } | |
1487 | |
1488 Expect.equals(count, message); | |
1489 count++; | |
1490 replyTo.send(message * 2, null); | |
1491 }); | |
1492 } | |
1493 } | |
1494 | |
1495 | |
1496 | |
1497 // --------------------------------------------------------------------------- | |
1498 // State state test. | |
1499 // --------------------------------------------------------------------------- | |
1500 | |
1501 class StaticStateTest { | |
1502 | |
1503 static String state; | |
1504 | |
1505 static void test() { | |
1506 IsolateTest.waitForDone(); | |
1507 Expect.equals(null, state); | |
1508 state = "foo"; | |
1509 Expect.equals("foo", state); | |
1510 | |
1511 new StaticStateIsolate().spawn().then((SendPort remote) { | |
1512 remote.call("bar").then((reply) { | |
1513 Expect.equals("foo", state); | |
1514 Expect.equals(null, reply); | |
1515 | |
1516 state = "baz"; | |
1517 remote.call("exit").then((reply) { | |
1518 Expect.equals("baz", state); | |
1519 Expect.equals("bar", reply); | |
1520 IsolateTest.done(); | |
1521 }); | |
1522 }); | |
1523 }); | |
1524 } | |
1525 | |
1526 } | |
1527 | |
1528 | |
1529 class StaticStateIsolate extends Isolate { | |
1530 | |
1531 StaticStateIsolate() : super() { } | |
1532 | |
1533 void main() { | |
1534 Expect.equals(null, StaticStateTest.state); | |
1535 this.port.receive((var message, SendPort replyTo) { | |
1536 String old = StaticStateTest.state; | |
1537 StaticStateTest.state = message; | |
1538 replyTo.send(old, null); | |
1539 if (message == "exit") { | |
1540 this.port.close(); | |
1541 return; | |
1542 } | |
1543 }); | |
1544 } | |
1545 | |
1546 } | |
1547 | |
1548 | |
1549 // Dart test program for testing isolate communication with | |
1550 // complex messages. | |
1551 // ImportOptions=IsolateTestFramework | |
1552 | |
1553 | |
1554 class IsolateComplexMessagesTest { | |
1555 | |
1556 static void testMain() { | |
1557 LogClient.test(); | |
1558 } | |
1559 } | |
1560 | |
1561 | |
1562 // --------------------------------------------------------------------------- | |
1563 // Log server test. | |
1564 // --------------------------------------------------------------------------- | |
1565 | |
1566 class LogClient { | |
1567 static void test() { | |
1568 IsolateTestFramework.waitForDone(); | |
1569 new LogIsolate().spawn().then((SendPort remote) { | |
1570 | |
1571 remote.send(1, null); | |
1572 remote.send("Hello", null); | |
1573 remote.send("World", null); | |
1574 remote.send(const [null, 1, 2, 3, 4], null); | |
1575 remote.send(const [1, 2.0, true, false, 0xffffffffff], null); | |
1576 remote.send(const ["Hello", "World", 0xffffffffff], null); | |
1577 // Shutdown the LogRunner. | |
1578 remote.call(-1).then((int message) { | |
1579 Expect.equals(6, message); | |
1580 }); | |
1581 IsolateTestFramework.done(); | |
1582 }); | |
1583 } | |
1584 } | |
1585 | |
1586 | |
1587 class LogIsolate extends Isolate { | |
1588 LogIsolate() : super() { } | |
1589 | |
1590 void main() { | |
1591 print("Starting log server."); | |
1592 | |
1593 int count = 0; | |
1594 | |
1595 this.port.receive((var message, SendPort replyTo) { | |
1596 if (message == -1) { | |
1597 this.port.close(); | |
1598 print("Stopping log server."); | |
1599 replyTo.send(count, null); | |
1600 } else { | |
1601 print("Log ($count) $message"); | |
1602 switch (count) { | |
1603 case 0: | |
1604 Expect.equals(1, message); | |
1605 break; | |
1606 case 1: | |
1607 Expect.equals("Hello", message); | |
1608 break; | |
1609 case 2: | |
1610 Expect.equals("World", message); | |
1611 break; | |
1612 case 3: | |
1613 Expect.equals(5, message.length); | |
1614 Expect.equals(null, message[0]); | |
1615 Expect.equals(1, message[1]); | |
1616 Expect.equals(2, message[2]); | |
1617 Expect.equals(3, message[3]); | |
1618 Expect.equals(4, message[4]); | |
1619 break; | |
1620 case 4: | |
1621 Expect.equals(5, message.length); | |
1622 Expect.equals(1, message[0]); | |
1623 Expect.equals(2.0, message[1]); | |
1624 Expect.equals(true, message[2]); | |
1625 Expect.equals(false, message[3]); | |
1626 Expect.equals(0xffffffffff, message[4]); | |
1627 break; | |
1628 case 5: | |
1629 Expect.equals(3, message.length); | |
1630 Expect.equals("Hello", message[0]); | |
1631 Expect.equals("World", message[1]); | |
1632 Expect.equals(0xffffffffff, message[2]); | |
1633 break; | |
1634 } | |
1635 count++; | |
1636 } | |
1637 }); | |
1638 } | |
1639 } | |
1640 | |
1641 | |
1642 // Dart test program for testing serialization of messages. | |
1643 // VMOptions=--enable_type_checks --enable_asserts | |
1644 // ImportOptions=IsolateTestFramework | |
1645 | |
1646 // --------------------------------------------------------------------------- | 1229 // --------------------------------------------------------------------------- |
1647 // Message passing test. | 1230 // Message passing test. |
1648 // --------------------------------------------------------------------------- | 1231 // --------------------------------------------------------------------------- |
1649 | 1232 |
1650 class MessageTest { | 1233 class MessageTest { |
1651 static void testMain() { | |
1652 PingPongClient.test(); | |
1653 } | |
1654 | |
1655 static final List list1 = const ["Hello", "World", "Hello", 0xfffffffffff]; | 1234 static final List list1 = const ["Hello", "World", "Hello", 0xfffffffffff]; |
1656 static final List list2 = const [null, list1, list1, list1, list1]; | 1235 static final List list2 = const [null, list1, list1, list1, list1]; |
1657 static final List list3 = const [list2, 2.0, true, false, 0xfffffffffff]; | 1236 static final List list3 = const [list2, 2.0, true, false, 0xfffffffffff]; |
1658 static final Map map1 = const { | 1237 static final Map map1 = const { |
1659 "a=1" : 1, "b=2" : 2, "c=3" : 3, | 1238 "a=1" : 1, "b=2" : 2, "c=3" : 3, |
1660 }; | 1239 }; |
1661 static final Map map2 = const { | 1240 static final Map map2 = const { |
1662 "list1" : list1, "list2" : list2, "list3" : list3, | 1241 "list1" : list1, "list2" : list2, "list3" : list3, |
1663 }; | 1242 }; |
1664 static final List list4 = const [map1, map2]; | 1243 static final List list4 = const [map1, map2]; |
(...skipping 29 matching lines...) Expand all Loading... |
1694 | 1273 |
1695 static void VerifyObject(int index, var actual) { | 1274 static void VerifyObject(int index, var actual) { |
1696 var expected = elms[index]; | 1275 var expected = elms[index]; |
1697 Expect.equals(true, expected is List); | 1276 Expect.equals(true, expected is List); |
1698 Expect.equals(true, actual is List); | 1277 Expect.equals(true, actual is List); |
1699 Expect.equals(expected.length, actual.length); | 1278 Expect.equals(expected.length, actual.length); |
1700 VerifyList(expected, actual); | 1279 VerifyList(expected, actual); |
1701 } | 1280 } |
1702 } | 1281 } |
1703 | 1282 |
1704 class PingPongClient { | 1283 pingPong() { |
1705 static void test() { | 1284 int count = 0; |
1706 IsolateTestFramework.waitForDone(); | 1285 port.receive((var message, SendPort replyTo) { |
1707 new PingPongServer().spawn().then((SendPort remote) { | 1286 if (message == -1) { |
| 1287 port.close(); |
| 1288 replyTo.send(count, null); |
| 1289 } else { |
| 1290 // Check if the received object is correct. |
| 1291 if (count < MessageTest.elms.length) { |
| 1292 MessageTest.VerifyObject(count, message); |
| 1293 } |
| 1294 // Bounce the received object back so that the sender |
| 1295 // can make sure that the object matches. |
| 1296 replyTo.send(message, null); |
| 1297 count++; |
| 1298 } |
| 1299 }); |
| 1300 } |
1708 | 1301 |
1709 // Send objects and receive them back. | 1302 message_test_main() { |
1710 for (int i = 0; i < MessageTest.elms.length; i++) { | 1303 test("send objects and receive them back", () { |
1711 var sentObject = MessageTest.elms[i]; | 1304 SendPort remote = spawnFunction(pingPong); |
1712 remote.call(sentObject).then((var receivedObject) { | 1305 // Send objects and receive them back. |
1713 MessageTest.VerifyObject(i, receivedObject); | 1306 for (int i = 0; i < MessageTest.elms.length; i++) { |
1714 }); | 1307 var sentObject = MessageTest.elms[i]; |
| 1308 // TODO(asiva): remove this local var idx once thew new for-loop |
| 1309 // semantics for closures is implemented. |
| 1310 var idx = i; |
| 1311 remote.call(sentObject).then(expectAsync1((var receivedObject) { |
| 1312 MessageTest.VerifyObject(idx, receivedObject); |
| 1313 })); |
| 1314 } |
| 1315 |
| 1316 // Send recursive objects and receive them back. |
| 1317 List local_list1 = ["Hello", "World", "Hello", 0xffffffffff]; |
| 1318 List local_list2 = [null, local_list1, local_list1 ]; |
| 1319 List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff]; |
| 1320 List sendObject = new List(5); |
| 1321 sendObject[0] = local_list1; |
| 1322 sendObject[1] = sendObject; |
| 1323 sendObject[2] = local_list2; |
| 1324 sendObject[3] = sendObject; |
| 1325 sendObject[4] = local_list3; |
| 1326 remote.call(sendObject).then((var replyObject) { |
| 1327 Expect.equals(true, sendObject is List); |
| 1328 Expect.equals(true, replyObject is List); |
| 1329 Expect.equals(sendObject.length, replyObject.length); |
| 1330 Expect.equals(true, replyObject[1] === replyObject); |
| 1331 Expect.equals(true, replyObject[3] === replyObject); |
| 1332 Expect.equals(true, replyObject[0] === replyObject[2][1]); |
| 1333 Expect.equals(true, replyObject[0] === replyObject[2][2]); |
| 1334 Expect.equals(true, replyObject[2] === replyObject[4][0]); |
| 1335 Expect.equals(true, replyObject[0][0] === replyObject[0][2]); |
| 1336 // Bigint literals are not canonicalized so do a == check. |
| 1337 Expect.equals(true, replyObject[0][3] == replyObject[4][4]); |
| 1338 }); |
| 1339 |
| 1340 // Shutdown the MessageServer. |
| 1341 remote.call(-1).then(expectAsync1((int message) { |
| 1342 Expect.equals(MessageTest.elms.length + 1, message); |
| 1343 })); |
| 1344 }); |
| 1345 } |
| 1346 |
| 1347 // --------------------------------------------------------------------------- |
| 1348 // tests/isolate/request_reply_test.dart |
| 1349 // --------------------------------------------------------------------------- |
| 1350 |
| 1351 void request_reply_entry() { |
| 1352 port.receive((message, SendPort replyTo) { |
| 1353 replyTo.send(message + 87); |
| 1354 port.close(); |
| 1355 }); |
| 1356 } |
| 1357 |
| 1358 void request_reply_main() { |
| 1359 test("call", () { |
| 1360 SendPort port = spawnFunction(request_reply_entry); |
| 1361 port.call(42).then(expectAsync1((message) { |
| 1362 Expect.equals(42 + 87, message); |
| 1363 })); |
| 1364 }); |
| 1365 |
| 1366 test("send", () { |
| 1367 SendPort port = spawnFunction(request_reply_entry); |
| 1368 ReceivePort reply = new ReceivePort(); |
| 1369 port.send(99, reply.toSendPort()); |
| 1370 reply.receive(expectAsync2((message, replyTo) { |
| 1371 Expect.equals(99 + 87, message); |
| 1372 reply.close(); |
| 1373 })); |
| 1374 }); |
| 1375 } |
| 1376 |
| 1377 // --------------------------------------------------------------------------- |
| 1378 // tests/isolate/count_test.dart |
| 1379 // --------------------------------------------------------------------------- |
| 1380 |
| 1381 void countMessages() { |
| 1382 int count = 0; |
| 1383 port.receive((int message, SendPort replyTo) { |
| 1384 if (message == -1) { |
| 1385 Expect.equals(10, count); |
| 1386 replyTo.send(-1, null); |
| 1387 port.close(); |
| 1388 return; |
| 1389 } |
| 1390 Expect.equals(count, message); |
| 1391 count++; |
| 1392 replyTo.send(message * 2, null); |
| 1393 }); |
| 1394 } |
| 1395 |
| 1396 void count_main() { |
| 1397 test("count 10 consecutive messages", () { |
| 1398 int count = 0; |
| 1399 SendPort remote = spawnFunction(countMessages); |
| 1400 ReceivePort local = new ReceivePort(); |
| 1401 SendPort reply = local.toSendPort(); |
| 1402 |
| 1403 local.receive(expectAsync2((int message, SendPort replyTo) { |
| 1404 if (message == -1) { |
| 1405 Expect.equals(11, count); |
| 1406 local.close(); |
| 1407 return; |
1715 } | 1408 } |
1716 | 1409 |
1717 // Send recursive objects and receive them back. | 1410 Expect.equals((count - 1) * 2, message); |
1718 List local_list1 = ["Hello", "World", "Hello", 0xffffffffff]; | 1411 remote.send(count++, reply); |
1719 List local_list2 = [null, local_list1, local_list1 ]; | 1412 if (count == 10) { |
1720 List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff]; | 1413 remote.send(-1, reply); |
1721 List sendObject = new List(5); | 1414 } |
1722 sendObject[0] = local_list1; | 1415 }, count: 11)); |
1723 sendObject[1] = sendObject; | 1416 remote.send(count++, reply); |
1724 sendObject[2] = local_list2; | 1417 }); |
1725 sendObject[3] = sendObject; | |
1726 sendObject[4] = local_list3; | |
1727 remote.call(sendObject).then((var replyObject) { | |
1728 Expect.equals(true, sendObject is List); | |
1729 Expect.equals(true, replyObject is List); | |
1730 Expect.equals(sendObject.length, replyObject.length); | |
1731 Expect.equals(true, replyObject[1] === replyObject); | |
1732 Expect.equals(true, replyObject[3] === replyObject); | |
1733 Expect.equals(true, replyObject[0] === replyObject[2][1]); | |
1734 Expect.equals(true, replyObject[0] === replyObject[2][2]); | |
1735 Expect.equals(true, replyObject[2] === replyObject[4][0]); | |
1736 Expect.equals(true, replyObject[0][0] === replyObject[0][2]); | |
1737 // Bigint literals are not canonicalized so do a == check. | |
1738 Expect.equals(true, replyObject[0][3] == replyObject[4][4]); | |
1739 }); | |
1740 | |
1741 // Shutdown the MessageServer. | |
1742 remote.call(-1).then((int message) { | |
1743 Expect.equals(MessageTest.elms.length + 1, message); | |
1744 IsolateTestFramework.done(); | |
1745 }); | |
1746 }); | |
1747 } | |
1748 } | |
1749 | |
1750 class PingPongServer extends Isolate { | |
1751 PingPongServer() : super() {} | |
1752 | |
1753 void main() { | |
1754 print("Starting server."); | |
1755 int count = 0; | |
1756 this.port.receive( | |
1757 (var message, SendPort replyTo) { | |
1758 if (message == -1) { | |
1759 this.port.close(); | |
1760 print("Stopping server."); | |
1761 replyTo.send(count, null); | |
1762 } else { | |
1763 // Check if the received object is correct. | |
1764 if (count < MessageTest.elms.length) { | |
1765 MessageTest.VerifyObject(count, message); | |
1766 } | |
1767 // Bounce the received object back so that the sender | |
1768 // can make sure that the object matches. | |
1769 replyTo.send(message, null); | |
1770 count++; | |
1771 } | |
1772 }); | |
1773 } | |
1774 } | 1418 } |
1775 | 1419 |
1776 | 1420 |
1777 // ImportOptions=IsolateTestFramework | 1421 // --------------------------------------------------------------------------- |
| 1422 // tests/isolate/mandel_isolate_test.dart |
| 1423 // --------------------------------------------------------------------------- |
1778 | 1424 |
1779 class MandelIsolateTest { | 1425 final TERMINATION_MESSAGE = -1; |
| 1426 final N = 100; |
| 1427 final ISOLATES = 20; |
1780 | 1428 |
1781 static final TERMINATION_MESSAGE = -1; | 1429 mandel_main() { |
1782 static final N = 100; | 1430 test("Render Mandelbrot in parallel", () { |
1783 static final ISOLATES = 20; | |
1784 | |
1785 static void testMain() { | |
1786 IsolateTestFramework.waitForDone(); | |
1787 final state = new MandelbrotState(); | 1431 final state = new MandelbrotState(); |
| 1432 state._validated.future.then(expectAsync1((result) { |
| 1433 expect(result, isTrue); |
| 1434 })); |
1788 for (int i = 0; i < Math.min(ISOLATES, N); i++) state.startClient(i); | 1435 for (int i = 0; i < Math.min(ISOLATES, N); i++) state.startClient(i); |
1789 } | 1436 }); |
1790 | |
1791 } | 1437 } |
1792 | 1438 |
1793 | 1439 |
1794 class MandelbrotState { | 1440 class MandelbrotState { |
1795 | 1441 |
1796 MandelbrotState() { | 1442 MandelbrotState() { |
1797 _result = new List<List<int>>(MandelIsolateTest.N); | 1443 _result = new List<List<int>>(N); |
1798 _lineProcessedBy = new List<LineProcessorClient>(MandelIsolateTest.N); | 1444 _lineProcessedBy = new List<LineProcessorClient>(N); |
1799 _sent = 0; | 1445 _sent = 0; |
1800 _missing = MandelIsolateTest.N; | 1446 _missing = N; |
| 1447 _validated = new Completer<bool>(); |
1801 } | 1448 } |
1802 | 1449 |
1803 void startClient(int id) { | 1450 void startClient(int id) { |
1804 assert(_sent < MandelIsolateTest.N); | 1451 assert(_sent < N); |
1805 final client = new LineProcessorClient(this, id); | 1452 final client = new LineProcessorClient(this, id); |
1806 client.processLine(_sent++); | 1453 client.processLine(_sent++); |
1807 } | 1454 } |
1808 | 1455 |
1809 void notifyProcessedLine(LineProcessorClient client, int y, List<int> line) { | 1456 void notifyProcessedLine(LineProcessorClient client, int y, List<int> line) { |
1810 assert(_result[y] === null); | 1457 assert(_result[y] === null); |
1811 _result[y] = line; | 1458 _result[y] = line; |
1812 _lineProcessedBy[y] = client; | 1459 _lineProcessedBy[y] = client; |
1813 | 1460 |
1814 if (_sent != MandelIsolateTest.N) { | 1461 if (_sent != N) { |
1815 client.processLine(_sent++); | 1462 client.processLine(_sent++); |
1816 } else { | 1463 } else { |
1817 client.shutdown(); | 1464 client.shutdown(); |
1818 } | 1465 } |
1819 | 1466 |
1820 // If all lines have been computed, validate the result. | 1467 // If all lines have been computed, validate the result. |
1821 if (--_missing == 0) _validateResult(); | 1468 if (--_missing == 0) { |
| 1469 _printResult(); |
| 1470 _validateResult(); |
| 1471 } |
1822 } | 1472 } |
1823 | 1473 |
1824 void _validateResult() { | 1474 void _validateResult() { |
1825 // TODO(ngeoffray): Implement this. | 1475 // TODO(ngeoffray): Implement this. |
1826 IsolateTestFramework.done(); | 1476 _validated.complete(true); |
| 1477 } |
| 1478 |
| 1479 void _printResult() { |
| 1480 var output = new StringBuffer(); |
| 1481 for (int i = 0; i < _result.length; i++) { |
| 1482 List<int> line = _result[i]; |
| 1483 for (int j = 0; j < line.length; j++) { |
| 1484 if (line[j] < 10) output.add("0"); |
| 1485 output.add(line[j]); |
| 1486 } |
| 1487 output.add("\n"); |
| 1488 } |
| 1489 // print(output); |
1827 } | 1490 } |
1828 | 1491 |
1829 List<List<int>> _result; | 1492 List<List<int>> _result; |
1830 List<LineProcessorClient> _lineProcessedBy; | 1493 List<LineProcessorClient> _lineProcessedBy; |
1831 int _sent; | 1494 int _sent; |
1832 int _missing; | 1495 int _missing; |
1833 | 1496 Completer<bool> _validated; |
1834 } | 1497 } |
1835 | 1498 |
1836 | 1499 |
1837 class LineProcessorClient { | 1500 class LineProcessorClient { |
1838 | 1501 |
1839 LineProcessorClient(MandelbrotState this._state, int this._id) { | 1502 LineProcessorClient(MandelbrotState this._state, int this._id) { |
1840 _out = new LineProcessor().spawn(); | 1503 _port = spawnFunction(processLines); |
1841 } | 1504 } |
1842 | 1505 |
1843 void processLine(int y) { | 1506 void processLine(int y) { |
1844 _out.then((SendPort p) { | 1507 _port.call(y).then((List<int> message) { |
1845 p.call(y).then((List<int> message) { | 1508 _state.notifyProcessedLine(this, y, message); |
1846 _state.notifyProcessedLine(this, y, message); | |
1847 }); | |
1848 }); | 1509 }); |
1849 } | 1510 } |
1850 | 1511 |
1851 void shutdown() { | 1512 void shutdown() { |
1852 _out.then((SendPort p) { | 1513 _port.send(TERMINATION_MESSAGE, null); |
1853 p.send(MandelIsolateTest.TERMINATION_MESSAGE, null); | |
1854 }); | |
1855 } | 1514 } |
1856 | 1515 |
1857 MandelbrotState _state; | 1516 MandelbrotState _state; |
1858 int _id; | 1517 int _id; |
1859 Future<SendPort> _out; | 1518 SendPort _port; |
1860 | |
1861 } | 1519 } |
1862 | 1520 |
| 1521 List<int> processLine(int y) { |
| 1522 double inverseN = 2.0 / N; |
| 1523 double Civ = y * inverseN - 1.0; |
| 1524 List<int> result = new List<int>(N); |
| 1525 for (int x = 0; x < N; x++) { |
| 1526 double Crv = x * inverseN - 1.5; |
1863 | 1527 |
1864 class LineProcessor extends Isolate { | 1528 double Zrv = Crv; |
| 1529 double Ziv = Civ; |
1865 | 1530 |
1866 LineProcessor() : super() { } | 1531 double Trv = Crv * Crv; |
| 1532 double Tiv = Civ * Civ; |
1867 | 1533 |
1868 void main() { | 1534 int i = 49; |
1869 this.port.receive((message, SendPort replyTo) { | 1535 do { |
1870 if (message == MandelIsolateTest.TERMINATION_MESSAGE) { | 1536 Ziv = (Zrv * Ziv) + (Zrv * Ziv) + Civ; |
1871 assert(replyTo === null); | 1537 Zrv = Trv - Tiv + Crv; |
1872 this.port.close(); | 1538 |
1873 } else { | 1539 Trv = Zrv * Zrv; |
1874 replyTo.send(_processLine(message), null); | 1540 Tiv = Ziv * Ziv; |
1875 } | 1541 } while (((Trv + Tiv) <= 4.0) && (--i > 0)); |
1876 }); | 1542 |
| 1543 result[x] = i; |
1877 } | 1544 } |
1878 | 1545 return result; |
1879 static List<int> _processLine(int y) { | |
1880 double inverseN = 2.0 / MandelIsolateTest.N; | |
1881 double Civ = y * inverseN - 1.0; | |
1882 List<int> result = new List<int>(MandelIsolateTest.N); | |
1883 for (int x = 0; x < MandelIsolateTest.N; x++) { | |
1884 double Crv = x * inverseN - 1.5; | |
1885 | |
1886 double Zrv = Crv; | |
1887 double Ziv = Civ; | |
1888 | |
1889 double Trv = Crv * Crv; | |
1890 double Tiv = Civ * Civ; | |
1891 | |
1892 int i = 49; | |
1893 do { | |
1894 Ziv = (Zrv * Ziv) + (Zrv * Ziv) + Civ; | |
1895 Zrv = Trv - Tiv + Crv; | |
1896 | |
1897 Trv = Zrv * Zrv; | |
1898 Tiv = Ziv * Ziv; | |
1899 } while (((Trv + Tiv) <= 4.0) && (--i > 0)); | |
1900 | |
1901 result[x] = i; | |
1902 } | |
1903 return result; | |
1904 } | |
1905 | |
1906 } | 1546 } |
1907 | 1547 |
1908 // Dart test program for testing that isolates are spawned. | 1548 void processLines() { |
1909 // ImportOptions=IsolateTestFramework | 1549 port.receive((message, SendPort replyTo) { |
1910 | 1550 if (message == TERMINATION_MESSAGE) { |
1911 class IsolateNegativeTest extends Isolate { | 1551 assert(replyTo == null); |
1912 IsolateNegativeTest() : super(); | 1552 port.close(); |
1913 | 1553 } else { |
1914 static void testMain() { | 1554 replyTo.send(processLine(message), null); |
1915 IsolateTestFramework.waitForDone(); | 1555 } |
1916 new IsolateNegativeTest().spawn().then((SendPort port) { | 1556 }); |
1917 port.call("foo").then((message) { | |
1918 Expect.equals(true, false); // <=-------- Should fail here. | |
1919 IsolateTestFramework.done(); | |
1920 }); | |
1921 }); | |
1922 } | |
1923 | |
1924 void main() { | |
1925 this.port.receive((ignored, replyTo) { | |
1926 replyTo.send("foo", null); | |
1927 }); | |
1928 } | |
1929 } | 1557 } |
OLD | NEW |