/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ /* vim:set ts=2 sw=2 sts=2 et: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* * Tests for correct behavior of asynchronous responses.
*/
ChromeUtils.defineLazyGetter(this, "PREPATH", function () { return"http://localhost:" + srv.identity.primaryPort;
});
var srv;
function run_test() {
srv = createServer(); for (var path in handlers) {
srv.registerPathHandler(path, handlers[path]);
}
srv.start(-1);
runHttpTests(tests, testComplete(srv));
}
/** ************* * BEGIN TESTS *
***************/
ChromeUtils.defineLazyGetter(this, "tests", function () { return [ new Test(PREPATH + "/handleSync", null, start_handleSync, null), new Test(
PREPATH + "/handleAsync1", null,
start_handleAsync1,
stop_handleAsync1
), new Test(
PREPATH + "/handleAsync2",
init_handleAsync2,
start_handleAsync2,
stop_handleAsync2
), new Test(
PREPATH + "/handleAsyncOrdering", null, null,
stop_handleAsyncOrdering
),
];
});
var handlers = {};
function handleSync(request, response) {
response.setStatusLine(request.httpVersion, 500, "handleSync fail");
try {
response.finish();
do_throw("finish called on sync response");
} catch (e) {
isException(e, Cr.NS_ERROR_UNEXPECTED);
}
function start_handleAsync1(ch) { Assert.equal(ch.responseStatus, 200); Assert.equal(ch.responseStatusText, "New status line!"); Assert.equal(ch.getResponseHeader("X-Foo"), "new value");
}
function stop_handleAsync1(ch, status, data) { Assert.equal(data.length, 0);
}
/* * Tests that accessing output stream *before* calling processAsync() works * correctly, sending written data immediately as it is written, not buffering * until finish() is called -- which for this much data would mean we would all * but certainly deadlock, since we're trying to read/write all this data in one * process on a single thread.
*/ function handleAsyncOrdering(request, response) { var out = new BinaryOutputStream(response.bodyOutputStream);
var data = []; for (var i = 0; i < 65536; i++) {
data[i] = 0;
} var count = 20;
var writeData = {
run() { if (count-- === 0) {
response.finish(); return;
}
try {
out.writeByteArray(data);
step();
} catch (e) { try {
do_throw("error writing data: " + e);
} finally {
response.finish();
}
}
},
}; function step() { // Use gThreadManager here because it's expedient, *not* because it's // intended for public use! If you do this in client code, expect me to // knowingly break your code by changing the variable name. :-P
Services.tm.dispatchToMainThread(writeData);
}
step();
response.processAsync();
}
handlers["/handleAsyncOrdering"] = handleAsyncOrdering;
function stop_handleAsyncOrdering(ch, status, data) { Assert.equal(data.length, 20 * 65536);
data.forEach(function (v, index) { if (v !== 0) {
do_throw("value " + v + " at index " + index + " should be zero");
}
});
}
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet)
¤
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 ist noch experimentell.