/** * TestServer: A single instance of this is created as |serv|. When created, * it starts listening on the loopback address on port |serv.port|. Tests will * connect to it after setting |serv.acceptCallback|, which is invoked after it * accepts a connection. * * Within |serv.acceptCallback|, various properties of |serv| can be used to * run checks. After the callback, the connection is closed, but the server * remains listening until |serv.stop| * * Note: TestServer can only handle a single connection at a time. Tests * should use run_next_test at the end of |serv.acceptCallback| to start the * following test which creates a connection.
*/ function TestServer() { this.reset();
// start server. // any port (-1), loopback only (true), default backlog (-1) this.listener = ServerSocket(-1, true, -1); this.port = this.listener.port;
info("server: listening on " + this.port); this.listener.asyncListen(this);
}
// one connection at a time. if (this.input !== null) { try {
socket.close();
} catch (ignore) {}
do_throw("Test written to handle one connection at a time.");
}
this.acceptCallback();
} catch (e) { /* In a native callback such as onSocketAccepted, exceptions might not * get output correctly or logged to test output. Send them through
* do_throw, which fails the test immediately. */
do_report_unexpected_exception(e, "in TestServer.onSocketAccepted");
}
this.reset();
},
onStopListening() {},
/** * Called to close a connection and clean up properties.
*/
reset() { if (this.input) { try { this.input.close();
} catch (ignore) {}
} if (this.output) { try { this.output.close();
} catch (ignore) {}
}
/** * Cleanup for TestServer and this test case.
*/
stop() { this.reset(); try { this.listener.close();
} catch (ignore) {}
},
};
/** * Helper function. * Compares two nsINetAddr objects and ensures they are logically equivalent.
*/ function checkAddrEqual(lhs, rhs) { Assert.equal(lhs.family, rhs.family);
if (lhs.family === Ci.nsINetAddr.FAMILY_INET) { Assert.equal(lhs.address, rhs.address); Assert.equal(lhs.port, rhs.port);
}
/* TODO: fully support ipv6 and local */
}
/** * An instance of SocketTransportService, used to create connections.
*/ var sts;
/** * Single instance of TestServer
*/ var serv;
/** * A place for individual tests to place Objects of importance for access * throughout asynchronous testing. Particularly important for any output or * input streams opened, as cleanup of those objects (by the garbage collector) * causes the stream to close and may have other side effects.
*/ var testDataStore = null;
// Useful timeout for debugging test hangs /*serv.timeoutCallback = function(tname) { if (tname === 'testIpv4') do_throw('testIpv4 never completed a connection to TestServ'); };
do_timeout(connectTimeout, function(){ serv.timeoutCallback('testIpv4'); });*/
testDataStore.transport = sts.createTransport(
[], "127.0.0.1",
serv.port, null, null
); /* * Need to hold |output| so that the output stream doesn't close itself and * the associated connection.
*/
testDataStore.output = testDataStore.transport.openOutputStream(
Ci.nsITransport.OPEN_BLOCKING,
0,
0
);
/* NEXT: * openOutputStream -> onSocketAccepted -> acceptedCallback -> run_next_test * OR (if the above timeout is uncommented) * <connectTimeout lapses> -> timeoutCallback -> do_throw
*/
}
/** * Running the tests.
*/ function run_test() {
sts = Cc["@mozilla.org/network/socket-transport-service;1"].getService(
Ci.nsISocketTransportService
);
serv = new TestServer();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.