mirror of
https://github.com/RustyRaptor/brackets.git
synced 2026-12-04 00:15:22 -07:00
Fixes code hint tests by controlling worker resets.
Previously, the web worker would be restarted at somewhat random times during test runs (after a specific number of hints was requested). This change forces the worker to start afresh with each test and switches the worker so that it doesn't reset during any of the tests.
This commit is contained in:
1 parent
598007f70d
commit
f80c0c9d98
5 files changed
+171
-41
No files matched your search
@@ -36,7 +36,8 @@ define(function (require, exports, module) {
|
||||
TERN_CALLED_FUNC_TYPE_MSG = "FunctionType",
|
||||
TERN_PRIME_PUMP_MSG = "PrimePump",
|
||||
TERN_GET_GUESSES_MSG = "GetGuesses",
|
||||
TERN_WORKER_READY = "WorkerReady";
|
||||
TERN_WORKER_READY = "WorkerReady",
|
||||
SET_CONFIG = "SetConfig";
|
||||
|
||||
// Message parameter constants
|
||||
var TERN_FILE_INFO_TYPE_PART = "part",
|
||||
@@ -57,6 +58,7 @@ define(function (require, exports, module) {
|
||||
exports.TERN_FILE_INFO_TYPE_PART = TERN_FILE_INFO_TYPE_PART;
|
||||
exports.TERN_FILE_INFO_TYPE_FULL = TERN_FILE_INFO_TYPE_FULL;
|
||||
exports.TERN_FILE_INFO_TYPE_EMPTY = TERN_FILE_INFO_TYPE_EMPTY;
|
||||
exports.SET_CONFIG = SET_CONFIG;
|
||||
});
|
||||
|
||||
|
||||
@@ -61,7 +61,9 @@ define(function (require, exports, module) {
|
||||
LARGE_LINE_CHANGE = 100,
|
||||
LARGE_LINE_COUNT = 2000,
|
||||
OFFSET_ZERO = {line: 0, ch: 0};
|
||||
|
||||
|
||||
var config = {};
|
||||
|
||||
/**
|
||||
* An array of library names that contain JavaScript builtins definitions.
|
||||
*
|
||||
@@ -168,7 +170,9 @@ define(function (require, exports, module) {
|
||||
* the message will not be posted until initialization is complete
|
||||
*/
|
||||
function postMessage(msg) {
|
||||
currentWorker.postMessage(msg);
|
||||
if (currentWorker) {
|
||||
currentWorker.postMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -778,6 +782,9 @@ define(function (require, exports, module) {
|
||||
*/
|
||||
function postMessage(msg) {
|
||||
addFilesPromise.done(function (ternWorker) {
|
||||
if (config.debug) {
|
||||
console.debug("Sending message", msg);
|
||||
}
|
||||
ternWorker.postMessage(msg);
|
||||
});
|
||||
}
|
||||
@@ -788,6 +795,9 @@ define(function (require, exports, module) {
|
||||
*/
|
||||
function _postMessageByPass(msg) {
|
||||
ternPromise.done(function (ternWorker) {
|
||||
if (config.debug) {
|
||||
console.debug("Sending message", msg);
|
||||
}
|
||||
ternWorker.postMessage(msg);
|
||||
});
|
||||
}
|
||||
@@ -949,10 +959,15 @@ define(function (require, exports, module) {
|
||||
|
||||
numAddedFiles += files.length;
|
||||
ternPromise.done(function (worker) {
|
||||
worker.postMessage({
|
||||
var msg = {
|
||||
type : MessageIds.TERN_ADD_FILES_MSG,
|
||||
files : files
|
||||
});
|
||||
};
|
||||
|
||||
if (config.debug) {
|
||||
console.debug("Sending message", msg);
|
||||
}
|
||||
worker.postMessage(msg);
|
||||
});
|
||||
|
||||
} else {
|
||||
@@ -1059,6 +1074,10 @@ define(function (require, exports, module) {
|
||||
_ternWorker = new Worker(path);
|
||||
|
||||
_ternWorker.addEventListener("message", function (e) {
|
||||
if (config.debug) {
|
||||
console.debug("Message received", e);
|
||||
}
|
||||
|
||||
var response = e.data,
|
||||
type = response.type;
|
||||
|
||||
@@ -1084,6 +1103,11 @@ define(function (require, exports, module) {
|
||||
}
|
||||
});
|
||||
|
||||
// Set the initial configuration for the worker
|
||||
_ternWorker.postMessage({
|
||||
type: MessageIds.SET_CONFIG,
|
||||
config: brackets.configureJSCodeHints.config
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Create a new tern server.
|
||||
@@ -1096,12 +1120,17 @@ define(function (require, exports, module) {
|
||||
numInitialFiles = files.length;
|
||||
|
||||
ternPromise.done(function (worker) {
|
||||
worker.postMessage({
|
||||
var msg = {
|
||||
type : MessageIds.TERN_INIT_MSG,
|
||||
dir : dir,
|
||||
files : files,
|
||||
env : ternEnvironment
|
||||
});
|
||||
};
|
||||
|
||||
if (config.debug) {
|
||||
console.debug("Sending message", msg);
|
||||
}
|
||||
worker.postMessage(msg);
|
||||
});
|
||||
rootTernDir = dir + "/";
|
||||
}
|
||||
@@ -1259,7 +1288,7 @@ define(function (require, exports, module) {
|
||||
return this;
|
||||
}
|
||||
|
||||
var reseting = false;
|
||||
var resettingDeferred = null;
|
||||
|
||||
/**
|
||||
* reset the tern worker thread, if necessary.
|
||||
@@ -1268,26 +1297,53 @@ define(function (require, exports, module) {
|
||||
* the web worker instance, and start a new one. To avoid a performance
|
||||
* hit when we do this we start up a new worker, and don't kill the old
|
||||
* one unitl the new one is initialized.
|
||||
*
|
||||
* During debugging, you can turn this automatic resetting behavior off
|
||||
* by running this in the console:
|
||||
* brackets.configureJSCodeHints({ noReset: true })
|
||||
*
|
||||
* This function is also used in unit testing with the "force" flag to
|
||||
* reset the worker for each test to start with a clean environment.
|
||||
*
|
||||
* @param {Session} session
|
||||
* @param {Document} document
|
||||
* @param {boolean} force true to force a reset regardless of how long since the last one
|
||||
* @return {Promise} resolved when the new worker is ready (the new worker is passed to the callback)
|
||||
*/
|
||||
function maybeReset(session, document) {
|
||||
function _maybeReset(session, document, force) {
|
||||
var newWorker;
|
||||
// if we're in the middle of a reset, don't have to check
|
||||
// the new worker will be online soon
|
||||
if (!reseting) {
|
||||
if (++_hintCount > MAX_HINTS) {
|
||||
reseting = true;
|
||||
if (!resettingDeferred) {
|
||||
|
||||
// We don't reset if the debugging flag is set
|
||||
// because it's easier to debug if the worker isn't
|
||||
// getting shut down all the time.
|
||||
if (force || (!config.noReset && ++_hintCount > MAX_HINTS)) {
|
||||
if (config.debug) {
|
||||
console.debug("Resetting tern worker");
|
||||
}
|
||||
|
||||
resettingDeferred = new $.Deferred();
|
||||
newWorker = new TernWorker();
|
||||
newWorker.handleEditorChange(session, document, null);
|
||||
newWorker.whenReady(function () {
|
||||
// tell the old worker to shut down
|
||||
currentWorker.closeWorker();
|
||||
currentWorker = newWorker;
|
||||
resettingDeferred.resolve(currentWorker);
|
||||
// all done reseting
|
||||
reseting = false;
|
||||
resettingDeferred = null;
|
||||
});
|
||||
_hintCount = 0;
|
||||
} else {
|
||||
var d = new $.Deferred();
|
||||
d.resolve(currentWorker);
|
||||
return d.promise();
|
||||
}
|
||||
}
|
||||
|
||||
return resettingDeferred.promise();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1338,7 +1394,7 @@ define(function (require, exports, module) {
|
||||
fileInfo = getFileInfo(session),
|
||||
offset = getOffset(session, fileInfo, null);
|
||||
|
||||
maybeReset(session, document);
|
||||
_maybeReset(session, document);
|
||||
|
||||
hintPromise = getTernHints(fileInfo, offset, sessionType.property);
|
||||
|
||||
@@ -1372,6 +1428,9 @@ define(function (require, exports, module) {
|
||||
var changed = documentChanges;
|
||||
if (changed === null) {
|
||||
documentChanges = changed = {from: changeList.from.line, to: changeList.from.line};
|
||||
if (config.debug) {
|
||||
console.debug("ScopeManager: document has changed");
|
||||
}
|
||||
}
|
||||
|
||||
var end = changeList.from.line + (changeList.text.length - 1);
|
||||
@@ -1438,12 +1497,26 @@ define(function (require, exports, module) {
|
||||
initPreferences(projectRootPath);
|
||||
}
|
||||
|
||||
|
||||
/** Used to avoid timing bugs in unit tests */
|
||||
function _readyPromise() {
|
||||
return deferredPreferences;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*
|
||||
* Update the configuration in the worker.
|
||||
*/
|
||||
function _setConfig(configUpdate) {
|
||||
config = brackets.configureJSCodeHints.config;
|
||||
postMessage({
|
||||
type: MessageIds.SET_CONFIG,
|
||||
config: configUpdate
|
||||
});
|
||||
}
|
||||
|
||||
exports._setConfig = _setConfig;
|
||||
exports._maybeReset = _maybeReset;
|
||||
exports.getBuiltins = getBuiltins;
|
||||
exports.getResolvedPath = getResolvedPath;
|
||||
exports.getTernHints = getTernHints;
|
||||
|
||||
@@ -55,6 +55,27 @@ define(function (require, exports, module) {
|
||||
matcher = null, // string matcher for hints
|
||||
ignoreChange; // can ignore next "change" event if true;
|
||||
|
||||
/**
|
||||
* Sets the configuration, generally for testing/debugging use.
|
||||
* Configuration keys are merged into the current configuration.
|
||||
* The Tern worker is automatically updated to the new config as well.
|
||||
*
|
||||
* * debug: Set to true if you want verbose logging
|
||||
* * noReset: Set to true if you don't want the worker to restart periodically
|
||||
*
|
||||
* @param {Object} configUpdate keys/values to merge into the config
|
||||
*/
|
||||
function setConfig(configUpdate) {
|
||||
var config = setConfig.config;
|
||||
Object.keys(configUpdate).forEach(function (key) {
|
||||
config[key] = configUpdate[key];
|
||||
});
|
||||
|
||||
ScopeManager._setConfig(configUpdate);
|
||||
}
|
||||
|
||||
setConfig.config = {};
|
||||
|
||||
/**
|
||||
* Get the value of current session.
|
||||
* Used for unit testing.
|
||||
@@ -79,6 +100,10 @@ define(function (require, exports, module) {
|
||||
var trimmedQuery,
|
||||
formattedHints;
|
||||
|
||||
if (setConfig.config.debug) {
|
||||
console.debug("Hints", _.pluck(hints, "label"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns a formatted list of hints with the query substring
|
||||
* highlighted.
|
||||
@@ -578,8 +603,10 @@ define(function (require, exports, module) {
|
||||
* for changes
|
||||
*/
|
||||
function uninstallEditorListeners(editor) {
|
||||
$(editor)
|
||||
.off(HintUtils.eventName("change"));
|
||||
if (editor) {
|
||||
$(editor)
|
||||
.off(HintUtils.eventName("change"));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -745,9 +772,12 @@ define(function (require, exports, module) {
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
// Register quickEditHelper.
|
||||
brackets._jsCodeHintsHelper = quickEditHelper;
|
||||
|
||||
// Configuration function used for debugging
|
||||
brackets.configureJSCodeHints = setConfig;
|
||||
|
||||
ExtensionUtils.loadStyleSheet(module, "styles/brackets-js-hints.css");
|
||||
|
||||
|
||||
@@ -26,6 +26,8 @@
|
||||
|
||||
importScripts("thirdparty/requirejs/require.js");
|
||||
|
||||
var config = {};
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
@@ -473,7 +475,6 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
* the offset into the file where we want completions for
|
||||
*/
|
||||
function handleFunctionType(fileInfo, offset) {
|
||||
|
||||
var request = buildRequest(fileInfo, "type", offset),
|
||||
error;
|
||||
|
||||
@@ -482,7 +483,12 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
var fnType = "";
|
||||
try {
|
||||
ternServer.request(request, function (error, data) {
|
||||
|
||||
|
||||
if (error) {
|
||||
_log("Error for Tern request: \n" + JSON.stringify(request) + "\n" + error);
|
||||
return;
|
||||
}
|
||||
|
||||
var file = ternServer.findFile(fileInfo.name);
|
||||
|
||||
// convert query from partial to full offsets
|
||||
@@ -497,6 +503,7 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
Infer.resetGuessing();
|
||||
var type = Infer.expressionType(expr);
|
||||
type = type.getFunctionType() || type.getType();
|
||||
|
||||
if (type) {
|
||||
fnType = getParameters(type);
|
||||
} else {
|
||||
@@ -506,7 +513,7 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
});
|
||||
} catch (e) {
|
||||
error = e.message;
|
||||
_log("Error thrown in tern_worker:" + error);
|
||||
_log("Error thrown in tern_worker:" + error + "\n" + e.stack);
|
||||
}
|
||||
|
||||
// Post a message back to the main thread with the completions
|
||||
@@ -565,11 +572,28 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the configuration, typically for debugging purposes.
|
||||
* Keys set in the config object passed in are merged into the
|
||||
* configuration.
|
||||
*
|
||||
* @param {Object} configUpdate updated config keys
|
||||
*/
|
||||
function setConfig(configUpdate) {
|
||||
Object.keys(configUpdate).forEach(function (key) {
|
||||
config[key] = configUpdate[key];
|
||||
});
|
||||
}
|
||||
|
||||
self.addEventListener("message", function (e) {
|
||||
var file, text, offset,
|
||||
request = e.data,
|
||||
type = request.type;
|
||||
|
||||
if (config.debug) {
|
||||
_log("Message received " + type);
|
||||
}
|
||||
|
||||
if (type === MessageIds.TERN_INIT_MSG) {
|
||||
|
||||
var env = request.env,
|
||||
@@ -597,6 +621,8 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
getTernProperties(request.fileInfo, offset, MessageIds.TERN_GET_GUESSES_MSG);
|
||||
} else if (type === MessageIds.TERN_UPDATE_FILE_MSG) {
|
||||
handleUpdateFile(request.path, request.text);
|
||||
} else if (type === MessageIds.SET_CONFIG) {
|
||||
setConfig(request.config);
|
||||
} else {
|
||||
_log("Unknown message: " + JSON.stringify(request));
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
*/
|
||||
|
||||
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
|
||||
/*global define, describe, it, xit, expect, beforeEach, afterEach, waitsFor, runs, $, brackets, waits, waitsForDone, spyOn */
|
||||
/*global define, describe, xdescribe, it, xit, expect, beforeEach, afterEach, waitsFor, runs, $, brackets, waitsForDone, beforeFirst, afterLast, spyOn */
|
||||
|
||||
define(function (require, exports, module) {
|
||||
"use strict";
|
||||
@@ -57,18 +57,7 @@ define(function (require, exports, module) {
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Returns an Editor suitable for use in isolation, given a Document.
|
||||
*
|
||||
* @param {Document} doc - the document to be contained by the new Editor
|
||||
* @return {Editor} - the mock editor object
|
||||
*/
|
||||
function createMockEditor(doc) {
|
||||
return SpecRunnerUtils.createMockEditorForDocument(doc);
|
||||
}
|
||||
|
||||
describe("JavaScript Code Hinting", function () {
|
||||
|
||||
/*
|
||||
* Ask provider for hints at current cursor position; expect it to
|
||||
* return some
|
||||
@@ -436,10 +425,10 @@ define(function (require, exports, module) {
|
||||
|
||||
// create Editor instance (containing a CodeMirror instance)
|
||||
runs(function () {
|
||||
testEditor = createMockEditor(testDoc);
|
||||
testEditor = SpecRunnerUtils.createMockEditorForDocument(testDoc);
|
||||
preTestText = testDoc.getText();
|
||||
|
||||
waitsForDone(ScopeManager._readyPromise());
|
||||
waitsForDone(ScopeManager._maybeReset(JSCodeHints.getSession(), testDoc, true));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -457,7 +446,18 @@ define(function (require, exports, module) {
|
||||
}
|
||||
|
||||
describe("JavaScript Code Hinting Basic", function () {
|
||||
|
||||
beforeFirst(function () {
|
||||
brackets.configureJSCodeHints({
|
||||
noReset: true
|
||||
});
|
||||
});
|
||||
|
||||
afterLast(function () {
|
||||
brackets.configureJSCodeHints({
|
||||
noReset: false
|
||||
});
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
setupTest(testPath, false);
|
||||
});
|
||||
@@ -725,7 +725,7 @@ define(function (require, exports, module) {
|
||||
var start = { line: 6, ch: 0 },
|
||||
middle = { line: 6, ch: 3 },
|
||||
end = { line: 6, ch: 8 };
|
||||
|
||||
|
||||
testDoc.replaceRange("A1.", start, start);
|
||||
testEditor.setCursorPos(middle);
|
||||
var hintObj = expectHints(JSCodeHints.jsHintProvider);
|
||||
@@ -896,7 +896,7 @@ define(function (require, exports, module) {
|
||||
});
|
||||
});
|
||||
|
||||
xit("should list function defined from .prototype", function () {
|
||||
it("should list function defined from .prototype", function () {
|
||||
var start = { line: 59, ch: 5 };
|
||||
|
||||
testEditor.setCursorPos(start);
|
||||
@@ -904,10 +904,9 @@ define(function (require, exports, module) {
|
||||
runs(function () {
|
||||
hintsPresentExact(hintObj, ["calc"]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
xit("should list function type defined from .prototype", function () {
|
||||
it("should list function type defined from .prototype", function () {
|
||||
var start = { line: 59, ch: 10 };
|
||||
testEditor.setCursorPos(start);
|
||||
runs(function () {
|
||||
@@ -915,7 +914,7 @@ define(function (require, exports, module) {
|
||||
});
|
||||
});
|
||||
|
||||
xit("should list function inherited from super class", function () {
|
||||
it("should list function inherited from super class", function () {
|
||||
var start = { line: 79, ch: 11 };
|
||||
testEditor.setCursorPos(start);
|
||||
var hintObj = expectHints(JSCodeHints.jsHintProvider);
|
||||
|
||||
Reference in new issue
Block a user