Bug 1837457 - Change httpserver tests to load httpd.js as a module rather than a script. r=necko-reviewers,jesup

Differential Revision: https://phabricator.services.mozilla.com/D180354
This commit is contained in:
Mark Banner 2023-06-12 07:29:20 +00:00
parent 0d74d9867e
commit af6552f825
6 changed files with 76 additions and 37 deletions

View File

@ -11,6 +11,7 @@
*/
var EXPORTED_SYMBOLS = [
"dumpn",
"HTTP_400",
"HTTP_401",
"HTTP_402",
@ -36,7 +37,11 @@ var EXPORTED_SYMBOLS = [
"HTTP_505",
"HttpError",
"HttpServer",
"LineData",
"NodeServer",
"nsHttpHeaders",
"overrideBinaryStreamsForTests",
"WriteThroughCopier",
];
const CC = Components.Constructor;
@ -194,9 +199,6 @@ function dumpStack() {
stack.forEach(dumpn);
}
/** The XPCOM thread manager. */
var gThreadManager = null;
/**
* JavaScript constructors for commonly-used classes; precreating these is a
* speedup over doing the same from base principles. See the docs at
@ -254,6 +256,16 @@ var BinaryOutputStream = CC(
"setOutputStream"
);
function overrideBinaryStreamsForTests(
inputStream,
outputStream,
responseSegmentSize
) {
BinaryInputStream = inputStream;
BinaryOutputStream = outputStream;
Response.SEGMENT_SIZE = responseSegmentSize;
}
/**
* Returns the RFC 822/1123 representation of a date.
*
@ -367,10 +379,6 @@ function printObj(o, showMembers) {
* Instantiates a new HTTP server.
*/
function nsHttpServer() {
if (!gThreadManager) {
gThreadManager = Cc["@mozilla.org/thread-manager;1"].getService();
}
/** The port on which this server listens. */
this._port = undefined;
@ -457,7 +465,7 @@ nsHttpServer.prototype = {
// Note: must use main thread here, or we might get a GC that will cause
// threadsafety assertions. We really need to fix XPConnect so that
// you can actually do things in multi-threaded JS. :-(
input.asyncWait(reader, 0, 0, gThreadManager.mainThread);
input.asyncWait(reader, 0, 0, Services.tm.mainThread);
} catch (e) {
// Assume this connection can't be salvaged and bail on it completely;
// don't attempt to close it so that we can assert that any connection
@ -504,7 +512,7 @@ nsHttpServer.prototype = {
self._notifyStopped();
},
};
gThreadManager.currentThread.dispatch(
Services.tm.currentThread.dispatch(
stopEvent,
Ci.nsIThread.DISPATCH_NORMAL
);
@ -1489,9 +1497,9 @@ RequestReader.prototype = {
"*** onInputStreamReady(input=" +
input +
") on thread " +
gThreadManager.currentThread +
Services.tm.currentThread +
" (main is " +
gThreadManager.mainThread +
Services.tm.mainThread +
")"
);
dumpn("*** this._state == " + this._state);
@ -1547,7 +1555,7 @@ RequestReader.prototype = {
}
if (this._state != READER_FINISHED) {
input.asyncWait(this, 0, 0, gThreadManager.currentThread);
input.asyncWait(this, 0, 0, Services.tm.currentThread);
}
},
@ -3020,7 +3028,7 @@ ServerHandler.prototype = {
}
let writeMore = function () {
gThreadManager.currentThread.dispatch(
Services.tm.currentThread.dispatch(
writeData,
Ci.nsIThread.DISPATCH_NORMAL
);
@ -4279,7 +4287,7 @@ Response.prototype = {
// way to handle both cases without removing bodyOutputStream access and
// moving its effective write(data, length) method onto Response, which
// would be slower and require more code than this anyway.
gThreadManager.currentThread.dispatch(
Services.tm.currentThread.dispatch(
{
run() {
dumpn("*** canceling copy asynchronously...");
@ -5019,7 +5027,7 @@ WriteThroughCopier.prototype = {
},
};
gThreadManager.currentThread.dispatch(event, Ci.nsIThread.DISPATCH_NORMAL);
Services.tm.currentThread.dispatch(event, Ci.nsIThread.DISPATCH_NORMAL);
},
/**
@ -5031,7 +5039,7 @@ WriteThroughCopier.prototype = {
this,
0,
Response.SEGMENT_SIZE,
gThreadManager.mainThread
Services.tm.mainThread
);
},
@ -5049,7 +5057,7 @@ WriteThroughCopier.prototype = {
this,
0,
pendingData[0].length,
gThreadManager.mainThread
Services.tm.mainThread
);
},
@ -5071,7 +5079,7 @@ WriteThroughCopier.prototype = {
this,
Ci.nsIAsyncOutputStream.WAIT_CLOSURE_ONLY,
0,
gThreadManager.mainThread
Services.tm.mainThread
);
},
@ -5636,7 +5644,7 @@ function server(port, basePath) {
srv.identity.setPrimary("http", "localhost", port);
srv.start(port);
var thread = gThreadManager.currentThread;
var thread = Services.tm.currentThread;
while (!srv.isStopped()) {
thread.processNextEvent(true);
}

View File

@ -3,15 +3,15 @@
/* 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/. */
/* global __LOCATION__ */
/* import-globals-from ../httpd.js */
var _HTTPD_JS_PATH = __LOCATION__.parent;
_HTTPD_JS_PATH.append("httpd.js");
load(_HTTPD_JS_PATH.path);
// if these tests fail, we'll want the debug output
var linDEBUG = true;
const {
dumpn,
LineData,
HttpServer,
nsHttpHeaders,
overrideBinaryStreamsForTests,
WriteThroughCopier,
} = ChromeUtils.import("resource://testing-common/httpd.js");
var { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
@ -20,6 +20,31 @@ var { NetUtil } = ChromeUtils.importESModule(
"resource://gre/modules/NetUtil.sys.mjs"
);
const CC = Components.Constructor;
const ScriptableInputStream = CC(
"@mozilla.org/scriptableinputstream;1",
"nsIScriptableInputStream",
"init"
);
const FileInputStream = CC(
"@mozilla.org/network/file-input-stream;1",
"nsIFileInputStream",
"init"
);
/* These two are non-const only so a test can overwrite them. */
var BinaryInputStream = CC(
"@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream",
"setInputStream"
);
var BinaryOutputStream = CC(
"@mozilla.org/binaryoutputstream;1",
"nsIBinaryOutputStream",
"setOutputStream"
);
/**
* Constructs a new nsHttpServer instance. This function is intended to
* encapsulate construction of a server so that at some point in the future it
@ -27,7 +52,7 @@ var { NetUtil } = ChromeUtils.importESModule(
* the server when used as an XPCOM component (not as an inline script).
*/
function createServer() {
return new nsHttpServer();
return new HttpServer();
}
/**

View File

@ -16,8 +16,6 @@
* have to change a little at the edges as well.
*/
gThreadManager = Cc["@mozilla.org/thread-manager;1"].createInstance();
function run_test() {
do_test_pending();
tests.push(function testsComplete(_) {
@ -387,14 +385,19 @@ function note(m) {
* changing the names of variables and properties.
*/
// These are used in head.js.
/* exported BinaryInputStream, BinaryOutputStream */
var BinaryInputStream = function BIS(stream) {
BinaryInputStream = function BIS(stream) {
return stream;
};
var BinaryOutputStream = function BOS(stream) {
BinaryOutputStream = function BOS(stream) {
return stream;
};
Response.SEGMENT_SIZE = SEGMENT.length;
// This overrides in httpd.js.
overrideBinaryStreamsForTests(
BinaryInputStream,
BinaryOutputStream,
SEGMENT.length
);
/**
* Roughly mocks an nsIPipe, presenting non-blocking input and output streams
@ -1535,7 +1538,7 @@ CopyTest.prototype = {
outputWrittenWatcher,
0,
1,
gThreadManager.currentThread
Services.tm.currentThread
);
this._waitingForData = true;
},
@ -1598,7 +1601,7 @@ CopyTest.prototype = {
}
},
};
gThreadManager.dispatchToMainThread(event);
Services.tm.dispatchToMainThread(event);
},
/**

View File

@ -6,6 +6,9 @@
// test that the LineData internal data structure works correctly
const CR = 0x0d;
const LF = 0x0a;
function run_test() {
var data = new LineData();
data.appendBytes(["a".charCodeAt(0), CR]);

View File

@ -255,7 +255,7 @@ function handleAsyncOrdering(request, response) {
// 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
gThreadManager.dispatchToMainThread(writeData);
Services.tm.dispatchToMainThread(writeData);
}
step();
response.processAsync();

View File

@ -1,6 +1,6 @@
[DEFAULT]
head = head_utils.js
support-files = data/** ../httpd.js
support-files = data/**
[test_async_response_sending.js]
[test_basic_functionality.js]