Bug 1295453 - Don't wait for data to be sent when closing socket immediately r?jdm
We can't guarantee any data will be written, it should suffice to just
make sure that the socket closes immediately and then verify that
a complete set of data wasn't read.
MozReview-Commit-ID: CHHuJEGRVgX
--- a/dom/network/tests/test_tcpsocket_client_and_server_basics.js
+++ b/dom/network/tests/test_tcpsocket_client_and_server_basics.js
@@ -30,33 +30,43 @@ function assertUint8ArraysEqual(a, b, co
/**
* Helper method to add event listeners to a socket and provide two Promise-returning
* helpers (see below for docs on them). This *must* be called during the turn of
* the event loop where TCPSocket's constructor is called or the onconnect method is being
* invoked.
*/
function listenForEventsOnSocket(socket, socketType) {
let wantDataLength = null;
+ let wantDataAndClose = false;
let pendingResolve = null;
let receivedEvents = [];
let receivedData = null;
let handleGenericEvent = function(event) {
dump('(' + socketType + ' event: ' + event.type + ')\n');
if (pendingResolve && wantDataLength === null) {
pendingResolve(event);
pendingResolve = null;
} else {
receivedEvents.push(event);
}
};
socket.onopen = handleGenericEvent;
socket.ondrain = handleGenericEvent;
socket.onerror = handleGenericEvent;
- socket.onclose = handleGenericEvent;
+ socket.onclose = function(event) {
+ if (!wantDataAndClose) {
+ handleGenericEvent(event);
+ } else if (pendingResolve) {
+ dump('(' + socketType + ' event: close)\n');
+ pendingResolve(receivedData);
+ pendingResolve = null;
+ wantDataAndClose = false;
+ }
+ }
socket.ondata = function(event) {
dump('(' + socketType + ' event: ' + event.type + ' length: ' +
event.data.byteLength + ')\n');
ok(socketCompartmentInstanceOfArrayBuffer(event.data),
'payload is ArrayBuffer');
var arr = new Uint8Array(event.data);
if (receivedData === null) {
receivedData = arr;
@@ -109,16 +119,29 @@ function listenForEventsOnSocket(socket,
receivedData = null;
return promise;
}
dump('(' + socketType + ' waiting for ' + length + ' bytes)\n');
return new Promise(function(resolve, reject) {
pendingResolve = resolve;
wantDataLength = length;
});
+ },
+ waitForAnyDataAndClose: function() {
+ if (pendingResolve) {
+ throw new Error('only one wait allowed at a time.');
+ }
+
+ return new Promise(function(resolve, reject) {
+ pendingResolve = resolve;
+ // we may receive no data before getting close, in which case we want to
+ // return an empty array
+ receivedData = new Uint8Array();
+ wantDataAndClose = true;
+ });
}
};
}
/**
* Return a promise that is resolved when the server receives a connection. The
* promise is resolved with { socket, queue } where `queue` is the result of
* calling listenForEventsOnSocket(socket). This must be done because we need
@@ -374,23 +397,20 @@ function* test_basics() {
// -- Attempt to send two non-string data.
is(clientSocket.send(bigUint8Array.buffer, 0, bigUint8Array.length), false,
'Server sending more than 64k should result in the buffer being full.');
is(clientSocket.send(bigUint8Array.buffer, 0, bigUint8Array.length), false,
'Server sending more than 64k should result in the buffer being full.');
clientSocket.closeImmediately();
- serverReceived = yield serverQueue.waitForDataWithAtLeastLength(1);
+ serverReceived = yield serverQueue.waitForAnyDataAndClose();
is(serverReceived.length < (2 * bigUint8Array.length), true, 'Received array length less than sent array length');
- is((yield serverQueue.waitForEvent()).type, 'close',
- 'The close event is received after calling closeImmediately');
-
// -- Close the listening server (and try to connect)
// We want to verify that the server actually closes / stops listening when
// we tell it to.
listeningServer.close();
// - try and connect, get an error
clientSocket = createSocket('127.0.0.1', serverPort,
{ binaryType: 'arraybuffer' });