mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 16:41:07 +00:00
![lrn@google.com](/assets/img/avatar_default.png)
Remove AS_EVENT as priority of ping/kill. It wasn't very usable or predictable. Make priority consistently named and a named parameter. Add response object to ping/addExitListener so it doesn't have to send null. This is useful for cases where you want to use the same receive port for multiple purposes. R=sgjesse@google.com Review URL: https://codereview.chromium.org//1074223002 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@45092 260f80e4-7a28-3924-810f-c04153c831b5
127 lines
3.7 KiB
Dart
127 lines
3.7 KiB
Dart
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import "dart:isolate";
|
|
import "dart:async";
|
|
import "package:async_helper/async_helper.dart";
|
|
|
|
void isomain(SendPort replyPort) {
|
|
RawReceivePort port = new RawReceivePort();
|
|
port.handler = (v) {
|
|
if (v == 0) {
|
|
// Shut down when receiving the 0 message.
|
|
port.close();
|
|
} else {
|
|
replyPort.send(v);
|
|
}
|
|
};
|
|
replyPort.send(port.sendPort);
|
|
}
|
|
|
|
|
|
void main() {
|
|
testExit();
|
|
testCancelExit();
|
|
testOverrideResponse();
|
|
}
|
|
|
|
void testExit() {
|
|
bool mayComplete = false;
|
|
asyncStart();
|
|
var completer = new Completer(); // Completed by first reply from isolate.
|
|
RawReceivePort reply = new RawReceivePort(completer.complete);
|
|
RawReceivePort onExitPort;
|
|
onExitPort = new RawReceivePort((v) {
|
|
if (v != "RESPONSE") throw "WRONG RESPONSE: $v";
|
|
reply.close();
|
|
onExitPort.close();
|
|
if (!mayComplete) throw "COMPLETED EARLY";
|
|
asyncEnd();
|
|
});
|
|
Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) {
|
|
isolate.addOnExitListener(onExitPort.sendPort, response: "RESPONSE");
|
|
return completer.future;
|
|
}).then((echoPort) {
|
|
int counter = 4;
|
|
reply.handler = (v) {
|
|
if (v != counter) throw "WRONG REPLY";
|
|
if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN";
|
|
counter--;
|
|
mayComplete = (counter == 0);
|
|
echoPort.send(counter);
|
|
};
|
|
echoPort.send(counter);
|
|
});
|
|
}
|
|
|
|
|
|
void testCancelExit() {
|
|
bool mayComplete = false;
|
|
asyncStart();
|
|
var completer = new Completer(); // Completed by first reply from isolate.
|
|
RawReceivePort reply = new RawReceivePort(completer.complete);
|
|
RawReceivePort onExitPort2 = new RawReceivePort((_) {
|
|
throw "RECEIVED EXIT MESSAGE";
|
|
});
|
|
RawReceivePort onExitPort1;
|
|
onExitPort1 = new RawReceivePort((_) {
|
|
reply.close();
|
|
onExitPort1.close();
|
|
if (!mayComplete) throw "COMPLETED EARLY";
|
|
new Timer(const Duration(milliseconds: 0), () {
|
|
onExitPort2.close();
|
|
asyncEnd();
|
|
});
|
|
});
|
|
Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) {
|
|
isolate.addOnExitListener(onExitPort2.sendPort);
|
|
isolate.addOnExitListener(onExitPort1.sendPort);
|
|
return completer.future.then((echoPort) {
|
|
int counter = 4;
|
|
reply.handler = (v) {
|
|
if (v != counter) throw "WRONG REPLY";
|
|
if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN";
|
|
counter--;
|
|
mayComplete = (counter == 0);
|
|
if (counter == 1) {
|
|
// Remove listener 2, keep listener 1.
|
|
isolate.removeOnExitListener(onExitPort2.sendPort);
|
|
}
|
|
echoPort.send(counter);
|
|
};
|
|
echoPort.send(counter);
|
|
});
|
|
});
|
|
}
|
|
|
|
void testOverrideResponse() {
|
|
bool mayComplete = false;
|
|
asyncStart();
|
|
var completer = new Completer(); // Completed by first reply from isolate.
|
|
RawReceivePort reply = new RawReceivePort(completer.complete);
|
|
RawReceivePort onExitPort;
|
|
onExitPort = new RawReceivePort((v) {
|
|
if (v != "RESPONSE2") throw "WRONG RESPONSE: $v";
|
|
reply.close();
|
|
onExitPort.close();
|
|
if (!mayComplete) throw "COMPLETED EARLY";
|
|
asyncEnd();
|
|
});
|
|
Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) {
|
|
isolate.addOnExitListener(onExitPort.sendPort, response: "RESPONSE");
|
|
isolate.addOnExitListener(onExitPort.sendPort, response: "RESPONSE2");
|
|
return completer.future;
|
|
}).then((echoPort) {
|
|
int counter = 4;
|
|
reply.handler = (v) {
|
|
if (v != counter) throw "WRONG REPLY";
|
|
if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN";
|
|
counter--;
|
|
mayComplete = (counter == 0);
|
|
echoPort.send(counter);
|
|
};
|
|
echoPort.send(counter);
|
|
});
|
|
}
|